Pls. help me recreate shell trick
Daniel Dittmar
daniel.dittmar at sap.com
Mon Nov 26 09:17:28 EST 2001
> My aim is not only to
> provide display only, debug mode for external commands, but also for
> possibly disrupting Python statements. One example could be:
>
> import shutil
> shutil.copyfile(src,dest)
class TracingProxy:
trace = 0
execute = 1
def __init__ (self, callable):
self.callable = callable
def __call__ (self, *args):
if self.trace:
printableArgs = map (repr, args)
argString = string.join (printableArgs, ', ')
print self.callable.__name__, '(', argString, ') =>',
if self.execute:
result = apply (self.callable, args)
else:
result = 'SKIPPED'
if self.trace:
print result
return result
def __getattr__ (self, attrname):
"""get any attributes from proxied object"""
return getattr (self.callable, attrname)
Use as:
copyfile = TracingProxy (shutil.copyfile)
copyfile (src, dest)
You can switch the 'trace' and 'execute' flags either globally in class
TracingProxy or per callable object.
You can use another routine besides repr () to convert arguments to
strings - say to limit string sizes.
Exercise: create a class ModuleProxy which takes a module in its __init__
method and creates a TracableProxy for each module method.
Daniel
More information about the Python-list
mailing list