File: utils.py

package info (click to toggle)
python-mongoengine 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 7,194; makefile: 57; sh: 17
file content (22 lines) | stat: -rw-r--r-- 617 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import re


class LazyRegexCompiler:
    """Descriptor to allow lazy compilation of regex"""

    def __init__(self, pattern, flags=0):
        self._pattern = pattern
        self._flags = flags
        self._compiled_regex = None

    @property
    def compiled_regex(self):
        if self._compiled_regex is None:
            self._compiled_regex = re.compile(self._pattern, self._flags)
        return self._compiled_regex

    def __get__(self, instance, owner):
        return self.compiled_regex

    def __set__(self, instance, value):
        raise AttributeError("Can not set attribute LazyRegexCompiler")