python cookbook 3rd code sample, just a copy