Filters — Building generic filter expressions

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

htf.filters.filterAnd(*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

Usage:

>>> filterAnd(filter1, filter2, ..., filterN)
# If all filters return True, True is returned
htf.filters.filterNand(*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

Usage:

>>> filterNand(filter1, filter2, ..., filterN)
# inverted filterAnd()
htf.filters.filterNor(*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

Usage:

>>> filterNor(filter1, filter2, ..., filterN)
# inverted filterOr()
htf.filters.filterNot(filterMethod)

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

Parameters

filterMethod (callable) – the filter that is inverted.

Returns

a filter that inverts filterMethod.

Return type

callable

Usage:

>>> filterNot(filter)
# inverted filter
htf.filters.filterOr(*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

Usage:

>>> filterOr(filter1, filter2, ..., filterN)
# If at least one filter returns True, True is returned immediately
htf.filters.filterXnor(*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

Usage:

>>> filterXnor(filter1, filter2, ..., filterN)
# inverted filterXor()
htf.filters.filterXor(*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

Usage:

>>> filterXor(filter1, filter2, ..., filterN)
# If an odd number of filters return True, True is returned
# Exception are rated as a filter returning False

Examples

OR-filter:

from htf.filters import filterOr

def filter1(arg):
    return arg == 1

def filter2(arg):
    return arg == 2

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

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

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

XOR-filter:

from htf.filters import filterXor

t = lambda *args, **kwargs: True

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