forked from catboost/catboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
140 lines (102 loc) · 3.13 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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()
@functools.wraps(func)
def wrapper(*args):
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
class classproperty(object):
def __init__(self, func):
self.func = func
def __get__(self, _, owner):
return self.func(owner)
class lazy_classproperty(object):
def __init__(self, func):
self.func = func
def __get__(self, _, owner):
attr_name = '_lazy_' + self.func.__name__
if not hasattr(owner, attr_name):
setattr(owner, attr_name, self.func(owner))
return getattr(owner, attr_name)
def memoize(thread_safe=False, limit=0):
assert limit >= 0
def decorator(func):
@functools.wraps(func)
def wrapper_with_memory(memory, lock, keys):
# remove branching for options
if limit:
def get(args):
if args not in memory:
memory[args] = func(*args)
keys.append(args)
if len(keys) > limit:
del memory[keys.popleft()]
return memory[args]
else:
def get(args):
if args not in memory:
memory[args] = func(*args)
return memory[args]
if thread_safe:
def wrapper(*args):
with lock:
return get(args)
else:
def wrapper(*args):
return get(args)
return wrapper
return wrapper_with_memory({}, threading.Lock() if thread_safe else None, collections.deque() if limit else None)
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)
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
def stable_uniq(it):
seen = set()
res = []
for e in it:
if e not in seen:
res.append(e)
seen.add(e)
return res
def first(it):
for d in it:
if d:
return d
def split(data, func):
l, r = [], []
for e in data:
if func(e):
l.append(e)
else:
r.append(e)
return l, r