Created
December 19, 2012 19:03
-
-
Save lorenzoriano/4339505 to your computer and use it in GitHub Desktop.
Print debug (taken from http://www.willmcgugan.com/blog/tech/2012/12/9/where-are-those-print-statements/)
This file contains hidden or 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 inspect | |
import sys | |
import os | |
class DebugPrint(object): | |
def __init__(self, f): | |
self.f = f | |
def write(self, text): | |
frame = inspect.currentframe() | |
filename = frame.f_back.f_code.co_filename.rsplit(os.sep, 1)[-1] | |
lineno = frame.f_back.f_lineno | |
prefix = "[%s:%s] " % (filename, lineno) | |
if text == os.linesep: | |
self.f.write(text) | |
else: | |
self.f.write(prefix + text) | |
if not isinstance(sys.stdout, DebugPrint): | |
sys.stdout = DebugPrint(sys.stdout) | |
#to use: | |
#>>> import debugprint | |
#>>> print "Hello" | |
#[<stdin>:1] Hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment