python -- for with an if statement
I dont understand why, when i run my code th开发者_如何转开发e for each loop under the if statement isn't run. Even when the number of found is greater than 0!
def findpattern(commit_msg):
pattern = re.compile("\w\w*-\d\d*")
group = pattern.finditer(commit_msg)
found = getIterLength(group)
print found
if found > 0:
issues = 0
for match in group:
print " print matched issues:"
auth = soap.login(jirauser,passwd)
print match.group(0)
getIssue(auth,match.group(0))
issues = issues + 1
else:
sys.exit("No issue patterns found.")
print "Retrieved issues: " + str(issues)
Any help would be appreciated, I have been banging my head on this for an hour.
Your getIterLength()
function is finding the length by exhausting the iterator returned by finditer()
. You would then need a new iterator instance for the for loop. Instead, I would restructure your code like this:
def findpattern(commit_msg):
pattern = re.compile("\w\w*-\d\d*")
group = pattern.finditer(commit_msg)
found = 0
issues = 0
for match in group:
print " print matched issues:"
auth = soap.login(jirauser,passwd)
print match.group(0)
getIssue(auth,match.group(0))
issues = issues + 1
found += 1
if found == 0:
sys.exit("No issue patterns found.")
print "Retrieved issues: " + str(issues)
OR, you could use the findall()
method instead of finditer()
to give you a list (which is an iterable, not an iterator) on which you can run len(group)
to get the size and then use it to iterate over in your for loop.
Check your code formatting it looks like under the for you have a double tab instead of a single tab, remember python is very picky about indentation
精彩评论