gchar.utils.compare

Comparable

class gchar.utils.compare.Comparable[source]

Base class that allows subclasses to perform arbitrary comparison operations by implementing the necessary interfaces.

Subclasses need to implement the _key method, which returns the key used for comparison.

Supported comparison operators: ==, !=, >=, >, <=, <

Usage:
  • Subclass Comparable and implement the _key method.

  • Objects of the subclass can then be compared using the defined comparison operators.

Example:
>>> class MyComparable(Comparable):
>>>     def __init__(self, value):
>>>         self.value = value
...
>>>     def _key(self):
>>>         return self.value
...
>>> obj1 = MyComparable(5)
>>> obj2 = MyComparable(10)
>>> print(obj1 == obj2)  # False
>>> print(obj1 < obj2)   # True
__eq__(other)[source]

Implementation of the equality operator (==).

Parameters:

other (object) – The other object to compare.

Returns:

True if the objects are equal, False otherwise.

Return type:

bool

__ge__(other)[source]

Implementation of the greater than or equal to operator (>=).

Parameters:

other (object) – The other object to compare.

Returns:

True if the current object is greater than or equal to the other object, False otherwise.

Return type:

bool

__gt__(other)[source]

Implementation of the greater than operator (>).

Parameters:

other (object) – The other object to compare.

Returns:

True if the current object is greater than the other object, False otherwise.

Return type:

bool

__le__(other)[source]

Implementation of the less than or equal to operator (<=).

Parameters:

other (object) – The other object to compare.

Returns:

True if the current object is less than or equal to the other object, False otherwise.

Return type:

bool

__lt__(other)[source]

Implementation of the less than operator (<).

Parameters:

other (object) – The other object to compare.

Returns:

True if the current object is less than the other object, False otherwise.

Return type:

bool

__ne__(other)[source]

Implementation of the inequality operator (!=).

Parameters:

other (object) – The other object to compare.

Returns:

True if the objects are not equal, False otherwise.

Return type:

bool

_check_same_type(other)[source]

Helper method to check if the type of the other object is the same as the current object’s type.

Parameters:

other (object) – The other object to compare.

Returns:

The other object if the types are the same.

Return type:

object

Raises:

TypeError – If the types are different.

_cmp(cmp, other)[source]

Helper method to perform comparison operations using the provided comparison function.

Parameters:
  • cmp (callable) – The comparison function to use.

  • other (object) – The other object to compare.

Returns:

The result of the comparison operation.

Return type:

bool

_key()[source]

Abstract method that needs to be implemented by subclasses. Returns the key used for comparison.

Returns:

The key used for comparison.

Return type:

object

Raises:

NotImplementedError – If the method is not implemented.