hbutils.model.compare

Overview:

Base interface to quickly implement a comparable object.

This module provides the IComparable interface class that allows easy implementation of comparison operations (__eq__, __ne__, __lt__, __le__, __gt__, __ge__) for custom objects by only requiring the implementation of a single _cmpkey() method.

IComparable

class hbutils.model.compare.IComparable[source]
Overview:

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)[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)[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)[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)[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)[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)[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.

_cmpkey()[source]

Function for getting a key value which is used for comparison.

Returns:

A value used to compare.

Raises:

NotImplementedError – This method must be implemented by subclasses.

Note

Subclasses must override this method to return a comparable value that represents the object for comparison purposes.