Skip to content

Instantly share code, notes, and snippets.

@lclarkmichalek
Forked from anonymous/fsfds.py
Created June 15, 2011 19:32
Show Gist options
  • Save lclarkmichalek/1027893 to your computer and use it in GitHub Desktop.
Save lclarkmichalek/1027893 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