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