total_ordering.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. From django.utils.
  6. """
  7. import sys
  8. if sys.version_info >= (2, 7, 2):
  9. from functools import total_ordering
  10. else:
  11. def total_ordering(cls):
  12. """Class decorator that fills in missing ordering methods"""
  13. convert = {
  14. '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
  15. ('__le__', lambda self, other: self < other or self == other),
  16. ('__ge__', lambda self, other: not self < other)],
  17. '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
  18. ('__lt__', lambda self, other: self <= other and not self == other),
  19. ('__gt__', lambda self, other: not self <= other)],
  20. '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
  21. ('__ge__', lambda self, other: self > other or self == other),
  22. ('__le__', lambda self, other: not self > other)],
  23. '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
  24. ('__gt__', lambda self, other: self >= other and not self == other),
  25. ('__lt__', lambda self, other: not self >= other)]
  26. }
  27. roots = set(dir(cls)) & set(convert)
  28. if not roots:
  29. raise ValueError('must define at least one ordering operation: < > <= >=')
  30. root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
  31. for opname, opfunc in convert[root]:
  32. if opname not in roots:
  33. opfunc.__name__ = opname
  34. opfunc.__doc__ = getattr(int, opname).__doc__
  35. setattr(cls, opname, opfunc)
  36. return cls