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

Python: Visualization with Bokeh

The Bokeh package is an interactive visualization library that uses web browsers for its presentation. Its goal is to provide graphics in the vein of D3.js that look elegant and are easy to construct. Bokeh supports large and streaming datasets. You will probably be using this library for creating plots / graphs. One of its primary competitors seems to be Plotly.

Note: This will not be an in-depth tutorial on the Bokeh library as the number of different graphs and visualizations it is capable of is quite large. Instead, the aim of the article is to give you a taste of what this interesting library can do.

Let’s take a moment and get it installed. The easiest way to do so is to use pip or conda. Here’s how you can use pip:

pip install bokeh

This will install Bokeh and all its dependencies. You may want to install Bokeh into a virtualenv because of this, but that’s up to you. Now let’s check out a simple example. Save the following code into a file with whatever name you deem appropriate.

Continue reading Python: Visualization with Bokeh

Python 3 – An Intro to asyncio

The asyncio module was added to Python in version 3.4 as a provisional package. What that means is that it is possible that asyncio receives backwards incompatible changes or could even be removed in a future release of Python. According to the documentation asyncio “provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives“. This chapter is not meant to cover everything you can do with asyncio, however you will learn how to use the module and why it is useful.

If you need something like asyncio in an older version of Python, then you might want to take a look at Twisted or gevent.


The asyncio module provides a framework that revolves around the event loop. An event loop basically waits for something to happen and then acts on the event. It is responsible for handling such things as I/O and system events. Asyncio actually has several loop implementations available to it. The module will default to the one most likely to be the most efficient for the operating system it is running under; however you can explicitly choose the event loop if you so desire. An event loop basically says “when event A happens, react with function B”.

Think of a server as it waits for someone to come along and ask for a resource, such as a web page. If the website isn’t very popular, the server will be idle for a long time. But when it does get a hit, then the server needs to react. This reaction is known as event handling. When a user loads the web page, the server will check for and call one or more event handlers. Once those event handlers are done, they need to give control back to the event loop. To do this in Python, asyncio uses coroutines.

A coroutine is a special function that can give up control to its caller without losing its state. A coroutine is a consumer and an extension of a generator. One of their big benefits over threads is that they don’t use very much memory to execute. Note that when you call a coroutine function, it doesn’t actually execute. Instead it will return a coroutine object that you can pass to the event loop to have it executed either immediately or later on.

One other term you will likely run across when you are using the asyncio module is future. A future is basically an object that represents the result of work that hasn’t completed. Your event loop can watch future objects and wait for them to finish. When a future finishes, it is set to done. Asyncio also supports locks and semaphores.

The last piece of information I want to mention is the Task. A Task is a wrapper for a coroutine and a subclass of Future. You can even schedule a Task using the event loop.

Continue reading Python 3 – An Intro to asyncio

PyDev of the Week: Nicholas Tollervey

This week we welcome Nicholas Tollervey (@ntoll) as our PyDev of the Week. He is the author of the Python in Education booklet and the co-author of Learning jQuery Deferreds: Taming Callback Hell with Deferreds and Promises. He was one of the co-founders of the London Python Code Dojo. You should check out his website to see what he’s up to. Let’s spend some time learning more about our fellow Pythonista!

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

I’m a UK based freelance programmer.

I first learned to program as a kid using a BBC micro. Like many UK programmers of a certain age I go all misty-eyed when remembering the sense of excitement, adventure and possibilities that such a machine generated.

As a teenager I had music lessons and ended up getting my undergraduate degree from the Royal College of Music in London. It was a lot of fun: among other things, my composition tutor used to teach Andrew Lloyd Webber and my keyboard harmony teacher was the Queen’s personal organist (he was director of the Chapel Royal). I played professionally for a while before embarking on a career teaching music. I also went on to read for degrees in philosophy and then computing ~ my twenties were all about
teaching and learning cool stuff!

When I started a family I decided I also needed a career change and remembered back to the fun I’d had on the BBC micro so reverted back to programming. Now I spend a lot of my free time making music.

Continue reading PyDev of the Week: Nicholas Tollervey

Python 201 Writing Update: Only 4 Chapters to go!

I finished up section #4 earlier this week which brings the book up to 26 chapters and a little over 200 pages. I have four more chapters planned and then a couple of updates to previous chapters. My goal is to have the book ready for proofing at the end of the month. Then I’ll create a sample print of the book and check it over for errors.

If anyone has been reading the book and found any errors, please let me know. I’ll be finalizing the chapters in mid-August or so and would like them to be as good as they can be before then.

Thanks so much for your support!

P.S. If you’d like to purchase the early version of the book, you can do at Gumroad or Leanpub

An Intro to is a 3rd party tool for Python that is used for measuring your code coverage. It was originally created by Ned Batchelder. The term “coverage” in programming circles is typically used to describe the effectiveness of your tests and how much of your code is actually covered by tests. You can use with Python 2.6 up to the current version of Python 3 as well as with PyPy.

pip install coverage

Now that we have installed, we need some code to use it with. Let’s create a module that we’ll call Here’s the code:

def add(a, b):
    return a + b
def subtract(a, b):
    return a - b
def multiply(a, b):
    return a * b
def divide(numerator, denominator):
    return float(numerator) / denominator

