473,465 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1624
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,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.