utils.py 625 B

12345678910111213141516171819202122
  1. import re
  2. class LazyRegexCompiler(object):
  3. """Descriptor to allow lazy compilation of regex"""
  4. def __init__(self, pattern, flags=0):
  5. self._pattern = pattern
  6. self._flags = flags
  7. self._compiled_regex = None
  8. @property
  9. def compiled_regex(self):
  10. if self._compiled_regex is None:
  11. self._compiled_regex = re.compile(self._pattern, self._flags)
  12. return self._compiled_regex
  13. def __get__(self, instance, owner):
  14. return self.compiled_regex
  15. def __set__(self, instance, value):
  16. raise AttributeError("Can not set attribute LazyRegexCompiler")