Created
June 15, 2011 17:43
-
-
Save anonymous/1027634 to your computer and use it in GitHub Desktop.
debugging context manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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