OLD | NEW |
(Empty) | |
| 1 """ |
| 2 For Python < 2.7.2. total_ordering in versions prior to 2.7.2 is buggy. |
| 3 See http://bugs.python.org/issue10042 for details. For these versions use |
| 4 code borrowed from Python 2.7.3. |
| 5 |
| 6 From django.utils. |
| 7 """ |
| 8 |
| 9 import sys |
| 10 if sys.version_info >= (2, 7, 2): |
| 11 from functools import total_ordering |
| 12 else: |
| 13 def total_ordering(cls): |
| 14 """Class decorator that fills in missing ordering methods""" |
| 15 convert = { |
| 16 '__lt__': [('__gt__', lambda self, other: not (self < other or self
== other)), |
| 17 ('__le__', lambda self, other: self < other or self == ot
her), |
| 18 ('__ge__', lambda self, other: not self < other)], |
| 19 '__le__': [('__ge__', lambda self, other: not self <= other or self
== other), |
| 20 ('__lt__', lambda self, other: self <= other and not self
== other), |
| 21 ('__gt__', lambda self, other: not self <= other)], |
| 22 '__gt__': [('__lt__', lambda self, other: not (self > other or self
== other)), |
| 23 ('__ge__', lambda self, other: self > other or self == ot
her), |
| 24 ('__le__', lambda self, other: not self > other)], |
| 25 '__ge__': [('__le__', lambda self, other: (not self >= other) or sel
f == other), |
| 26 ('__gt__', lambda self, other: self >= other and not self
== other), |
| 27 ('__lt__', lambda self, other: not self >= other)] |
| 28 } |
| 29 roots = set(dir(cls)) & set(convert) |
| 30 if not roots: |
| 31 raise ValueError('must define at least one ordering operation: < > <
= >=') |
| 32 root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ |
| 33 for opname, opfunc in convert[root]: |
| 34 if opname not in roots: |
| 35 opfunc.__name__ = opname |
| 36 opfunc.__doc__ = getattr(int, opname).__doc__ |
| 37 setattr(cls, opname, opfunc) |
| 38 return cls |
OLD | NEW |