WhintPy 1.1

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

whintpy.filters module

Purpose

The main principle is to create base classes and methods to allow the definition of a predicate, or a combination of predicates, that will be used as parameters to create a filter of a set of entries, for example with name SomeFilter.predicate(comparator=value).

Comparators are implemented to filter either strings or dates. Other comparators can be added further.

Pattern selection

Pattern selection is an important part to extract data and is obviously an important part of any filtering system. Thus, the following functions are proposed:

  • exact match: predicate(exact = P) is true if the string strictly corresponds to the pattern P;
  • contains: predicate(contains = P) is true if the string contains the expected pattern P;
  • starts with, predicate(startswith = P) is true if the string starts with the expected pattern P;
  • ends with, predicate(endswith = P) is true if the string ends with the expected pattern P.

All these matches can be reversed to represent respectively: not exactly match, not contains, not starts with or not ends with. Moreover, this pattern matching can be either case-sensitive or not. And for complex search, a selection based on regular expressions is available for advanced users, as predicate(regexp = R), where R is the expected regexp.

Filtering dates

The following comparators allow to compare dates:

  • equal to compare equality between two dates
  • lt to compare if the first date is lower than the second one
  • either 'le' or 'before' can be used to test if the first datetime is before or equal to the second datetime
  • gt to compare if the first date is greater than the second one
  • 'ge', or 'after' can be used to test if the first datetime is after or equal to the second datetime

Multiple selections

A multiple pattern selection as well as dates can be expressed with the operator ”|” to represent the logical ”or” and the operator ”&” to represent the logical ”and”, with for example:


SelStr(startswith=P1) & SelDate(after=d)

Subclass example

>>> class PersonFilter:
>>>    def __init__(self, obj):
>>>        self.__obj = obj
>>>    def name(self, **kwargs) -> FilteredSet:
>>>        comparator = StringComparator()
>>>        # extract the information from the arguments
>>>        BaseFilters.test_args(comparator, **kwargs)
>>>        logic_bool = BaseFilters.fix_logic_bool(**kwargs)
>>>        string_functions = BaseFilters.fix_functions(comparator, **kwargs)
>>>        # search for the persons matching the given filters
>>>        data = FilteredSet()
>>>        for item in self.__obj:
>>>            is_matching = StringComparator.match(item.name, string_functions, logic_bool)
>>>            if is_matching is True:
>>>                # Store both the item of the object and the filter which makes it true
>>>                data.append(item)
>>>        return data

Typical usage of such class is:

>>> f = PersonFilter(persons)
>>> f.name(icontains="Arthur")
>>> f.name(startswith="pa", not_endswith='a', logic_bool="and")
>>> f.name(startswith="pa") | f.name(startswith="ta")
>>> f.name(startswith="pa") & f.name(startswith="ta")

List of classes