diff options
author | rnefyodov <rnefyodov@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
commit | c22320e8c4f3d7be38c504706f137034e91d31e6 (patch) | |
tree | 35cc6c382a1bd8cb932449ffa734056fcc73e6d4 /library/python/func/__init__.py | |
parent | 93e9e4639b6ee2afbdf45cf3927cea6d340e19b0 (diff) | |
download | ydb-c22320e8c4f3d7be38c504706f137034e91d31e6.tar.gz |
Restoring authorship annotation for <rnefyodov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/python/func/__init__.py')
-rw-r--r-- | library/python/func/__init__.py | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/library/python/func/__init__.py b/library/python/func/__init__.py index 7424361635..5e20802bc7 100644 --- a/library/python/func/__init__.py +++ b/library/python/func/__init__.py @@ -1,49 +1,49 @@ -import functools +import functools import threading import collections - - -def map0(func, value): - return func(value) if value is not None else value - - -def single(x): - if len(x) != 1: - raise Exception('Length of {} is not equal to 1'.format(x)) - return x[0] - - -class _Result(object): - pass - - -def lazy(func): - result = _Result() - + + +def map0(func, value): + return func(value) if value is not None else value + + +def single(x): + if len(x) != 1: + raise Exception('Length of {} is not equal to 1'.format(x)) + return x[0] + + +class _Result(object): + pass + + +def lazy(func): + result = _Result() + @functools.wraps(func) def wrapper(*args): - try: - return result.result - except AttributeError: + try: + return result.result + except AttributeError: result.result = func(*args) - - return result.result - - return wrapper - - -def lazy_property(fn): - attr_name = '_lazy_' + fn.__name__ - - @property - def _lazy_property(self): - if not hasattr(self, attr_name): - setattr(self, attr_name, fn(self)) - return getattr(self, attr_name) - - return _lazy_property - - + + return result.result + + return wrapper + + +def lazy_property(fn): + attr_name = '_lazy_' + fn.__name__ + + @property + def _lazy_property(self): + if not hasattr(self, attr_name): + setattr(self, attr_name, fn(self)) + return getattr(self, attr_name) + + return _lazy_property + + class classproperty(object): def __init__(self, func): self.func = func @@ -67,7 +67,7 @@ class lazy_classproperty(object): def memoize(limit=0, thread_local=False): assert limit >= 0 - def decorator(func): + def decorator(func): memory = {} lock = threading.Lock() @@ -86,7 +86,7 @@ def memoize(limit=0, thread_local=False): if len(keys) > limit: del memory[keys.popleft()] return memory[args] - + else: def get(args): @@ -112,15 +112,15 @@ def memoize(limit=0, thread_local=False): return wrapper - return decorator - - -# XXX: add test -def compose(*functions): - def compose2(f, g): - return lambda x: f(g(x)) + return decorator + + +# XXX: add test +def compose(*functions): + def compose2(f, g): + return lambda x: f(g(x)) - return functools.reduce(compose2, functions, lambda x: x) + return functools.reduce(compose2, functions, lambda x: x) class Singleton(type): |