parsing python code

logistix logistix at zworg.com
Thu Nov 14 22:29:34 EST 2002


abidnego25 at yahoo.com (les) wrote in message news:<653cf871.0211141129.681e5dd7 at posting.google.com>...
> 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

The parser module builds Abstract Syntax Trees from text. 
Unfortunately, you'll still need to figure out how to climb through
the trees.  Tokenize module will give you one token at a time if you
want to build your own tree.  Again, it's not automagical, but you can
do alot of stuff.

compiler module might also be of interest, but I've never used it. 
And it seems pychecker just looks at the generated bytecode to analyze
code.

So it looks like there are alot of options, all of which will require
some work to get to where you're at.



More information about the Python-list mailing list