-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
178 lines (120 loc) · 3.46 KB
/
base.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# coding=utf-8
# some helper functions
# C19<caoyijun2050@gmail.com>
import time
import pdb
from collections import OrderedDict, Iterable
import xxhash as _xxhash
xxhash = lambda x: _xxhash.xxh64(ensure_utf8(x)).hexdigest()
class LimitedSizeDict(OrderedDict):
def __init__(self, *args, **kwds):
self.size_limit = kwds.pop("size_limit", None)
OrderedDict.__init__(self, *args, **kwds)
self._check_size_limit()
def __setitem__(self, key, value):
OrderedDict.__setitem__(self, key, value)
self._check_size_limit()
def _check_size_limit(self):
if self.size_limit is not None:
while len(self) > self.size_limit:
self.popitem(last=False)
def none_on_error(func, *arg, **kwarg):
try:
return func(*arg, **kwarg)
except Exception as e:
print(e)
return None
def ensure_unicode(string):
return string.decode('utf-8') if isinstance(string, str) else string
unzip = lambda x: zip(*x)
def flatten(items, ignore_types=(str, bytes)):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
for elem in flatten(x):
yield elem
else:
yield x
def chain(*funcs):
def wrap(*arg, **kwarg):
result = funcs[0](*arg, **kwarg)
for func in funcs[1:]:
result = func(result)
return result
return wrap
def do(*arg):
param, actions = arg[0], arg[1:]
for action in actions:
param = action(param)
return param
def curry_filter(func):
return lambda x: filter(func, x)
def curry_map(func):
return lambda x: map(func, x)
def breakpoint(arg):
pdb.set_trace()
return arg
def lazydo(*arg):
def func():
param, actions = arg[0], arg[1:]
for action in actions:
param = action(param)
return param
return func
def update(dict1, dict2):
dict1.update(dict2)
return dict1
# cache part
def clear_cache(func):
if 'cached' in func.__dict__:
del func.cached
print("{0}'s cache cleared.".format(func.__name__))
else:
print("{0} has no cache.".format(func.__name__))
def clear_all_cache(funcs):
map(clear_cache, funcs)
def cache(func):
cache.cached_funcs.add(func)
def wrap(*arg, **kwarg):
if 'cached' in func.__dict__:
return func.cached
else:
func.cached = func(*arg, **kwarg)
return func.cached
return wrap
cache.cached_funcs = set()
cache.clear_all = lambda: clear_all_cache(cache.cached_funcs)
# cache part end
def tick(arg):
if tick.t1:
t = time.time()
print("{0} s".format(t - tick.t1))
else:
print("begin")
tick.t1 = time.time()
return arg
tick.t1 = None
def tack(arg):
tick.t1 = time.time()
print("begin")
return arg
def iprint(*arg, **kwarg):
def wrap(onearg):
print(arg)
return onearg
return wrap
def ensure_unicode(string):
return string.decode('utf-8') if isinstance(string, str) else string
def ensure_utf8(string):
return string.encode('utf-8') if isinstance(string, unicode) else string
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj.__repr__()
class Constant:
def __init__(self, n):
self.n = n;
def __iter__(self):
return self
def next(self):
return self.n
one = Constant(1)
zero = Constant(0)