Parsing functions(?)
Paul M
paul.m at yale.edu
Thu Dec 9 15:32:01 EST 1999
Dear Pythoneers,
I'd like to write a "function recorder" class, something like this:
class frecorder:
def __init__(self):
self.flist = []
def record(self, fxncall):
fxn, arg = **UNKNOWN**(fxncall)
self.flist.append((fxn,arg))
Object of this hypothetical class could then be used to build up a
record of function calls and arguments which could then be applied all
at one time, something like the following:
>>> rec = frecorder()
>>> rec.record(foo(1))
>>> rec.record(bar('string', (t1,t2))
>>> rec.flist
[(function foo at XXXX, (1,)), (function bar at XXXX, ('string',
(t1,t2))]
>>> for i in rec.flist:
apply(i[0], i[1])
etc....
I know I could instead define the record method like this:
def record(self, fname, fargs):
self.flist.append((fname, fargs))
which would be called like:
rec.record(foo, (1,))
but it doesn't seem as natural as the first example, and besides it
means that one has to remember to do thinks like specify 1-tuples when
the function only takes a single argument.
Is this doable without parsing the command-line (or the *.py files -
I'd like to do this from within a module hierarchy I'm building)?
Thanks
Paul
More information about the Python-list
mailing list