tags:

views:

110

answers:

2

I dont understand why, when i run my code the 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.

+1  A: 

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

Jesus Ramos
i fixed the tabs. but still no mas.
garbagecollector
It actually doesn't care if you use double tabs in one block and single tabs in another. As long as the indention level doesn't change unexpectedly in the middle of a block, it's good.
James
It may not matter to the interpreter, but python *programmers* are much pickier about indentation.
bstpierre
+8  A: 

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.

Jeremy Brown