Implementations from python cookbook by David Beazley and Brian Jones