functools.py 765 B

1234567891011121314151617181920212223
  1. # Taken from python2.7/3.3 functools
  2. def cmp_to_key(mycmp):
  3. """Convert a cmp= function into a key= function"""
  4. class K(object):
  5. __slots__ = ['obj']
  6. def __init__(self, obj):
  7. self.obj = obj
  8. def __lt__(self, other):
  9. return mycmp(self.obj, other.obj) < 0
  10. def __gt__(self, other):
  11. return mycmp(self.obj, other.obj) > 0
  12. def __eq__(self, other):
  13. return mycmp(self.obj, other.obj) == 0
  14. def __le__(self, other):
  15. return mycmp(self.obj, other.obj) <= 0
  16. def __ge__(self, other):
  17. return mycmp(self.obj, other.obj) >= 0
  18. def __ne__(self, other):
  19. return mycmp(self.obj, other.obj) != 0
  20. __hash__ = None
  21. return K