Skip to content

Instantly share code, notes, and snippets.

Created June 15, 2011 17:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1027634 to your computer and use it in GitHub Desktop.
Save anonymous/1027634 to your computer and use it in GitHub Desktop.
debugging context manager
import time, logging
class debug_watch:
def __init__(self, name, logger=logging.info):
self.name = name
self.logger = logger
def __enter__(self):
self.t = time.time()
self.logger("Starting %s" % self.name)
def __exit__(self, *args):
t = time.time() - self.t
self.logger("Finished %s in %.3fs" % (self.name, t))
with debug_watch('some operation', logging.warning):
print '"working"'
time.sleep(2)
"""
WARNING:root:Starting some operation
"working"
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