code from the book of python cookbook