hbutils.model.compare
Comparable object utilities.
This module provides a lightweight base interface for implementing rich
comparison operations on custom objects. Subclasses only need to implement
the IComparable._cmpkey() method, and all comparison operators
(==, !=, <, <=, >, >=) will be derived from it.
The module contains the following main components:
IComparable- Base interface for defining comparable objects
Example:
>>> from hbutils.model.compare import IComparable
>>> class MyValue(IComparable):
... def __init__(self, v: int) -> None:
... self._v = v
...
... def _cmpkey(self):
... return self._v
...
>>> MyValue(1) < MyValue(2)
True
__all__
- hbutils.model.compare.__all__ = ['IComparable']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
IComparable
- class hbutils.model.compare.IComparable[source]
Interface for a comparable object.
This class provides a base interface for creating comparable objects. Subclasses only need to implement the
_cmpkey()method to enable all comparison operations. The comparison is based on the key values returned by_cmpkey().- 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
- __eq__(other: Any) bool[source]
Check equality between two objects.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if objects are equal, False otherwise.
- Return type:
bool
Note
Returns True immediately if comparing with self (identity check). Otherwise, compares using
_cmpkey()values if types match.
- __ge__(other: Any) bool[source]
Check if this object is greater than or equal to another object.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if this object is greater than or equal to other, False otherwise.
- Return type:
bool
- __gt__(other: Any) bool[source]
Check if this object is greater than another object.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if this object is greater than other, False otherwise.
- Return type:
bool
- __le__(other: Any) bool[source]
Check if this object is less than or equal to another object.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if this object is less than or equal to other, False otherwise.
- Return type:
bool
- __lt__(other: Any) bool[source]
Check if this object is less than another object.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if this object is less than other, False otherwise.
- Return type:
bool
- __ne__(other: Any) bool[source]
Check inequality between two objects.
- Parameters:
other (object) – The other object to compare with.
- Returns:
True if objects are not equal, False otherwise.
- Return type:
bool
Note
Returns False immediately if comparing with self (identity check). Otherwise, compares using
_cmpkey()values if types match.