Source code for hbutils.model.compare

"""
Overview:
    Base interface to quickly implement a comparable object.
"""
import operator as ops

__all__ = [
    'IComparable',
]


[docs]class IComparable: """ Overview: Interface for a comparable object. Examples:: >>> from hbutils.model import IComparable >>> class MyValue(IComparable): ... def __init__(self, v) -> None: ... self._v = v ... ... def _cmpkey(self): ... return self._v ... >>> MyValue(1) == MyValue(1) True >>> MyValue(1) == MyValue(2) False >>> MyValue(1) != MyValue(2) True >>> MyValue(1) > MyValue(2) False >>> MyValue(1) >= MyValue(2) False >>> MyValue(1) < MyValue(2) True >>> MyValue(1) <= MyValue(2) True """
[docs] def _cmpkey(self): """ Function for getting a key value which is used for comparison. :return: A value used to compare. """ raise NotImplementedError # pragma: no cover
def _cmpcheck(self, op, other, default=False): if type(self) == type(other): return op(self._cmpkey(), other._cmpkey()) else: return default
[docs] def __eq__(self, other): if self is other: return True else: return self._cmpcheck(ops.__eq__, other, default=False)
[docs] def __ne__(self, other): if self is other: return False else: return self._cmpcheck(ops.__ne__, other, default=True)
[docs] def __lt__(self, other): return self._cmpcheck(ops.__lt__, other)
[docs] def __le__(self, other): return self._cmpcheck(ops.__le__, other)
[docs] def __gt__(self, other): return self._cmpcheck(ops.__gt__, other)
[docs] def __ge__(self, other): return self._cmpcheck(ops.__ge__, other)