Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parsing python code

Reply
Thread Tools

parsing python code

 
 
Ryan Krauss
Guest
Posts: n/a
 
      12-05-2007
I need to parse a Python file by breaking it into blocks matching
indentation levels so that function definitions, for loops, and
classes are kept together as blocks. For example, if I have something
like

from scipy import*
from pylab import*

g = .6
Input_freq = 10.0

def load_data(path):
data = loadtxt(path, skiprows = 1)
t = data[:,0]
Input = data[:,1]
Output = data[:,2]
return t,Input,Output

def time_plot(x,y,n = 1):
figure(n)
clf()
for curx, cury in zip(x,y):
plot(curx,cury)
title('Time Plot')
xlabel('time')
ylabel('f(t)')
legend(['Input','Output'],1)
return figure(n)

t, Input, Output = load_data('system_data.txt')



I would like the blocks to be

block1 = ['from scipy import*', 'from pylab import*', 'g =
..6','Input_freq = 10.0']

block2 = ['def load_data(path):',
' data = loadtxt(path, skiprows = 1)',
' t = data[:,0]',
' Input = data[:,1]',
' Output = data[:,2]',
' return t,Input,Output']

and so on.

I think the parser module should enable me to do this, but I can't
seem to figure it out. Specifically, I think I need to use
parser.sequence2ast, but it doesn't work the way I think it should and
I can't find more documentation on it or an example that uses it.

I tried

f = open('Example.py','r')
mylines = f.readlines()
parser.sequence2ast(mylines)

but got a ParserError.

Is there an easy way to do what I need?

Thanks,

Ryan
 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      12-06-2007
On Dec 5, 6:02 pm, "Ryan Krauss" <ryanli...@gmail.com> wrote:
> I need to parse a Python file by breaking it into blocks matching
> indentation levels so that function definitions, for loops, and
> classes are kept together as blocks. For example, if I have something
> like
>
> from scipy import*
> from pylab import*
>
> g = .6
> Input_freq = 10.0
>
> def load_data(path):
> data = loadtxt(path, skiprows = 1)
> t = data[:,0]
> Input = data[:,1]
> Output = data[:,2]
> return t,Input,Output
>
> def time_plot(x,y,n = 1):
> figure(n)
> clf()
> for curx, cury in zip(x,y):
> plot(curx,cury)
> title('Time Plot')
> xlabel('time')
> ylabel('f(t)')
> legend(['Input','Output'],1)
> return figure(n)
>
> t, Input, Output = load_data('system_data.txt')
>
> I would like the blocks to be
>
> block1 = ['from scipy import*', 'from pylab import*', 'g =
> .6','Input_freq = 10.0']
>
> block2 = ['def load_data(path):',
> ' data = loadtxt(path, skiprows = 1)',
> ' t = data[:,0]',
> ' Input = data[:,1]',
> ' Output = data[:,2]',
> ' return t,Input,Output']
>
> and so on.
>
> I think the parser module should enable me to do this, but I can't
> seem to figure it out. Specifically, I think I need to use
> parser.sequence2ast, but it doesn't work the way I think it should and
> I can't find more documentation on it or an example that uses it.
>
> I tried
>
> f = open('Example.py','r')
> mylines = f.readlines()
> parser.sequence2ast(mylines)
>
> but got a ParserError.
>
> Is there an easy way to do what I need?
>
> Thanks,
>
> Ryan


You have to use parser.suite:

f_content = open('Example.py').read()
_ast = parser.suite(f_content)

You can display the _ast object as a nested list:

_lst = _ast.tolist()

This list can be converted back to an ast object using sequence2ast.

Another option is to use parser.expr instead of parser.suite but this
only applies for Python expressions but not statements. So
parser.expr / parser.suite are like eval / exec.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
Parsing Python code with a Python Program pgw Python 3 10-22-2004 09:18 AM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57