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
|
"""
Compatibility functionality stubs
"""
__all__ = ("cmp", "sorted_cmp", "sort_cmp")
def sorted_key_from_cmp(cmp_func, key_func=None):
class _key_proxy:
__slots__ = ('_obj',)
if key_func: # done this way for speed reasons.
def __init__(self, obj, key_convert=key_func):
self._obj = key_convert(obj)
else:
def __init__(self, obj):
self._obj = obj
def __lt__(self, other, _cmp_func=cmp_func):
return _cmp_func(self._obj, other._obj) < 0
return _key_proxy
# yes this is heinous. this is what they recommended in the python
# docs for porting however...
def _raw_cmp(a, b):
return (a > b) - (a < b)
def cmp(obj1, obj2, raw_cmp=_raw_cmp):
if obj1 is None:
if obj2 is None:
return 0
return -1
elif obj2 is None:
return 1
return raw_cmp(obj1, obj2)
def sorted_cmp(sequence, func, key=None, reverse=False):
return sorted(sequence, reverse=reverse,
key=sorted_key_from_cmp(func, key_func=key))
def sort_cmp(list_inst, func, key=None, reverse=False):
list_inst.sort(reverse=reverse,
key=sorted_key_from_cmp(func, key_func=key))
IGNORED_EXCEPTIONS = (RuntimeError, MemoryError, SystemExit, KeyboardInterrupt)
|