summaryrefslogtreecommitdiff
path: root/util.py
diff options
context:
space:
mode:
authorMarcel Bargull <marcel.bargull@udo.edu>2018-03-16 19:51:00 +0100
committerMarcel Bargull <marcel.bargull@udo.edu>2018-03-16 19:51:00 +0100
commit9ece6742b14cc7773c048bef35711b5e060d80f6 (patch)
tree60f739c5f05b24b3e6efa50ca6ba14fe1c26e14a /util.py
parent0bda7fa8499b6c802d6246435524b839e9a8ba4c (diff)
downloadruamel.yaml-9ece6742b14cc7773c048bef35711b5e060d80f6.tar.gz
evaluate re.compile lazily
Diffstat (limited to 'util.py')
-rw-r--r--util.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/util.py b/util.py
index 9f939cf..077b264 100644
--- a/util.py
+++ b/util.py
@@ -6,6 +6,9 @@ some helper functions that might be generally useful
from __future__ import absolute_import, print_function
+from functools import partial
+import re
+
from .compat import text_type, binary_type
if False: # MYPY
@@ -13,6 +16,26 @@ if False: # MYPY
from .compat import StreamTextType # NOQA
+class LazyEval(object):
+ def __init__(self, func, *args, **kwargs):
+ def lazy_self():
+ return_value = func(*args, **kwargs)
+ object.__setattr__(self, "lazy_self", lambda: return_value)
+ return return_value
+ object.__setattr__(self, "lazy_self", lazy_self)
+
+ def __getattribute__(self, name):
+ lazy_self = object.__getattribute__(self, "lazy_self")
+ if name == "lazy_self":
+ return lazy_self
+ return getattr(lazy_self(), name)
+
+ def __setattr__(self, name, value):
+ setattr(self.lazy_self(), name, value)
+
+
+RegExp = partial(LazyEval, re.compile)
+
# originally as comment
# https://github.com/pre-commit/pre-commit/pull/211#issuecomment-186466605
# if you use this in your code, I suggest adding a test in your test suite