Python 201 Releasing in 2 Weeks!

My second book, Python 201: Intermediate Python is releasing two weeks from today on September 6th, 2016. I just wanted to remind anyone who is interested that you can pre-order a signed paperback copy of the book here right up until release day. You will also receive a copy of the book in the following digital formats: PDF, EPUB and MOBI (Kindle format).

My book is also available for early release digitally on Gumroad and Leanpub.

Check out either of those links for more information!

ANN: The wxPython Cookbook Kickstarter

Several years ago, the readers of this blog asked me to take some of my articles and turn them into a cookbook on wxPython. I have finally decided to do just that. I am including over 50 recipes that I am currently editing to make them more consistent and updating them to be compatible with the latest versions of wxPython. I currently have nearly 300 pages of content!

To help fund the initial production of the book, I am doing a fun little Kickstarter campaign for the project. The money raised will be used for the unique perks offered in the campaign as well as various production costs related to the book, such as ISBN acquisition, artwork, software expenses, advertising, etc.

In case you don’t know what wxPython is, the wxPython package is a popular toolkit for creating cross platform desktop user interfaces. It works on Windows, Mac and Linux with little to no modification of your code base.

The examples in my book will work with both wxPython 3.0.2 Classic as well as wxPython Phoenix, which is the bleeding edge of wxPython that supports Python 3. If I discover any recipes that do not work with Phoenix, they will be clearly marked or there will be an alternative example given that does work.

Here is a listing of the current set of recipes in no particular order:

  • Adding / Removing Widgets Dynamically
  • How to put a background image on a panel
  • Binding Multiple Widgets to the Same Handler
  • Catching Exceptions from Anywhere
  • wxPython’s Context Managers
  • Converting wx.DateTime to Python datetime
  • Creating an About Box
  • How to Create a Login Dialog
  • How to Create a “Dark Mode”
  • Generating a Dialog from a Config File
  • How to Disable a Wizard’s Next Button
  • How to Use Drag and Drop
  • How to Drag and Drop a File From Your App to the OS
  • How to Edit Your GUI Interactively Using reload()
  • How to Embed an Image in the Title Bar
  • Extracting XML from the RichTextCtrl
  • How to Fade-in a Frame / Dialog
  • How to Fire Multiple Event Handlers
  • Making your Frame Maximize or Full Screen
  • Using wx.Frame Styles
  • Get the Event Name Instead of an Integer
  • How to Get Children Widgets from a Sizer
  • How to Use the Clipboard
  • Catching Key and Char Events
  • Learning How Focus Works in wxPython
  • Making Your Text Flash
  • Minimizing to System Tray
  • Using ObjectListView instead of ListCtrl
  • Making a Panel Self-Destruct
  • How to Switch Between Panels
  • wxPython: Using PyDispatcher instead of Pubsub
  • Creating Graphs with PyPlot
  • Redirect Python’s Logging Module to a TextCtrl
  • Redirecting stdout / stderr
  • Resetting the Background Color
  • Saving Data to a Config File
  • How to Take a Screenshot of Your wxPython App and Print it
  • Creating a Simple Notebook
  • Ensuring Only One Instance Per Frame
  • Storing Objects in ComboBox or ListBox Widgets
  • Syncing Scrolling Between Two Grids
  • Creating Taskbar Icons
  • A wx.Timer Tutorial
  • How to Update a Progress Bar from a Thread
  • Updating Your Application with Esky
  • Creating a URL Shortener
  • Using Threads in wxPython
  • How to Create a Grid in XRC
  • An Introduction to XRC

 Note: Recipe names and order are subject to change


PyDev of the Week: Michele Simionato

This week we welcome Michele Simionato as our PyDev of the Week! Michele is an expert on Python and is known for his paper on Python’s Method Resolution Order which was published to the Python website by Guide Van Rossum and for a very interesting series of articles on metaclasses that he wrote with David Mertz. They are a bit difficult to find, but you can read the first one of the 3-part series here. He is one of the founders of the Italian Python Association. Michele has a Ph. D. about the Renormalization of Quantum Field Theory. Let’s take a few moments


Can you tell us a little about yourself (hobbies, education, etc):

I originally come from academia and I have a Ph. D. in Theoretical Physics. Then I worked for several years for an Analytics firm (stock market risk assessment) and now I am back to science, doing earthquake simulations at GEM.

Why did you start using Python?

It happened in 2002. At the time I was a postdoc researcher in the department of Physics and Astronomy at Pittsburgh University. I decided that it was time to learn some modern programming language, in view of a possible career outside academia. After reading a couple of long books by Bruce Eckel, first about C++ and then about Java, I decided that I did not want to program in either of them. I was in doubt between Ruby and Python, but Python won because of the better scientific libraries and of the more pragmatic philosophy.

What other programming languages do you know and which is your favorite?

A long time ago I started with Basic and Pascal and later on I worked a lot with Mathematica and Maple. After learning Python I become interested in functional languages and I know decently well Scheme, so much that I nearly wrote a book on it, The Adventures of a Pythonista in Schemeland. In my daily job I had to work a lot with SQL (which I like enough) and with Javascript (which I don’t like).

