|
8 | 8 |
|
9 | 9 | def underscore_hating_key(string): |
10 | 10 | '''Key function for sorting that treats `_` as last character.''' |
11 | | - assert isinstance(string, basestring) |
12 | | - return unicode(string).replace('_', unichr(sys.maxunicode)) |
13 | | - |
14 | | - |
15 | | -def total_ordering(cls): |
16 | | - ''' |
17 | | - Add full arsenal of ordering methods to a class based on existing subset. |
18 | | - ''' |
19 | | - convert = { |
20 | | - '__lt__': [('__gt__', lambda self, other: |
21 | | - not (self < other or self == other)), |
22 | | - ('__le__', lambda self, other: |
23 | | - self < other or self == other), |
24 | | - ('__ge__', lambda self, other: |
25 | | - not self < other)], |
26 | | - '__le__': [('__ge__', lambda self, other: |
27 | | - not self <= other or self == other), |
28 | | - ('__lt__', lambda self, other: |
29 | | - self <= other and not self == other), |
30 | | - ('__gt__', lambda self, other: |
31 | | - not self <= other)], |
32 | | - '__gt__': [('__lt__', lambda self, other: |
33 | | - not (self > other or self == other)), |
34 | | - ('__ge__', lambda self, other: |
35 | | - self > other or self == other), |
36 | | - ('__le__', lambda self, other: |
37 | | - not self > other)], |
38 | | - '__ge__': [('__le__', lambda self, other: |
39 | | - (not self >= other) or self == other), |
40 | | - ('__gt__', lambda self, other: |
41 | | - self >= other and not self == other), |
42 | | - ('__lt__', lambda self, other: |
43 | | - not self >= other)] |
44 | | - } |
45 | | - roots = set(dir(cls)) & set(convert) |
46 | | - if not roots: |
47 | | - raise ValueError('Must define at least one ordering operation: `<`, ' |
48 | | - '`>`, `<=`, or `>=`.') |
49 | | - root = max(roots) # We prefer __lt__ to __le__ to __gt__ to __ge__ |
50 | | - for opname, opfunc in convert[root]: |
51 | | - if opname not in roots: |
52 | | - opfunc.__name__ = opname |
53 | | - opfunc.__doc__ = getattr(float, opname).__doc__ |
54 | | - setattr(cls, opname, opfunc) |
55 | | - return cls |
| 11 | + assert isinstance(string, str) |
| 12 | + return str(string).replace('_', chr(sys.maxunicode)) |
56 | 13 |
|
57 | 14 |
|
58 | 15 | def process_key_function_or_attribute_name(key_function_or_attribute_name): |
|
0 commit comments