1class ComparableMixin(object): 2 def _compare(self, other, method): 3 try: 4 return method(self._cmpkey(), other._cmpkey()) 5 except (AttributeError, TypeError): 6 # _cmpkey not implemented, or return different type, 7 # so I can't compare with "other". 8 return NotImplemented 9 10 def __lt__(self, other): 11 return self._compare(other, lambda s, o: s < o) 12 13 def __le__(self, other): 14 return self._compare(other, lambda s, o: s <= o) 15 16 def __eq__(self, other): 17 return self._compare(other, lambda s, o: s == o) 18 19 def __ge__(self, other): 20 return self._compare(other, lambda s, o: s >= o) 21 22 def __gt__(self, other): 23 return self._compare(other, lambda s, o: s > o) 24 25 def __ne__(self, other): 26 return self._compare(other, lambda s, o: s != o) 27