What projects are you working on now?

In the last three years I have become the maintainer and the main developer of the OpenQuake Engine, which is a computational engine to produce earthquake hazard and risk assessment. It means that after several years of being a database and Web developer I have become a scientific programmer and now I spend most of my time doing performance analysis of massive distributed calculations. I also keep a blog where I document my fighting with the engine.

Which Python libraries are your favorite (core or 3rd party)?

numpy is a really well thought library, an essential tool for people doing scientific applications.

Where do you see Python going as a programming language?

Honestly, I am unsure about where Python as a language is going, and I am not even convinced I like the recent trend. Certainly I would like for the language to become simpler, that’s what attracted me to Python in the first place, and instead I see several things that are becoming increasingly complicated. Also, there are now other languages out there that are worth of note, whereas for years Python had no competitors. If you want to know, I am thinking about Go for server side programming and about Julia for scientific programming. Both of them looks really interesting even if I have not programmed in either of them. Python should not rest thinking that it is best than Java and C++ (an easy win) and instead consider seriously the new contenders.

What is your take on the current market for Python programmers?

It has always been a good market for Python programmers (at least from when I started, 14 years ago) and now it is even more so. I get offers for Python jobs nearly every week.

Is there anything else you’d like to say?

My tagline at the EuroPython 2016 conference was “Legacy code warrior”: that reflects my daily job in the last 10 years at least. You can see a video of my talk here:

Thanks for doing the interview!

PyDev of the Week: Harry Percival

This week we welcome Harry Percival (@hjwp) as our PyDev of the Week! Harry is the author of Test-Driven Development with Python. You can visit his website to learn more about the book and even read it for free! Harry is also a programmer at PythonAnywhere which allows you to host, run and code Python in the cloud. Let’s take a few moments to learn more about our fellow Pythoneer!


Can you tell us a little about yourself (hobbies, education, etc):

Although my childhood started out on a promisingly nerdy path — programming in Basic on French Thomson TO-7s whose keys go “boop” when you press them — things went rather wrong and I ended up as a management consultant, which left me with an enduring fondness for spreadsheets and dislike for expressions like “leverage”, “utilize” (just say use!) and “going forwards”.

I did my CS degree by distance learning while living in Italy — because my wife is an opera singer, and she wanted to learn Italian. We lived by the beach. It was rough. Actually that’s the reason for my second job: I moonlight as captions operator for Give me half a chance and I’ll bend your ear about using reveal.js and websockets to stream opera captions to mobile devices. Also I just had a baby.

Continue reading PyDev of the Week: Harry Percival

PyDev of the Week: Ben Nuttall

This week we welcome Ben Nuttall (@ben_nuttall) as our PyDev of the Week. Ben is a Raspberry Pi Community Manager and is the creator of GPIO Zero, which is a simple interface to the GPIO components on the Raspberry Pi. You should also check out his website to see what Ben is up to. Let’s take a few moments to get to know Ben better!

ben-nuttall-firstbuild (copy)

Can you tell us a little about yourself (hobbies, education, etc):

As well as programming I like whitewater kayaking, and a mix of other outdoor pursuits. Also I recently got into photography (and have lots to learn).

I studied Mathematics and Computing at university, and then worked in the software industry for a couple of years. Around this time I was getting more involved in my local tech community, attending user groups and conferences. I set up a community event for Raspberry Pi (called Raspberry Jam), and did some events with schools to help teachers them use Raspberry Pi in their classrooms, and to engage kids in digital making.

Through community work like this I ended up getting hired by the Raspberry Pi Foundation to do development and outreach. We formed an Education team and started running teacher training programmes and other workshops. The Foundation’s getting bigger now as we try to reach more people around the world through our education programmes, and make more of an impact. I now work as Raspberry Pi Community Manager, supporting Jams and other community efforts including some great open source projects.

Continue reading PyDev of the Week: Ben Nuttall

A Simple Intro to Web Scraping with Python

Web scraping is where a programmer will write an application to download web pages and parse out specific information from them. Usually when you are scraping data you will need to make your application navigate the website programmatically. In this chapter, we will learn how to download files from the internet and parse them if need be. We will also learn how to create a simple spider that we can use to crawl a website.

Tips for Scraping

There are a few tips that we need to go over before we start scraping.

  • Always check the website’s terms and conditions before you scrape them. They usually have terms that limit how often you can scrape or what you can you scrape
  • Because your script will run much faster than a human can browse, make sure you don’t hammer their website with lots of requests. This may even be covered in the terms and conditions of the website.
  • You can get into legal trouble if you overload a website with your requests or you attempt to use it in a way that violates the terms and conditions you agreed to.
  • Websites change all the time, so your scraper will break some day. Know this: You will have to maintain your scraper if you want it to keep working.
  • Unfortunately the data you get from websites can be a mess. As with any data parsing activity, you will need to clean it up to make it useful to you.

With that out of the way, let’s start scraping!

Continue reading A Simple Intro to Web Scraping with Python

Python 3 Concurrency – The concurrent.futures Module

