Implemented lazy regex compiling in Field classes to improve 'import mongoengine' performance

This commit is contained in:
Bastien Gérard
2018-06-12 20:59:12 +02:00
parent 02a557aa67
commit 3d45cdc339
5 changed files with 67 additions and 10 deletions

22
mongoengine/base/utils.py Normal file
View File

@@ -0,0 +1,22 @@
import re
class LazyRegexCompiler(object):
"""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, obj, objtype):
return self.compiled_regex
def __set__(self, instance, value):
raise AttributeError("Can not set attribute LazyRegexCompiler")