aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/func
diff options
context:
space:
mode:
authorrnefyodov <rnefyodov@yandex-team.ru>2022-02-10 16:47:17 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:17 +0300
commitc753751b693cf7c481c0292912e2b7536fa6d36a (patch)
tree9814fbd1c3effac9b8377c5d604b367b14e2db55 /library/python/func
parentc22320e8c4f3d7be38c504706f137034e91d31e6 (diff)
downloadydb-c753751b693cf7c481c0292912e2b7536fa6d36a.tar.gz
Restoring authorship annotation for <rnefyodov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/python/func')
-rw-r--r--library/python/func/__init__.py102
-rw-r--r--library/python/func/ut/test_func.py144
2 files changed, 123 insertions, 123 deletions
diff --git a/library/python/func/__init__.py b/library/python/func/__init__.py
index 5e20802bc7d..74243616355 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):
diff --git a/library/python/func/ut/test_func.py b/library/python/func/ut/test_func.py
index f3605bafcfd..3c4fad1a076 100644
--- a/library/python/func/ut/test_func.py
+++ b/library/python/func/ut/test_func.py
@@ -1,87 +1,87 @@
-import pytest
+import pytest
import threading
-
+
import library.python.func as func
-
-
-def test_map0():
+
+
+def test_map0():
assert None is func.map0(lambda x: x + 1, None)
- assert 3 == func.map0(lambda x: x + 1, 2)
+ assert 3 == func.map0(lambda x: x + 1, 2)
assert None is func.map0(len, None)
- assert 2 == func.map0(len, [1, 2])
-
-
-def test_single():
- assert 1 == func.single([1])
- with pytest.raises(Exception):
- assert 1 == func.single([])
- with pytest.raises(Exception):
- assert 1 == func.single([1, 2])
-
-
-def test_memoize():
- class Counter(object):
- @staticmethod
- def inc():
- Counter._qty = getattr(Counter, '_qty', 0) + 1
- return Counter._qty
-
+ assert 2 == func.map0(len, [1, 2])
+
+
+def test_single():
+ assert 1 == func.single([1])
+ with pytest.raises(Exception):
+ assert 1 == func.single([])
+ with pytest.raises(Exception):
+ assert 1 == func.single([1, 2])
+
+
+def test_memoize():
+ class Counter(object):
+ @staticmethod
+ def inc():
+ Counter._qty = getattr(Counter, '_qty', 0) + 1
+ return Counter._qty
+
@func.memoize()
- def t1(a):
- return a, Counter.inc()
-
+ def t1(a):
+ return a, Counter.inc()
+
@func.memoize()
- def t2(a):
- return a, Counter.inc()
-
+ def t2(a):
+ return a, Counter.inc()
+
@func.memoize()
- def t3(a):
- return a, Counter.inc()
-
+ def t3(a):
+ return a, Counter.inc()
+
@func.memoize()
- def t4(a):
- return a, Counter.inc()
-
+ def t4(a):
+ return a, Counter.inc()
+
@func.memoize()
- def t5(a, b, c):
- return a + b + c, Counter.inc()
-
+ def t5(a, b, c):
+ return a + b + c, Counter.inc()
+
@func.memoize()
- def t6():
- return Counter.inc()
-
+ def t6():
+ return Counter.inc()
+
@func.memoize(limit=2)
def t7(a, _b):
return a, Counter.inc()
- assert (1, 1) == t1(1)
- assert (1, 1) == t1(1)
- assert (2, 2) == t1(2)
- assert (2, 2) == t1(2)
-
- assert (1, 3) == t2(1)
- assert (1, 3) == t2(1)
- assert (2, 4) == t2(2)
- assert (2, 4) == t2(2)
-
- assert (1, 5) == t3(1)
- assert (1, 5) == t3(1)
- assert (2, 6) == t3(2)
- assert (2, 6) == t3(2)
-
- assert (1, 7) == t4(1)
- assert (1, 7) == t4(1)
- assert (2, 8) == t4(2)
- assert (2, 8) == t4(2)
-
- assert (6, 9) == t5(1, 2, 3)
- assert (6, 9) == t5(1, 2, 3)
- assert (7, 10) == t5(1, 2, 4)
- assert (7, 10) == t5(1, 2, 4)
-
- assert 11 == t6()
- assert 11 == t6()
-
+ assert (1, 1) == t1(1)
+ assert (1, 1) == t1(1)
+ assert (2, 2) == t1(2)
+ assert (2, 2) == t1(2)
+
+ assert (1, 3) == t2(1)
+ assert (1, 3) == t2(1)
+ assert (2, 4) == t2(2)
+ assert (2, 4) == t2(2)
+
+ assert (1, 5) == t3(1)
+ assert (1, 5) == t3(1)
+ assert (2, 6) == t3(2)
+ assert (2, 6) == t3(2)
+
+ assert (1, 7) == t4(1)
+ assert (1, 7) == t4(1)
+ assert (2, 8) == t4(2)
+ assert (2, 8) == t4(2)
+
+ assert (6, 9) == t5(1, 2, 3)
+ assert (6, 9) == t5(1, 2, 3)
+ assert (7, 10) == t5(1, 2, 4)
+ assert (7, 10) == t5(1, 2, 4)
+
+ assert 11 == t6()
+ assert 11 == t6()
+
assert (1, 12) == t7(1, None)
assert (2, 13) == t7(2, None)
assert (1, 12) == t7(1, None)
@@ -158,5 +158,5 @@ def test_memoize_thread_local():
th.join()
-if __name__ == '__main__':
- pytest.main([__file__])
+if __name__ == '__main__':
+ pytest.main([__file__])