Code from Dive into Python book