polymatheia.filter
The filter
module implements a number of filtering classes.
All filters support the same expression language and are designed to work with the
NavigableDict
. Each individual expression is structured as follows:
('operator', 'parameter'[, 'parameter', ...])
. The number of parameters depends on the operator:
Equal:
('eq', param1, param2)
: returnsTrue
if the two parameters are equalNot-equal:
('neq', param1, param2)
: returnsTrue
if the two parameters are not equalGreater than:
('gt', param1, param2)
: returnsTrue
if the param1 is greater than param2Greater than or equal:
('gte', param1, param2)
: returnsTrue
if the param1 is greater than or equal to param2Less than:
('lt', param1, param2)
: returnsTrue
if the param1 is less than param2Less than or equal:
('lte', param1, param2)
: returnsTrue
if the param1 is less than or equal to param2Containment:
('contains', param1, param2)
: returnsTrue
if the param2 is contained within param1Exists:
('exists', param1)
: returnsTrue
if the param1 exists and has a value that is notNone
Not:
('not', expression)
: See belowAnd:
('and', expression1, expression2, ...)
: See belowOr:
('and', expression1, expression2, ...)
: See belowTrue:
('true',)
: always returnsTrue
False:
('true',)
: always returnsFalse
In the first six expressions both param1
and param2
can be one of the following three, allowing for comparison
of a value from a NavigableDict
record against a constant value, two values from the same
NavigableDict
record, or two constant values:
A constant value: The comparison is performed against this value
A dotted path: The comparison is performed against the value fetched from the record using the path
A a list: The comparison is performed against the value fetched from the record using the path in list form. This is needed if either the path elements contain a full-stop or square brackets or if you wish to compare against a value at the top level of the
NavigableDict
record.
For the operators 'not'
, 'and'
, and 'or'
the expression
parameter is a nested expression using any of
the given operators. The 'not'
operator only supports at most a single nested expression. The 'and'
and
'or'
operators allow any number of nested expression parameters. This
'not'
,'and'
, or'or'
with no nested expressions always returnTrue
'and'
or'or'
with a single nested expression always return the value of the nested expression'and'
or'or'
with more than one nested expression are evaluated lazily. That means that for'and'
as soon as a nested expression isFalse
,False
is immediately returned. Likewise for'or'
as soon as a nested expression isTrue
, thenTrue
is immediately returned.
- class polymatheia.filter.Filter(expression)
The
Filter
is a callable filter forNavigableDict
.- __call__(record)
Apply the expression to the
record
.- Parameters:
record (
NavigableDict
) – The record to apply the filter expression to.- Returns:
Whether the filter expression matches the record or not.
- Return type:
bool
- __init__(expression)
Create a new
Filter
.- Parameters:
expression (
tuple
) – The expression that thisFilter
will apply.
- __weakref__
list of weak references to the object (if defined)
- class polymatheia.filter.RecordsFilter(records, expression)
The
RecordsFilter
provides a records filter iteration container.It allows iterating over all
NavigableDict
that match the given filter expression. Filtering is performed using aFilter
.- __init__(records, expression)
Create a new
RecordsFilter
.- Parameters:
records – The source records to iterate over and filter.
expression (
tuple
orFilter
) – The filter expression to apply
- __iter__()
Return a new class:~polymatheia.filter.RecordsFilterIterator as the iterator.
- __weakref__
list of weak references to the object (if defined)
- class polymatheia.filter.RecordsFilterIterator(records, expression)
The
RecordsFilterIterator
provides a records filter iterator.It allows iterating over all
NavigableDict
that match the given filter expression. Filtering is performed using aFilter
.- __init__(records, expression)
Create a new
RecordsFilterIterator
.- Parameters:
records – The source records to iterate over and filter.
expression (
tuple
orFilter
) – The filter expression to apply
- __iter__()
Return this class:~polymatheia.filter.RecordsFilterIterator as the iterator.
- __next__()
Return the next
NavigableDict
that matches the filter.- Returns:
The next record that matches the filter
- Return type:
- Raises:
StopIteration – If no more records are available
- __weakref__
list of weak references to the object (if defined)