易截截图软件、单文件、免安装、纯绿色、仅160KB

取得Python代码段的正则表达式?

如:
def aa(i):
print i
a=[1,2,3]
b=2
c=3

能识别出起始位def aa(i):
和结束位" a=[1,2,3]"?
像Python对语法这么要求严格的我觉得就不要用正则了,效率不高。直接用判断就可以了,我写了个识别顶级函数的,抛砖引玉:
Python code:
s = r'''
def aa(i):
print i
a=[1,2,3]
b=2
c=3
def a():
print\
'w'
'''

def findFunc(s):
result = []
begin = False
def show(x): print '\n***Function***:\n', '\n'.join(x)

ss = s.split('\n')
for i,row in enumerate(ss):
if len(row)==0: continue
if begin:
if row.startswith(' '):
result.append(row)
elif ss[i-1].endswith('\\'):
result.append(row)
else:
show(result)
begin = False
result = []
if row.startswith('def '):
begin = True
result.append(row)
show(result)

findFunc(s)


我觉得应该用ast 才能把他正确解释出来

e.g.,

import ast

class V(ast.NodeVisitor):
def visit_FunctionDef(self, node):
print 'def %s' % (node.name)

s='''
def aa(i):
print i
a = [ 1, 2, 3]

b = 2
c = 3
'''

t = ast.parse(s)
V().visit(t)



相关问答:

eric4怎么运行python程序?

python和pyqt以及Eric4都已经配置好了,打开Eric4写个python程序比如简单的
print“hello,world”,怎么编译运行了,Start菜单的所有命令都出现和图中差不多的对话框,应当怎么运行了?

求大家指教

没有人用 ......

python urllib urllib2 httplib 访问url的问题

我想用python解析网页中的url 在下载之前, 我想先发一个head请求 不需要下载整个网页, 先获得该网站的head信息, 读取一些信息之后 在调用这些库的read方法来读取整个网页 该怎么操作呢 多谢
帮顶了

有人能指教一 ......

【求助】python传系统参数无法传入,急!

我在批处理中调用python脚本
如下:
call %pyscript%\StaticResult.py -s D:\DailySDV\Bts8.1\TestLog\TestResult.xml -d %CiRoot_WndPath%\cruisecontrol\userData\groups\group01\projects\%ProjectName%\%GtrRl ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号