1

Well. I figured it out. It turns out that the if does not run exactly when it's not supposed to match, and that's because _s3_bucket.list(prefix=basename) = []. My faith in python has been restored. :) Thank you all!

Why does this snippet of code not ever print "doesnt match"? When the regex matches, it successfully prints "matches" but when it doesn't, it doesn't execute the else. print type(match) returns <type 'list'>.

object_regex = re.compile('%s\.(\d+)\.%s' % \
    (re.escape('.'.join(basename.split('.')[:-2])),
     re.escape(basename.split('.')[-1])))
for obj in _s3_bucket.list(prefix=basename):
    match = object_regex.findall(obj.name)
    print match //prints nothing when nothing is found. not even []
    if match:
        print "matches"
    else:
        print "doesnt match"

I also tried

if not match:
    print "doesnt match"
else:
    print "matches"

and it never executes the if either.

Printout (UPLOAD TO S3 prints when the function is called):

UPLOAD TO S3 /fonts/HelveticaNeueLTPro-Lt.1351206175.otf
[u'1351206175']
matches
UPLOAD TO S3 /fonts/HelveticaNeueLTPro-Bd.1351206175.otf
[u'1351206175']
matches
UPLOAD TO S3 /css/common.1357015625.css      <-- what is going on here?
UPLOAD TO S3 /css/landing-style.1356896077.css
[u'1356896077']
matches

When I print type(match) instead of print match I get <type 'list'> every time, which leads me to believe that match indeed is an empty list. But empty lists are supposed to do this...

>>> match=[]
>>> if match:
...     print "ASDF"
... 
>>> if not match:
...     print "asdf"
... 
asdf
az_
  • 1,493
  • 1
  • 16
  • 24
  • 1
    Please print a handfull of the obj's. That will help us help you... – rh0dium Jan 01 '13 at 05:03
  • Agreed with @Thomas and Ignacio. You told us it doesn't execute the else side. Are you sure it "doesn't match"? – rh0dium Jan 01 '13 at 05:09
  • 1
    The implication is that it never executes the if _block_ (as opposed to the _else_ block) not the whole statement. If I am reading the question correctly, `if match` is always evaluating as true. – David H. Clements Jan 01 '13 at 05:10
  • 1
    @DavidH.Clements I reckon Ignacio is right - what we *see* is not actually what's *there* as it were... Oh - great surname by the way *cough* :) – Jon Clements Jan 01 '13 at 05:11
  • Added lots of stuff to the original question. @rh0dium David H. Clements I'm sure it doesn't match. Regardless of matching or not matching, there doesn't seem to be anything preventing the program to execute the if/else. – az_ Jan 01 '13 at 06:13
  • @DavidH.Clements The `if match` is not always evaluating as true, or else it would print "matches". But it's not evaluating as false either... – az_ Jan 01 '13 at 06:15
  • 2
    If you have found an answer to your own question, please supply it as an answer, not by editing the question. – SingleNegationElimination Jan 01 '13 at 06:24

3 Answers3

2

The return value of findall is a list. If the RE has groups then, according to the docs, it may include empty group entries for the non-matches. Since you are using groups it may return a list of empty tuples. But since the list is not empty it evaluates to True. Regardless.

To show this, print out the return value of findall. Printing stuff for debugging is common practice. That would be the first think I would do.

OR

your method _s3_bucket.list(prefix=basename) is not returning anything to iterate over and the entire loop block is skipped. What is the output of that?

BUT

We can't really tell since you dynamically construct your RE and we don't know what it really looks like or what it will match.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • Added to the original question. The first thing I did was print type and value, and it made me even more confused with this bug! haha – az_ Jan 01 '13 at 06:08
0

When there's no match, the function findall return []. The example as follows:

>>> import re
>>> res = re.findall('\d','ad34hsfg2')
>>> print res
['3', '4', '2']
>>> res = re.findall('\d','adhsfg')
>>> print res
[]
>>> print res == None
False
>>> print res == 0
False

so,now I think you should understand. Do I answer your question?

ytodayt
  • 3
  • 1
0

The actual issue was that _s3_bucket.list(prefix=basename) == [], when object_regex.findall(obj.name) was supposed to be []. Hence, the if/else never executed. I'm not sure why then, when I printed type, it still printed... but I guess I forgot to set that one to not match the regex.

Basically, if an if/else does not seem to work, it's control flow is probably getting cut off by something before it!

Thanks everyone! and... Happy New Year!

az_
  • 1,493
  • 1
  • 16
  • 24