Skip to content

Instantly share code, notes, and snippets.

/fsfds.py
Created Jun 15, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.