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 viareturnthe line above…Then, a way to obtain what you want could be:
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.findallinre.searchif 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!