http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170