-
Notifications
You must be signed in to change notification settings - Fork 0
/
_func.py
225 lines (146 loc) · 4.61 KB
/
_func.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from schema import Schema, SchemaError
from itertools import izip, repeat
from functools import partial
import inspect
from collections import namedtuple
functype = 'FUNCTYPE'
def fzip(funcs, args): #Embedded file name: func.py
for func, arg in izip(funcs, args):
yield func(arg)
def _and(*x):
#Embedded file name: func.py
return reduce(_and2, x)
def _and2(f, g):
#Embedded file name: func.py
return lambda x: f(x) and g(x)
def _id(x):
#Embedded file name: func.py
return x
def _not(f):
#Embedded file name: func.py
return lambda x: not f(x)
def _or(f, g):
#Embedded file name: func.py
return lambda x: f(x) or g(x)
def apply_each(funcs, arg):
#Embedded file name: func.py
return fzip(funcs, repeat(arg))
def apply_key_func(k, v, funcdict):
#Embedded file name: func.py
return funcdict.get(k, _id)(v)
def _bnot(x):
#Embedded file name: func.py
return ~x
#def <lambda>(x):
# #Embedded file name: func.py
# if x:
# return 1
# return 0
message = "Function {0} argument {1} expected type {2} but was type {3}.".format
def check_type(func, _type, obj):
#Embedded file name: func.py
assert isinstance(obj, _type), message(func.__name__, obj, _type, type(obj))
def cmp2(f, g):
#Embedded file name: func.py
return lambda a, b: (f(a), g(b))
def compose2(outer, inner):
#Embedded file name: func.py
def newfunc(*args, **kwargs):
return outer(inner(*args, **kwargs))
return newfunc
def compose_all(*funcs):
return reduce(compose, funcs)
compose = compose_all
def dict_intersect(d1, d2):
#Embedded file name: func.py
return {x:d1[x] for x in d1 if x in d2}
def dictmap(func, _dict):
#Embedded file name: func.py
return dict(((key, func(val)) for key, val in _dict.items()))
def flatten_list(a):
#Embedded file name: func.py
if type(a) != list:
return a
return a[0]
def get_funcs():
#Embedded file name: func.py
return inspect.getmembers(sys.modules[__name__], predicate=lambda f: inspect.isfunction(f) and f.__module__ == __name__)
def handle_check(etype, arg, kwargs, func):
#Embedded file name: func.py
if type(etype) is dict:
scheme, matched_args = dict_intersect(etype, kwargs), dict_intersect(kwargs, etype)
try:
Schema(scheme).validate(matched_args)
except SchemaError as e:
print 'scheme {0} did not fit kwargs {1}'.format(scheme, kwargs)
raise e
else:
if arg is None or etype is None:
return
if etype == functype:
assert hasattr(arg, '__call__')
else:
check_type(func, etype, arg)
def ilen(iterable):
return sum((1 for _ in iterable))
def is_local(object):
return isinstance(object, types.FunctionType) and object.__module__ == __name__
def iter_until_stop(f, *args, **kwargs):
#Embedded file name: func.py
while True:
try:
yield f(*args, **kwargs)
except StopIteration:
break
def kstarcompose(*funcs):
#Embedded file name: func.py
return reduce(kstarcompose2, funcs)
def kstarcompose2(f, g):
#Embedded file name: func.py
return lambda x: f(**g(x))
def merge_dicts(*dict_args):
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
def nameddict(Name, _dict):
names, values = unzip(_dict.items())
return namedtuple(Name, names)(*values)
def partial2(method, param):
def t(x):
return method(x, param)
return t
def partition(pred, iterable):
t1, t2 = it.tee(iterable)
return (it.ifilterfalse(pred, t1), it.ifilter(pred, t2))
def pjoin(x):
#Embedded file name: func.py
return partial(string.join, sep=x)
def psplit(x):
#Embedded file name: func.py
return partial(string.split, sep=x)
def pstrip(x):
#Embedded file name: func.py
return partial(string.strip, chars=x)
def reverse(collection):
return collection[::-1]
def rpartial(func, *args):
return lambda *a: func(*(a + args))
def split_list(A, idx):
#Embedded file name: func.py
return (A[:idx], A[idx:])
def starcompose(*funcs):
#Embedded file name: func.py
return reduce(starcompose2, funcs)
def starcompose2(f, g):
#Embedded file name: func.py
return lambda x: f(*g(x))
def typecheck(*types):
def decorator(func):
@wraps(func)
def typechecked(*args, **kwargs):
check = partial(handle_check, func=func, kwargs=kwargs)
map(check, types, args)
return func(*args, **kwargs)
return typechecked
return decorator