Compile regular expressions on first use This class allows one to store regular expressions and compiles them on first use.
| 27 | |
| 28 | |
| 29 | class LazyReCompile: |
| 30 | """Compile regular expressions on first use |
| 31 | |
| 32 | This class allows one to store regular expressions and compiles them on |
| 33 | first use.""" |
| 34 | |
| 35 | def __init__(self, regex: str, flags: int = 0) -> None: |
| 36 | self.regex = regex |
| 37 | self.flags = flags |
| 38 | |
| 39 | @cached_property |
| 40 | def compiled(self) -> Pattern[str]: |
| 41 | return re.compile(self.regex, self.flags) |
| 42 | |
| 43 | def finditer(self, *args, **kwargs) -> Iterator[Match[str]]: |
| 44 | return self.compiled.finditer(*args, **kwargs) |
| 45 | |
| 46 | def search(self, *args, **kwargs) -> Match[str] | None: |
| 47 | return self.compiled.search(*args, **kwargs) |
| 48 | |
| 49 | def match(self, *args, **kwargs) -> Match[str] | None: |
| 50 | return self.compiled.match(*args, **kwargs) |
| 51 | |
| 52 | def sub(self, *args, **kwargs) -> str: |
| 53 | return self.compiled.sub(*args, **kwargs) |
no outgoing calls
no test coverage detected