Filters — Building generic filter expressions

filters is a generic filter expression module to build arbitrary callable filters. A filter expression returns True or False.

NOT Filter

Functions as an invert of an individual filter.

filter_not(filter)

AND Filter

Returns True if all filter expressions return True individually.

filter_and(filter1, filter2, ..., filterN)

NAND Filter

Functions as an inverted AND Filter (filter_and()).

filter_nand(filter1, filter2, ..., filterN)

OR Filter

Returns True as soon as any of the filter expressions returns True individually.

filter_or(filter_or(filter1, filter2, ..., filterN))

NOR Filter

Functions as an inverted OR Filter (filter_or()).

filter_nor(filter1, filter2, ..., filterN)

XOR Filter

Returns True if an odd number of filters individually returns True. In this, Exceptions are rated as a filter returning False.

filter_xor(filter1, filter2, ..., filterN)

XNOR Filter

Functions as an inverted XOR Filter (filter_xor()).

filter_xnor(filter1, filter2, ..., filterN)

Examples

A possible example for an OR-Filter is:

from htf.filters import filter_or

def filter1(arg):
    return arg == 1

def filter2(arg):
    return arg == 2

filter1(1)  # True
filter1(2)  # False

filter1or2 = filter_or(filter1, filter2)  # create a new callable filter that matches 1 or 2

filter1or2(1)  # True
filter1or2(2)  # True
filter1or2(3)  # False

A possible example for a XOR-Filter is:

from htf.filters import filter_xor

t = lambda *args, **kwargs: True

print(filter_xor(t)())  # True
print(filter_xor(t, t)())  # False
print(filter_xor(t, t, t)())  # True
print(filter_xor(True, False)())  # True

Filters

htf.filters.filter_and(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using logical and.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that combines all elements in *filters using logical and.

Return type:

callable

htf.filters.filter_nand(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using logical nand.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that combines all elements in *filters using logical nand.

Return type:

callable

htf.filters.filter_nor(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using logical nor.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that combines all elements in *filters using logical nor.

Return type:

callable

htf.filters.filter_not(filter_method: Callable[[...], bool]) Callable[[...], bool]

Create a filter that inverts filter_method’s result or values.

Parameters:

filter_method (callable) – the filter that is inverted.

Returns:

a filter that inverts filter_method.

Return type:

callable

htf.filters.filter_or(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using or-logic.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that conjuncts all elements in *filters.

Return type:

callable

htf.filters.filter_xnor(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using logical xnor.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that combines all elements in *filters using logical xnor.

Return type:

callable

htf.filters.filter_xor(*filters: Callable[[...], bool]) Callable[[...], bool]

Create a filter that combines all results or values of *filters using xor-logic.

Parameters:

*filters – a tuple of filters.

Returns:

a filter that xors all elements in *filters.

Return type:

callable

Changelog

htf-filters-4.0.5

  • add support for Python 3.13

htf-filters-4.0.4

  • add requirements for validation purposes

htf-filters-4.0.3

  • add build environment data for validation purposes

htf-filters-4.0.2

  • htf-filters can now be used standalone

htf-filters-4.0.1

  • add support for Python 3.12

htf-filters-4.0.0

  • remove all camel-case methods

  • add type-hints

  • use hlm-3.1

htf-filters-3.0.1

  • add support for Python 3.11

htf-filters-3.0.0

  • extract htf-filters from htf