Created
June 11, 2010 07:20
-
-
Save qingfeng/434161 to your computer and use it in GitHub Desktop.
PyCon
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
from trace import traced | |
class Tracer(type): | |
def __new__(cls, name, bases, cdict): | |
for n in cdict: | |
if callable(cdict[n]) \ | |
and not n.startswith("__"): | |
cdict[n] = traced(cdict[n]) | |
return type.__new__(cls, name, bases, cdict) | |
if __name__ == "__main__": | |
class MyClass(object): | |
__metaclass__ = Tracer | |
def method1(self, a): | |
print "Method 1:", a | |
def method2(self, a, b=10): | |
return "Method 2: %s" % (a*b) | |
mc = MyClass() | |
mc.method1("Hooray!") | |
print mc.method2("Easy", b=3) |
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
def traced(f): | |
def tracer(*args, **kw): | |
print ("** %s(%s)") % ( | |
f.__name__, | |
", ".join( | |
[repr(arg) for arg in args]+ | |
["%s=%s" % (k, repr(v)) | |
for (k, v) in kw.items()])) | |
return f(*args, **kw) | |
return tracer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment