WhintPy 1.1

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

Module whintpy.filters

Class StringComparator

Description

Comparison methods for strings.

Extend BaseComparator() to provide various string comparison methods. It includes methods for exact matches, case-insensitive matches, diacritics-insensitive matches, and pattern matching using regular expressions. The class ensures that the inputs are strings and raises appropriate errors for invalid inputs.

Example
 >>> sc = StringComparator()
 >>> sc.exact("abc", "abc")
 > True
 >>> sc.get('exact')("abc", "abc")
 > True
 >>> sc.istartswith("hello", "HE")
 > True
 >>> sc.regexp("hello123", r"\d+")
 > True

Constructor

Create a StringComparator instance.

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

    """
    super().__init__()
    self._methods['exact'] = StringComparator.exact
    self._methods['iexact'] = StringComparator.iexact
    self._methods['aexact'] = StringComparator.aexact
    self._methods['startswith'] = StringComparator.startswith
    self._methods['istartswith'] = StringComparator.istartswith
    self._methods['astartswith'] = StringComparator.astartswith
    self._methods['endswith'] = StringComparator.endswith
    self._methods['iendswith'] = StringComparator.iendswith
    self._methods['aendswith'] = StringComparator.aendswith
    self._methods['contains'] = StringComparator.contains
    self._methods['icontains'] = StringComparator.icontains
    self._methods['acontains'] = StringComparator.acontains
    self._methods['regexp'] = StringComparator.regexp

Public functions

exact

Test if two strings strictly contain the same characters.

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def exact(s1: str, s2: str) -> bool:
    """Test if two strings strictly contain the same characters.

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1 == s2

iexact

Test if two strings contain the same characters (case-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def iexact(s1: str, s2: str) -> bool:
    """Test if two strings contain the same characters (case-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1.lower() == s2.lower()

aexact

Test if two strings contain the same characters (case-insensitive, diacritics-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def aexact(s1: str, s2: str) -> bool:
    """Test if two strings contain the same characters (case-insensitive, diacritics-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    s1 = s1.lower()
    s2 = s2.lower()
    return TypesDealer.remove_diacritics_and_non_ascii(s1) == TypesDealer.remove_diacritics_and_non_ascii(s2)

startswith

Test if the first string starts with the second string.

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def startswith(s1: str, s2: str) -> bool:
    """Test if the first string starts with the second string.

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1.startswith(s2)

istartswith

Test if the first string starts with the second string (case-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def istartswith(s1: str, s2: str) -> bool:
    """Test if the first string starts with the second string (case-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1.lower().startswith(s2.lower())

astartswith

Test if the first string starts with the second string (case-insensitive, diacritics-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def astartswith(s1: str, s2: str) -> bool:
    """Test if the first string starts with the second string (case-insensitive, diacritics-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    s1 = s1.lower()
    s2 = s2.lower()
    s1 = TypesDealer.remove_diacritics_and_non_ascii(s1)
    s2 = TypesDealer.remove_diacritics_and_non_ascii(s2)
    return s1.startswith(s2)

endswith

Test if the first string ends with the second string.

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def endswith(s1: str, s2: str) -> bool:
    """Test if the first string ends with the second string.

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1.endswith(s2)

iendswith

Test if the first string ends with the second string (case-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def iendswith(s1: str, s2: str) -> bool:
    """Test if the first string ends with the second string (case-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s1.lower().endswith(s2.lower())

aendswith

Test if the first string ends with the second string (case-insensitive, diacritics-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def aendswith(s1: str, s2: str) -> bool:
    """Test if the first string ends with the second string (case-insensitive, diacritics-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    s1 = s1.lower()
    s2 = s2.lower()
    s1 = TypesDealer.remove_diacritics_and_non_ascii(s1)
    s2 = TypesDealer.remove_diacritics_and_non_ascii(s2)
    return s1.endswith(s2)

contains

Test if the first string contains the second string.

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def contains(s1: str, s2: str) -> bool:
    """Test if the first string contains the second string.

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s2 in s1

icontains

Test if the first string contains the second string (case-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def icontains(s1: str, s2: str) -> bool:
    """Test if the first string contains the second string (case-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    return s2.lower() in s1.lower()

acontains

Test if the first string contains the second string (case-insensitive and diacritics-insensitive).

Parameters
  • s1: (str) String to compare.
  • s2: (str) String to be compared with.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def acontains(s1: str, s2: str) -> bool:
    """Test if the first string contains the second string (case-insensitive and diacritics-insensitive).

        :param s1: (str) String to compare.
        :param s2: (str) String to be compared with.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s1)
    StringComparator._check_string(s2)
    s1 = s1.lower()
    s2 = s2.lower()
    return TypesDealer.remove_diacritics_and_non_ascii(s2) in TypesDealer.remove_diacritics_and_non_ascii(s1)

regexp

Test if the first string matches the second string.

Parameters
  • s: (str) String.
  • pattern: (str) Pattern to match in string.
Returns
  • (bool)
Raises
  • TypeError: invalid given parameter.
View Source
@staticmethod
def regexp(s: str, pattern: str) -> bool:
    """Test if the first string matches the second string.

        :param s: (str) String.
        :param pattern: (str) Pattern to match in string.
        :return: (bool)
        :raises: TypeError: invalid given parameter.

        """
    StringComparator._check_string(s)
    StringComparator._check_string(pattern)
    return True if re.match(pattern, s) else False

Private functions

_check_string

Raise TypeError if the given parameter is not a string.

Parameters
  • s: (str) A string to be checked.
Raises
  • TypeError: If the given parameter is not a string.
View Source
@staticmethod
def _check_string(s: str) -> None:
    """Raise TypeError if the given parameter is not a string.

        :param s: (str) A string to be checked.
        :raises: TypeError: If the given parameter is not a string.

        """
    if isinstance(s, str) is False:
        raise TypeError(f'Expected a string. Got {str(type(s))} instead.')