Cache suitable regular expressions
authorPhilipp Hagemeister <phihag@phihag.de>
Wed, 21 Aug 2013 02:06:46 +0000 (04:06 +0200)
committerPhilipp Hagemeister <phihag@phihag.de>
Wed, 21 Aug 2013 02:06:48 +0000 (04:06 +0200)
This speeds up TestAllURLsMatching.test_no_duplicates by about 8000% at the cost of minimal memory overhead.

youtube_dl/extractor/common.py

index da50abfc1cd492b8d360ef601b44841a938c055b..8009c2d85708638509ce1539198ecbd6db1dbafa 100644 (file)
@@ -77,7 +77,13 @@ class InfoExtractor(object):
     @classmethod
     def suitable(cls, url):
         """Receives a URL and returns True if suitable for this IE."""
-        return re.match(cls._VALID_URL, url) is not None
+
+        # This does not use has/getattr intentionally - we want to know whether
+        # we have cached the regexp for *this* class, whereas getattr would also
+        # match the superclass
+        if '_VALID_URL_RE' not in cls.__dict__:
+            cls._VALID_URL_RE = re.compile(cls._VALID_URL)
+        return cls._VALID_URL_RE.match(url) is not None
 
     @classmethod
     def working(cls):