The concurrent.futures module was added in Python 3.2. According to the Python documentation it provides the developer with a high-level interface for asynchronously executing callables. Basically concurrent.futures is an abstraction layer on top of Python’s threading and multiprocessing modules that simplifies using them. However it should be noted that while the abstraction layer simplifies the usage of these modules, it also removes a lot of their flexibility, so if you need to do something custom, then this might not be the best module for you.

Concurrent.futures includes an abstract class called Executor. It cannot be used directly though, so you will need to use one of its two subclasses: ThreadPoolExecutor or ProcessPoolExecutor. As you’ve probably guessed, these two subclasses are mapped to Python’s threading and multiprocessing APIs respectively. Both of these subclasses will provide a pool that you can put threads or processes into.

The term future has a special meaning in computer science. It refers to a construct that can be used for synchronization when using concurrent programming techniques. The future is actually a way to describe the result of a process or thread before it has finished processing. I like to think of them as a pending result.

Continue reading Python 3 Concurrency – The concurrent.futures Module

Python 201: A multiprocessing tutorial

The multiprocessing module was added to Python in version 2.6. It was originally defined in PEP 371 by Jesse Noller and Richard Oudkerk. The multiprocessing module allows you to spawn processes in much that same manner than you can spawn threads with the threading module. The idea here is that because you are now spawning processes, you can avoid the Global Interpreter Lock (GIL) and take full advantages of multiple processors on a machine.

The multiprocessing package also includes some APIs that are not in the threading module at all. For example, there is a neat Pool class that you can use to parallelize executing a function across multiple inputs. We will be looking at Pool in a later section. We will start with the multiprocessing module’s Process class.

Getting started with multiprocessing

The Process class is very similar to the threading module’s Thread class. Let’s try creating a series of processes that call the same function and see how that works:

import os
from multiprocessing import Process
def doubler(number):
    A doubling function that can be used by a process
    result = number * 2
    proc = os.getpid()
    print('{0} doubled to {1} by process id: {2}'.format(
        number, result, proc))
if __name__ == '__main__':
    numbers = [5, 10, 15, 20, 25]
    procs = []
    for index, number in enumerate(numbers):
        proc = Process(target=doubler, args=(number,))
    for proc in procs:

For this example, we import Process and create a doubler function. Inside the function, we double the number that was passed in. We also use Python’s os module to get the current process’s ID (or pid). This will tell us which process is calling the function. Then in the block of code at the bottom, we create a series of Processes and start them. The very last loop just calls the join() method on each process, which tells Python to wait for the process to terminate. If you need to stop a process, you can call its terminate() method.

Continue reading Python 201: A multiprocessing tutorial

PyDev of the Week: Cory Benfield

This week we welcome Cory Benfield (@lukasaoz) as our PyDev of the Week! Cory is a core developer of the Python language, specifically on urllib3 as well as a core developer of the requests package. He is also the lead maintainer of the Hyper Project which is a set of related projects that provide HTTP/2 functionality to Python projects. Let’s spend some time getting to know Cory better!

Can you tell us a little about yourself (hobbies, education, etc):

I studied Physics at university in Scotland, originally with a bit of an eye towards ending up somewhere on the more practical end of professional physics: maybe doing something like medical physics. Before my degree, I’d never written a program in my life. However, I’d been a computer “power user” for a long time: I’d owned PCs and Macs and was comfortable in Linux as well as Windows and OS X, and I’ve been building PCs since I was 15. So when I found myself in a computational physics module staring at a Mathematica screen, something clicked in my brain and I realised that this was something I could be really happy doing. So I graduated with my masters degree in Physics and with a job offer to write software for the telecommunications industry.

When I’m not writing software, I like doing lots of different things to relax. I play video games (like most of the world now does), but I love watching great movies and TV shows. I also greatly enjoy cooking, reading, and spending time with people. Sometimes I even write: I’m not good at the discipline required to write regularly, but I think my writing is ok!

Continue reading PyDev of the Week: Cory Benfield

Python 201: A Tutorial on Threads

The threading module was first introduced in Python 1.5.2 as an enhancement of the low-level thread module. The threading module makes working with threads much easier and allows the program to run multiple operations at once.

Note that the threads in Python work best with I/O operations, such as downloading resources from the Internet or reading files and directories on your computer. If you need to do something that will be CPU intensive, then you will want to look at Python’s multiprocessing module instead. The reason for this is that Python has the Global Interpreter Lock (GIL) that basically makes all threads run inside of one master thread. Because of this, when you go to run multiple CPU intensive operations with threads, you may find that it actually runs slower. So we will be focusing on what threads do best: I/O operations!

Intro to Threads

A thread let’s you run a piece of long running code as if it were a separate program. It’s kind of like calling subprocess except that you are calling a function or class instead of a separate program. I always find it helpful to look at a concrete example. Let’s take a look at something that’s really simple:

import threading
def doubler(number):
    A function that can be used by a thread
    print(threading.currentThread().getName() + '\n')
    print(number * 2)
if __name__ == '__main__':
    for i in range(5):
        my_thread = threading.Thread(target=doubler, args=(i,))

Continue reading Python 201: A Tutorial on Threads