在函数内查找特定的变量并将它们返回排序 [英] Find specific variables inside a function and return them sorted
问题描述
我正在使用Python,我试图搜索一个.py文件,其所有函数都以名称test_开头,并包含所有变量。我搜索的变量格式如下:var [blabla]。所以在这里我有一个例子:
def test_123:
init = var [blabla1]
init2 = var [blabla2]
* somecode *
def test_456:
init3 = var [blabla3]
init4 = var [blabla4 ]
* somecode *
我已经写了一个脚本,函数和变量在html文件中。但是我必须让它们排序,所以我可以更好地处理它们。
现在就是这样:
test_123,test456
var [blabla1],var [blabla2],...
我想要这样:
test_123:
var [blabla1]
var [blabla2]
test_456:
var [blabla3]
var [blabla4]
编辑:我现在有这个权利:
def suchentpar():
fobj = open(2.py,r)
search = fobj.read )
tpar = re.findall(r'var \ [\\w + \\]',搜索)
返回tpar
fobj.close()
def suchenseq():
fobj = open(2.py,r)
search = fobj.read()
seq = re.findall r'test \_\w +',搜索)
返回seq
fobj.close()
fobj.close()
,因为函数将通过返回
上面的行... 然后,一种获得你想要的东西的方法是: import re
fcontent ='''
def test_a(self):
var [hello ]
var [world]
def test_b(self):
var [hola]
var [mundo]
' ''
dict_ = {}
chunks = [块为fcontent.split中的块('def')if chunk.strip()]
为块中的块:
tname = re.findall(r'test \_\w +',chunk)[0]
vars = re.findall(r'var \ [\\w + \ \]',chunk)
dict_ [tname] = vars
for k,v in dict_.items():
打印k
for v in:v
打印\ t%s%e
注意:中上面的代码在你写下它们的时候离开了正则表达式,但是你当然可以改进它们,并且可以根据需要更改 re.search
中的第一个 re.findall
。换句话说,上面的内容纯粹是一个展示概念的演示,但是您应该研究边缘案例和效率......
HTH!
first of all, thank you for your help in forward. I'm using Python and I'm trying to search a .py file for all of its functions starting with the name "test_" and all of the variables included. The variables I search for are formatted like this: "var["blabla"]". So here I have an example:
def test_123:
init = var["blabla1"]
init2 = var["blabla2"]
*somecode*
def test_456:
init3 = var["blabla3"]
init4 = var["blabla4"]
*somecode*
What I already wrote is a script, that returns all my functions and variables in a html file. But I have to get them sorted, so I can work with them better.
Right now its like this:
test_123,test456
var["blabla1"],var["blabla2"],...
And I want it like this:
test_123:
var["blabla1"]
var["blabla2"]
test_456:
var["blabla3"]
var["blabla4"]
EDIT: I have this right now:
def suchentpar():
fobj = open("2.py", "r")
search = fobj.read()
tpar = re.findall(r'var\[\"\w+\"\]',search)
return tpar
fobj.close()
def suchenseq():
fobj = open("2.py", "r")
search = fobj.read()
seq = re.findall(r'test\_\w+',search)
return seq
fobj.close()
First off, your code as is will never run fobj.close()
, given that the functions will exit via return
the line above...
Then, a way to obtain what you want could be:
import re
fcontent = '''
def test_a(self):
var["hello"]
var["world"]
def test_b(self):
var["hola"]
var["mundo"]
'''
dict_ = {}
chunks = [chunk for chunk in fcontent.split('def ') if chunk.strip()]
for chunk in chunks:
tname = re.findall(r'test\_\w+', chunk)[0]
vars = re.findall(r'var\[\"\w+\"\]', chunk)
dict_[tname] = vars
for k, v in dict_.items():
print k
for e in v:
print "\t%s" % e
NOTE: In the above code I left the regexes as you wrote them, but of course you could improve on them, and could change the first re.findall
in re.search
if you wished. In other words: what above is purely a demo to show the concept, but you should work on the edge cases and efficiency...
HTH!
这篇关于在函数内查找特定的变量并将它们返回排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!