Skip to content

Instantly share code, notes, and snippets.


Created Jun 15, 2011
What would you like to do?
debugging context manager
import time, logging
class debug_watch:
def __init__(self, name, = name
self.logger = logger
def __enter__(self):
self.t = time.time()
self.logger("Starting %s" %
def __exit__(self, *args):
t = time.time() - self.t
self.logger("Finished %s in %.3fs" % (, t))
with debug_watch('some operation', logging.warning):
print '"working"'
WARNING:root:Starting some operation
WARNING:root:Finished some operation in 2.001s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.