How can I list function calls by using exec in python? -
i want use exec simple python code , list functions called instead of calling them.
if know functions called, can create dictionary defines named functions print
, use second argument of exec
.
i'm trying use custom dictionary class prints called functions overwritting getitem, exec
not helping issuing:
typeerror: exec: arg 2 must dictionary or none
is there way customize function call in generic way?
edit:
for instance, suppose have following configuration file, written in python:
user('foo') password('foo123') home('/home/foo') user('bar') password('bar123') home('/home/foo')
i need run file , print information contained therein. can following python program:
d = { 'user': print, 'password': print, 'home: 'print } execfile(filename, d, {})
the problem approach have initialize d
functions present in file. tried use custom dictionary did different on getitem, , got typeerror
above.
perhaps following?
class printer(dict): def __missing__(self, key): def wrapped(*args, **kwargs): print('{} called: args={}, kwargs={}'.format(key, args, kwargs)) return wrapped code = ''' foo() bar(1, 2, baz=3) ''' exec(code, printer())
output:
foo called: args=(), kwargs={} bar called: args=(1, 2), kwargs={'baz': 3}
Comments
Post a Comment