473,287 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Getting callfunc from ast code.

"""Hi, how can I extend the code shown below so that I can identify
any "CallFunc" in "func.code" and identify the value of "node" in
"CallFunc"? Thanks.

This is my code so far:
"""

""" Given a python file, this program prints out each function's name
in that file, the line number and the ast code of that function.

"""

import compiler
import compiler.ast

parse_file = compiler.parseFile("/home/glich/file.py")

count = 0

while count < (len(parse_file.node.nodes) - 1):

func = parse_file.node.nodes[count]

print "\nFunc name: " + str(func.name)
print "Func line number: " + str(func.lineno) + "\n"

print str(func.code) + "\n"

count += 1
"""file.py:
def fun1():

print "Hi"
hi = "1"

fun2()
fun4()

def fun2():

fun3()

def fun3():

fun4()

def fun4():
print "Hi"

fun1()

"""

Oct 28 '07 #1
2 1620
On 28 Okt, 19:09, Glich <Glich.Gl...@googlemail.comwrote:
"""Hi, how can I extend the code shown below so that I can identify
any "CallFunc" in "func.code" and identify the value of "node" in
"CallFunc"? Thanks.

This is my code so far:
"""
I tend to use isinstance to work out what kind of AST node I'm looking
at, although I often use the visitor infrastructure to avoid doing
even that.
""" Given a python file, this program prints out each function's name
in that file, the line number and the ast code of that function.

"""

import compiler
import compiler.ast

parse_file = compiler.parseFile("/home/glich/file.py")

count = 0

while count < (len(parse_file.node.nodes) - 1):

func = parse_file.node.nodes[count]
A quick style tip here: it's easier to iterate over the nodes, and if
you want a counter, use the enumerate built-in function. Together with
the above advice...

for count, func in enumerate(parse_file.node.nodes):
if isinstance(func, compiler.ast.CallFunc):
...
print "\nFunc name: " + str(func.name)
print "Func line number: " + str(func.lineno) + "\n"

print str(func.code) + "\n"
Take a look at the help text for enumerate to see how I arrived at the
above:

help(enumerate)

Paul

Oct 28 '07 #2
Your help is very useful. I would not be able to progress without you!
Thanks.

Oct 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
5
by: Yoshitha | last post by:
Hi I am working on a web project. I have a InstallerClass in my project. While making setup ( using web setup template) for this web application, I have added a userinterface with 4...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
5
by: Manjiri | last post by:
Hello Everybody... Here i have the program which prints how many number of times the element appears in the array.... The code is as follows... #include<iostream.h> class Count
0
by: hydroxide8 | last post by:
hey im using the cx_Oracle module and i want to run a function that return obejct of VARCHAR2 type, and i dont know how to do that with the "callfunc" function.. i was try this : result =...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.