You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Utilities for defining models
  2. """
  3. import operator
  4. class KeyBasedCompareMixin(object):
  5. """Provides comparision capabilities that is based on a key
  6. """
  7. def __init__(self, key, defining_class):
  8. self._compare_key = key
  9. self._defining_class = defining_class
  10. def __hash__(self):
  11. return hash(self._compare_key)
  12. def __lt__(self, other):
  13. return self._compare(other, operator.__lt__)
  14. def __le__(self, other):
  15. return self._compare(other, operator.__le__)
  16. def __gt__(self, other):
  17. return self._compare(other, operator.__gt__)
  18. def __ge__(self, other):
  19. return self._compare(other, operator.__ge__)
  20. def __eq__(self, other):
  21. return self._compare(other, operator.__eq__)
  22. def __ne__(self, other):
  23. return self._compare(other, operator.__ne__)
  24. def _compare(self, other, method):
  25. if not isinstance(other, self._defining_class):
  26. return NotImplemented
  27. return method(self._compare_key, other._compare_key)