adding recipe 4.14 from python cookbook