tags:

views:

87

answers:

2

Not sure if I'm missing something obvious, but here's what is happening: I have a python 2.4.3 script that contains several RegEx objects. Below one of the regex objects is searching for all matches in a string (tMatchList). Even if tMatchList is not null, it is printing an empty set after the 'if p:' step. This behavior occurs even if it prints correctly before the 'if p:' step. I thought it may have been a scope issue, but everything is declared & contained within one function. I'm not quite seeing how the 'if p:' step is not able to see tMatchList. I am able to print tMatchList after the if statement as well.

tMatchList = []
for lines in r:
    linecount += 1

    tMatchList = self._testReplacePDFTag.findall(lines)

    p = self._pdfPathRegex.search(lines)
    print tMatchList   #tMatchList is printing just fine here if it has any elements
    if p:
        print tMatchList #now it's empty, 
                         #even if it printed elements in prior statement
        lines = .....
    else:
        <something else gets done>
    print tMatchList #now it prints again

Including entire function definition for those who would like to see it....

def FindFilesAndModifyPDFTag(self, inRootDirArg, inRollBackBool):
    for root, dirs, files in os.walk(inRootDirArg):
        for d in dirs: 
            if d.startswith('.'):#excludes directories that start with '.'
                continue
        for file in files:
            if os.path.splitext(file)[1] == self._fileExt:
                #Backup original. just do it
                shutil.copy2(os.path.join(root, file), os.path.join(root, file)+"~") 
                r = open(os.path.join(root, file)+"~", "r")
                f = open(os.path.join(root, file), "w")

                linecount = 0
                tMatchList = []
                for lines in r:
                    linecount += 1

                    tMatchList = self._testReplacePDFTag.findall(lines)
                    t = self._testReplacePDFTag.search(lines)

                    #find pdf path(s) in line                    
                    pMatchList = self._pdfPathRegex.findall(lines)
                    p = self._pdfPathRegex.search(lines)
                    #fix the pdf tracking code 
                    print id(tMatchList), "BEFORE"
                    if p:   
                        print id(tMatchList), "INSIDE"
                        lines = self.processPDFTagLine(pMatchList, lines, linecount, file, tMatchList)
                    else:
                        lines = self.processCheckMetaTag(lines, linecount, file)
                        #print id(tMatchList), "INSIDE ELSE"

                         print id(tMatchList), "AFTER"
                         f.writelines(lines)

                f.close()
                r.close()
                os.remove(os.path.join(root, file)+"~")

enter code here
A: 

The findall may not create a list object. If it is some kind of generator function, then it has a value which is "consumed" by traversing the results once.

After consuming the results yielded by this function, there are no more results.

tMatchList = self._testReplacePDFTag.findall(lines)

p = self._pdfPathRegex.search(lines)
print tMatchList   #tMatchList is printing just fine here if it has any elements
if p:
    print tMatchList #now it's empty, 

Try this.

tMatchList = list( self._testReplacePDFTag.findall(lines) )
S.Lott
That's very possible.Wouldn't that mean that the generator-object has a ____str____ method that also traverses the results?Edit: I don't think this is the case now, because re.findall always returns a list of strings.
jcao219
jonny's question states that tMatchList DOES still have elements, though, because it prints just fine after the `if` statement.
JAB
from the documentation:re.findall(pattern, string[, flags]): Return all non-overlapping matches of pattern in string, as a list of strings
jonny
A: 

So this is what ended up 'fixing' the issue: I moved the tMatchList findall() line to follow the search. Then I added an 'if t:' statement. Now I am seeing the content of tMatchList inside the 'if p:' statement when I print it out. So problem solved for now, but I'm thinking there is some sort of behavioral concern I'm not aware of concerning the re module(?) or evaluating an empty list object.

#original code ....
linecount += 1

#this is modified section
t = self._testReplacePDFTag.search(lines)
if t:
    tMatchList = self._testReplacePDFTag.findall(lines)

#end modified section

pMatchList = self._pdfPathRegex.findall(lines)
jonny
Another problem i discovered yesterday after all that was that the t value was only getting set once. If i tried resetting the list inside the 'for lines in r:' loop to fix this issue, it was always empty. I ended up blowing up this routine and starting from scratch. This will be the accepted answer to the specific problem above, but the general issue was not truly addressed in my solution(why the values from the list seemed to disappear going into the 'if p:' statement)
jonny