Here's a nice use of any() with a generator expression:
def is_url_interesting(self, url):It's fast because any() shortcircuits the same way "or" does. It's way shorter than the comparable "for" loop. Last of all, I think it's very readable, at least if you're used to reading list comprehensions.
return any(regex.match(url) for regex in self.interesting_url_regexes)
Comments
any(re.match(regex, url) for regex in self.interesting_url_regexes)
regex = re.compile(regex_string)
since you would probably want to use the function more than once, it would make sense to pre-compile the regex and save time.
If you have >100 regex then it is definitely worth pre-compiling them. If you have less then you still save some time by precompiling since you do not need to do the cache lookup on each match.
The source for 2.5.2 is at http://svn.python.org/view/python/tags/r252/Lib/re.py?rev=60915&view=auto.
The relevant code is in the _compile function.
I know that that's true for certain regular expression engines, but are you sure it's true for Python's?
return any(map(regex.match, self.interesting_url_regexes))
- bram