Now we need a test. Let’s create one that tests the add function. Let’s give our test the following name: Go ahead and save it in the same location as you did for the previous module. Then add the following code to our test:

import mymath
import unittest
class TestAdd(unittest.TestCase):
    Test the add function from the mymath library
    def test_add_integers(self):
        Test that the addition of two integers returns the correct total
        result = mymath.add(1, 2)
        self.assertEqual(result, 3)
    def test_add_floats(self):
        Test that the addition of two floats returns the correct result
        result = mymath.add(10.5, 2)
        self.assertEqual(result, 12.5)
    def test_add_strings(self):
        Test the addition of two strings returns the two string as one
        concatenated string
        result = mymath.add('abc', 'def')
        self.assertEqual(result, 'abcdef')
if __name__ == '__main__':

Continue reading An Intro to

Python 201: An Intro to mock

The unittest module now includes a mock submodule as of Python 3.3. It will allow you to replace portions of the system that you are testing with mock objects as well as make assertions about how they were used. A mock object is used for simulating system resources that aren’t available in your test environment. In other words, you will find times when you want to test some part of your code in isolation from the rest of it or you will need to test some code in isolation from outside services.

Note that if you have a version of Python prior to Python 3, you can download the Mock library and get the same functionality.

Let’s think about why you might want to use mock. One good example is if your application is tied to some kind of third party service, such as Twitter or Facebook. If your application’s test suite goes out and retweets a bunch of items or “likes” a bunch of posts every time its run, then that is probably undesirable behavior since it will be doing that every time the test is run. Another example might be if you had designed a tool for making updates to your database tables easier. Each time the test runs, it will do some updates on the same records every time and could wipe out valuable data.

Instead of doing any of those things, you can use unittest’s mock. It will allow you to mock and stub out those kinds of side-effects so you don’t have to worry about them. Instead of interacting with the third party resources, you will be running your test against a dummy API that matches those resources. The piece that you care about the most is that your application is calling the functions it’s supposed to. You probably don’t care as much if the API itself actually executes. Of course, there are times when you will want to do an end-to-end test that does actually execute the API, but those tests don’t need mocks!

Continue reading Python 201: An Intro to mock

PyDev of the Week: James Tauber

This week we welcome James Tauber (@jtauber) as our PyDev of the Week. James is the founder and CEO of Eldarion. He is also the creator and lead developer of Pinax. You can about his many projects on his website. Let’s take a few moment to get to know him better.

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

I’m an Australian but have spent most of the last 17 years working for tech companies in the Boston area. I was originally studying theoretical physics but switched majors to linguistics half-way through my undergraduate studies. I had planned to pursue an academic career but, by the mid 90s, the Web was really taking off and I had the opportunity to be at the forefront of Web technology and standards and that’s what eventually led to me moving to the US.

I’m now the founder and CEO of Eldarion, a web application development company focused on Python and Django.

I still maintain an involvement in linguistic scholarship, particularly in relation to Ancient Greek and the Greek of the New Testament, and am currently doing postgraduate study in Ancient Greek. I also compose music in a variety of styles, both pop and classical. I enjoy travel, food, and wine and recently passed my Level 1 with the Court of Master Sommeliers.

I’m somewhat obsessed with life-long learning and have completed over 20 MOOCs on astronomy, music, molecular biology, game design, and language teaching. Whenever I’m learning something new, one of my first questions is always: how would I implement this in Python.

Continue reading PyDev of the Week: James Tauber

Yet Another Python datetime Replacement: Pendulum

I stumbled across another new library that purports that it is better than Python’s datetime module. It is called Pendulum. Pendulum is heavily influenced by Carbon for PHP according to its documentation.

These libraries are always interesting, although I am not sure what makes this one better than Arrow or Delorean. There were some comments on Reddit between the creator of Pendulum where he stated that he found Arrow to behave strangely in certain circumstances.

Regardless, this article isn’t about comparing these competing libraries. It’s just to take a look at how Pendulum itself works. One of the primary purposes for these various libraries is to create a datetime module with a more “Pythonic” API and one that is timezone aware.

Let’s get it installed so we can check it out:

pip install pendulum

If you don’t like installing it directly to your main Python installation, feel free to install it in a virtualenv instead. Regardless of where you install it, you will see it install a couple of dependencies of its own such as tzlocal, python-translate, codegen, and polib.

Now that it is installed, let’s open up Python’s interpreter and give it a try:

>>> import pendulum
<pendulum [2016-07-13T10:39:22.788807+09:00]>
</pendulum><pendulum [2016-07-12T20:39:27.250320-05:00]>

Continue reading Yet Another Python datetime Replacement: Pendulum

ANN: The Python by Example Udemy Course

I am happy to announce my first Udemy course on the Python programming language. It is called Python by Example and is a re-branding of my Python 101 Screencast series. I had originally hoped to keep the name, Python 101 on Udemy, but someone else had already taken it by the time I got my course started on there. Regardless, the course is made with the first 35 videos from the Python 101 Screencast. I plan to add the rest later this year.


What You Get

At the time of writing, you will receive 35 lectures or 6.5 hours of content. I have also included chapters from Python 101 that correspond with each lecture.

How to Purchase

To purchase Python by Example, just go to the following link: The coupon code will give you 25% off.