WhintPy 1.1

https://sourceforge.net/projects/whintpy/

Module whintpy.filters

Class NumericComparator

Description

Comparison methods for 'int' or 'float' objects.

Extend the BaseComparator class to provide comparison methods specifically for numeric types (int and float). It includes methods to check equality, less than, less than or equal to, greater than, and greater than or equal to between two numeric values. The class ensures type consistency and raises appropriate errors for type mismatches.

Example
 >>> tc = 4
 >>> tc.equal(4, 2*2)
 > True
 >>> tc.get('ge')(8, 2*2*2)
 > True

Constructor

Create a NumericComparator instance.

View Source
def __init__(self):
    """Create a NumericComparator instance.

    """
    super().__init__()
    self._methods['equal'] = NumericComparator.equal
    self._methods['le'] = NumericComparator.le
    self._methods['lt'] = NumericComparator.lt
    self._methods['ge'] = NumericComparator.ge
    self._methods['gt'] = NumericComparator.gt

Public functions

equal

Test if two numbers are equal to each other.

Parameters
  • a: (int|float) number to compare.
  • b: (datetime) number to be compared with.
Raises
  • TypeError: invalid given parameter.
  • TypeError: Inconsistent types
Returns
  • (bool)
View Source
@staticmethod
def equal(a: int | float, b: int | float) -> bool:
    """Test if two numbers are equal to each other.

        :param a: (int|float) number to compare.
        :param b: (datetime) number to be compared with.
        :raises: TypeError: invalid given parameter.
        :raises: TypeError: Inconsistent types
        :return: (bool)

        """
    NumericComparator._check_numeric(a)
    NumericComparator._check_numeric(b)
    if type(a) is type(b):
        return a == b
    else:
        raise TypeError()

lt

Test if the first number is lower than the second number.

Parameters
  • a: (int|float) number to compare.
  • b: (datetime) number to be compared with.
Raises
  • TypeError: invalid given parameter.
  • TypeError: Inconsistent types
Returns
  • (bool)
View Source
@staticmethod
def lt(a: int | float, b: int | float) -> bool:
    """Test if the first number is lower than the second number.

        :param a: (int|float) number to compare.
        :param b: (datetime) number to be compared with.
        :raises: TypeError: invalid given parameter.
        :raises: TypeError: Inconsistent types
        :return: (bool)

        """
    NumericComparator._check_numeric(a)
    NumericComparator._check_numeric(b)
    if type(a) is type(b):
        return a < b
    else:
        raise TypeError(NumericComparator._ERROR_TYPES_MISMATCH.format(str(type(a)), str(type(b))))

le

Test if the first number is before or equal to the second number.

Parameters
  • a: (int|float) number to compare.
  • b: (datetime) number to be compared with.
Raises
  • TypeError: invalid given parameter.
  • TypeError: Inconsistent types
Returns
  • (bool)
View Source
@staticmethod
def le(a: int | float, b: int | float) -> bool:
    """Test if the first number is before or equal to the second number.

        :param a: (int|float) number to compare.
        :param b: (datetime) number to be compared with.
        :raises: TypeError: invalid given parameter.
        :raises: TypeError: Inconsistent types
        :return: (bool)

        """
    NumericComparator._check_numeric(a)
    NumericComparator._check_numeric(b)
    if type(a) is type(b):
        return a <= b
    else:
        raise TypeError(NumericComparator._ERROR_TYPES_MISMATCH.format(str(type(a)), str(type(b))))

gt

Test if the first number is after the second number.

Parameters
  • a: (int|float) number to compare.
  • b: (datetime) number to be compared with.
Raises
  • TypeError: invalid given parameter.
  • TypeError: Inconsistent types
Returns
  • (bool)
View Source
@staticmethod
def gt(a: int | float, b: int | float) -> bool:
    """Test if the first number is after the second number.

        :param a: (int|float) number to compare.
        :param b: (datetime) number to be compared with.
        :raises: TypeError: invalid given parameter.
        :raises: TypeError: Inconsistent types
        :return: (bool)

        """
    NumericComparator._check_numeric(a)
    NumericComparator._check_numeric(b)
    if type(a) is type(b):
        return a > b
    else:
        raise TypeError(NumericComparator._ERROR_TYPES_MISMATCH.format(str(type(a)), str(type(b))))

ge

Test if the first number is after or equal to the second number.

Parameters
  • a: (int|float) number to compare.
  • b: (datetime) number to be compared with.
Raises
  • TypeError: invalid given parameter.
  • TypeError: Inconsistent types
Returns
  • (bool)
View Source
@staticmethod
def ge(a: int | float, b: int | float) -> bool:
    """Test if the first number is after or equal to the second number.

        :param a: (int|float) number to compare.
        :param b: (datetime) number to be compared with.
        :raises: TypeError: invalid given parameter.
        :raises: TypeError: Inconsistent types
        :return: (bool)

        """
    NumericComparator._check_numeric(a)
    NumericComparator._check_numeric(b)
    if type(a) is type(b):
        return a >= b
    else:
        raise TypeError(NumericComparator._ERROR_TYPES_MISMATCH.format(str(type(a)), str(type(b))))

Private functions

_check_numeric

Raise TypeError if not an int or float object.

Parameters
  • entry: (int|float) A date to be checked
Raises
  • TypeError: d is not of the expected type
View Source
@staticmethod
def _check_numeric(entry: int | float) -> None:
    """Raise TypeError if not an int or float object.

        :param entry: (int|float) A date to be checked
        :raises: TypeError: d is not of the expected type

        """
    if isinstance(entry, NumericComparator.TYPES) is False:
        raise TypeError(f'Expected a numeric type. Got {str(type(entry))} instead.')