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)

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

Parameters

*filters (tuple) – a tuple of filters.

Returns

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

Return type

callable

htf.filters.filter_nand(*filters)

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

Parameters

*filters (tuple) – a tuple of filters.

Returns

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

Return type

callable

htf.filters.filter_nor(*filters)

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

Parameters

*filters (tuple) – 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)

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)

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

Parameters

*filters (tuple) – a tuple of filters.

Returns

a filter that conjuncts all elements in *filters.

Return type

callable

htf.filters.filter_xnor(*filters)

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

Parameters

*filters (tuple) – a tuple of filters.

Returns

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

Return type

callable

htf.filters.filter_xor(*filters)

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

Parameters

*filters (tuple) – a tuple of filters.

Returns

a filter that xors all elements in *filters.

Return type

callable