| | 313 | |
| | 314 | try: |
| | 315 | from functools import total_ordering |
| | 316 | except ImportError: |
| | 317 | # For Python < 2.7 |
| | 318 | def total_ordering(cls): |
| | 319 | """Class decorator that fills in missing ordering methods""" |
| | 320 | convert = { |
| | 321 | '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), |
| | 322 | ('__le__', lambda self, other: self < other or self == other), |
| | 323 | ('__ge__', lambda self, other: not self < other)], |
| | 324 | '__le__': [('__ge__', lambda self, other: not self <= other or self == other), |
| | 325 | ('__lt__', lambda self, other: self <= other and not self == other), |
| | 326 | ('__gt__', lambda self, other: not self <= other)], |
| | 327 | '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), |
| | 328 | ('__ge__', lambda self, other: self > other or self == other), |
| | 329 | ('__le__', lambda self, other: not self > other)], |
| | 330 | '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), |
| | 331 | ('__gt__', lambda self, other: self >= other and not self == other), |
| | 332 | ('__lt__', lambda self, other: not self >= other)] |
| | 333 | } |
| | 334 | roots = set(dir(cls)) & set(convert) |
| | 335 | if not roots: |
| | 336 | raise ValueError('must define at least one ordering operation: < > <= >=') |
| | 337 | root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ |
| | 338 | for opname, opfunc in convert[root]: |
| | 339 | if opname not in roots: |
| | 340 | opfunc.__name__ = opname |
| | 341 | opfunc.__doc__ = getattr(int, opname).__doc__ |
| | 342 | setattr(cls, opname, opfunc) |
| | 343 | return cls |