# tracing.py import customdec customdec.enhance_classes("[Decorated]") class B(object): def __init__(self): "[tracedmethod]" super(B, self).__init__() class D(object): def __init__(self): "[tracedmethod]" super(D, self).__init__() class E(B, D): def __init__(self): "[tracedmethod]" super(E, self).__init__()
# example6.py "How to trace a class method" import customdec; customdec.enhance_classes() class C(object): "[Decorated]" def fact(cls,n): # a traced classmethod "[classmethod,tracedmethod]" if n==0: return 1 else: return n*cls.fact(n-1)
# chatty2.py import customdec; customdec.enhance_classes("[Decorated]") # sets the log files log1=file('file1.log','w') log2=file('file2.log','w') class C: def f(self): "[chattymethod2]" f.logfile=log1 # function attribute def g(self): "[chattymethod2]" g.logfile=log2 # function attribute assert C.__dict__['f'].logfile is log1 # check the conversion assert C.__dict__['g'].logfile is log2 # function attr. -> decorator attr. c=C() # C instantiation c.f() # print a message in file1.log c.g() # print a message in file2.log log1.close(); log2.close() # finally
# chatty2.py import customdec customdec.enhance_classes() # sets the log files log1 = file('file1.log', 'w') log2 = file('file2.log', 'w') class C: "[Decorated]" def f(self): "[chattymethod2]" f.logfile = log1 # function attribute def g(self): "[chattymethod2]" g.logfile = log2 # function attribute assert C.__dict__['f'].logfile is log1 # check the conversion assert C.__dict__['g'].logfile is log2 # function attr. -> decorator attr. c = C() # C instantiation c.f() # print a message in file1.log c.g() # print a message in file2.log
# logged.py import customdec customdec.enhance_classes() class D(object): # will print a message at D creation "[Logged]"