-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathfunctools.py
50 lines (42 loc) · 1.37 KB
/
functools.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
def partial(func, *args, **kwargs):
def _partial(*more_args, **more_kwargs):
kw = kwargs.copy()
kw.update(more_kwargs)
return func(*(args + more_args), **kw)
return _partial
def update_wrapper(wrapper, wrapped, assigned=None, updated=None):
# Dummy impl
return wrapper
def wraps(wrapped, assigned=None, updated=None):
# Dummy impl
return lambda x: x
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
# CPython git v3.4.10
def cmp_to_key(mycmp):
"""Convert a cmp= function into a key= function"""
class K(object):
__slots__ = ['obj']
def __init__(self, obj):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
__hash__ = None
return K