我不明白为什么,当我运行我的代码时,if语句下的每个循环都没有运行.即使找到的数量大于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)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,我一直在敲打我一小时.
你的getIterLength()
函数是通过耗尽返回的迭代器来查找长度finditer()
.然后,您需要for循环的新迭代器实例.相反,我会像这样重构您的代码:
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)
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用该findall()
方法而不是为finditer()
您提供一个列表(这是一个可迭代的,而不是迭代器),您可以在该列表上运行len(group)
以获取大小,然后使用它在for循环中进行迭代.