Issue 12 in f2py: How to extract block.content recursively?

3 views
Skip to first unread message

f2...@googlecode.com

unread,
Mar 24, 2010, 7:57:41 PM3/24/10
to f2py-...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 12 by TakaoKotani: How to extract block.content recursively?
http://code.google.com/p/f2py/issues/detail?id=12

Hi pearu,
I wanted to know how to extract instances recursively.
For the purpose, I made a function,

def toreprx(block, depth=-1, incrtab=''):
if depth==0 or not block.content:return
for c in block.content:
print 'xxxxxx ', depth, c.__class__.__name__
toreprx(c,depth-1,incrtab)
return

I think this should work when we supply parser.block to this routine.
But it gives strange answer. block.content of Comment is not None.
Why so? For me it is easy (e.g. to set up call-caller trees) if this works.

What I did is
1. Put takao_blocktest.py just above f2py/
2. do
> takao_blocktest.py takao_blocktest.F

takao


Attachments:
takao_blocktest.tar.gz 744 bytes

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

f2...@googlecode.com

unread,
Mar 25, 2010, 6:22:19 PM3/25/10
to f2py-...@googlegroups.com
Updates:
Status: Started

Comment #1 on issue 12 by pearu.peterson: How to extract block.content
recursively?
http://code.google.com/p/f2py/issues/detail?id=12

Note that not all Statement instances have content attribute,
and if they have, then the content attribute does not need to be a list
(in the Comment case the content attribute is a string).
The proper way to check if a statement has "substatements" is to
check if the statement is BeginStatement instance.

I have implemented a walk function that recursivly yields statements
and their depths from the block till given depth. See

fparser.api.walk.__doc__

for more information.

f2...@googlecode.com

unread,
Mar 26, 2010, 5:46:41 AM3/26/10
to f2py-...@googlegroups.com

Comment #2 on issue 12 by TakaoKotani: How to extract block.content
recursively?
http://code.google.com/p/f2py/issues/detail?id=12

Thank you for your suggestions. I understand what you said.

To make things easier, I think your fparser should include an API
which makes array of statement classes.
API should be together with some simple examples.
I now have def stackclass(block) below.
I hope your package will include something like this.

Then people do "f_arrayc.py *.F", and understand how it works.

takao

--------f_arrayc.py -----------
#!/usr/bin/env python
import sys,os
thisdir= os.path.dirname(os.path.abspath(__file__))
sys.path.append(thisdir+'/f2py/fparser')
from base_classes import EndStatement,classes

def stackclass(block):
arrayc=[]
toreprx(block,arrayc)
return arrayc

def toreprx(block,arrayc, depth=-1, incrtab=''):
if isinstance(block, classes.BeginStatement):
for c in block.content:
#print 'xxxxxx ', depth, c.__class__.__name__
depthx=depth
if isinstance(c, classes.EndStatement):
depthx=depth+1
arrayc.append([depthx,c])
toreprx(c,arrayc,depth-1,incrtab)
return

########## main #########
from api import parse
from readfortran import *
from parsefortran import FortranParser
#nargv = len(sys.argv) -1
argset= sys.argv[1:]
print argset
from inspect import *

for ffile in argset:
print '---- @@@@@ '+ffile+' @@@@@ start -----'
reader = FortranFileReader(ffile)
reader.set_mode(isfree=False,isstrict=False)
parser=FortranParser(reader,ignore_comments=False)
parser.parse()
parser.analyze()

arrayc = stackclass(parser.block)
for c in arrayc:
if(isinstance(c[1],classes.Comment)):
print c[0],c[1].__class__.__name__,c[1].item.comment,
c[1].item.span
else:
print c[0],c[1].__class__.__name__,c[1].item.line,
c[1].item.span
#print dir(c[1].item)

print '---- @@@@@ '+ffile+' @@@@@ end -----'
print
print
--------end of f_arrayc.py -----------

f2...@googlecode.com

unread,
Mar 26, 2010, 6:15:36 AM3/26/10
to f2py-...@googlegroups.com

Comment #3 on issue 12 by pearu.peterson: How to extract block.content
recursively?
http://code.google.com/p/f2py/issues/detail?id=12

fparser.api.walk is doing already this, try:

..
for c in fparser.api.walk(parser.block):
..

That is, list(fparser.api.walk(parser.block)) should be equal
to stackclass(parser.block).

Using walk function is more memory efficient.

f2...@googlecode.com

unread,
Mar 26, 2010, 8:16:45 AM3/26/10
to f2py-...@googlegroups.com

Comment #4 on issue 12 by TakaoKotani: How to extract block.content
recursively?
http://code.google.com/p/f2py/issues/detail?id=12

I see. I should have examined your code first.
Thank you.

f2...@googlecode.com

unread,
Mar 26, 2010, 4:02:47 PM3/26/10
to f2py-...@googlegroups.com
Updates:
Status: Fixed

Comment #5 on issue 12 by pearu.peterson: How to extract block.content
recursively?
http://code.google.com/p/f2py/issues/detail?id=12

(No comment was entered for this change.)

Reply all
Reply to author
Forward
0 new messages