I have a for loop which iterates through a text file (in this case actually a Python file) and it is trying to extract all the functions (looking for the word def). Once it finds that word it starts recording lines until it hits a blank space (which I’m using to denote the end of the function).
My problem is that I want to backup once I hit a def in the file and record any comments that might come before the function. Ex: # This function does the following... etc. I want to backup until I no longer hit a hash.
How would I look backwards with this loop I have written?
for (counter,line) in enumerate(visible_texts):
line= line.encode('utf-8')
# if line doesn't contain def then ignore it
if "def" in line and infunction== 0:
match = re.search(r'\def (\w+)', line.strip())
line = line.split ("def")[1]
print "Recording start of the function..."
# Backup to see if there's any hashes above it (until the end of the hashes) ** how do I do this **
An example of output I would want at the end would be:
# This function was created by Thomas
# This function print a pass string into the function
def printme( str ):
"This prints a passed string into this function"
print str
return
Don’t back up; record comments regardless, until you hit another line. If it’s not a
defline, discard the comments gathered: