parsing python code
les
abidnego25 at yahoo.com
Thu Nov 14 14:29:18 EST 2002
Hi,
i would like to find out the function call tree of a python
code. i.e.
say i have a code like this:
def fun111():
..
def fun11():
fun111()
def fun1():
fun11()
fun12()
if __name__ =='__main__':
fun1()
then i would like to build a dictionary like this
d ={ 'root': '__name__',
'__name__': ['fun1']
'fun1': [ 'fun11','fun12'],
'fun11': [ 'fun111']
}
it is easy to parse out function names from the code,
but not so easy to jump around to figure the tree structure.
is there a module which exports some function when
given a functionname returns all the functions it calls?
(only those functions that are in the source code)
so far i have something like this
that removes the function names
but before i try the brute force way,i wanted to ask if there is already
a module that does what i want
######################################
from sys import argv,exit,stdin
if __name__ == '__main__':
try:
codefile = argv[1]
except IndexError:
print 'USAGE %s <file>' % argv[0]
exit(-1)
#pull out function names
fp = open(codefile)
fun_names=[]
for x in fp:
if x[0:3]=='def':
fun_names.append( x.split()[1].split('(')[0])
print fun_names
fp.close()
# go thought the file again and build nested list
nested={}
fp = open(codefile)
################ I NEED HELP HERE
fp.close()
thanks
More information about the Python-list
mailing list