Skip to content

Instantly share code, notes, and snippets.

@qingfeng
Created June 11, 2010 07:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qingfeng/434161 to your computer and use it in GitHub Desktop.
Save qingfeng/434161 to your computer and use it in GitHub Desktop.
PyCon
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)
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