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): returns True if the two parameters are equal

  • Not-equal: ('neq', param1, param2): returns True if the two parameters are not equal

  • Greater than: ('gt', param1, param2): returns True if the param1 is greater than param2

  • Greater than or equal: ('gte', param1, param2): returns True if the param1 is greater than or equal to param2

  • Less than: ('lt', param1, param2): returns True if the param1 is less than param2

  • Less than or equal: ('lte', param1, param2): returns True if the param1 is less than or equal to param2

  • Containment: ('contains', param1, param2): returns True if the param2 is contained within param1

  • Exists: ('exists', param1): returns True if the param1 exists and has a value that is not None

  • Not: ('not', expression): See below

  • And: ('and', expression1, expression2, ...): See below

  • Or: ('and', expression1, expression2, ...): See below

  • True: ('true',): always returns True

  • False: ('true',): always returns False

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 return True

  • '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 is False, False is immediately returned. Likewise for 'or' as soon as a nested expression is True, then True is immediately returned.

class polymatheia.filter.Filter(expression)

The Filter is a callable filter for NavigableDict.

__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 this Filter 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 a Filter.

__init__(records, expression)

Create a new RecordsFilter.

Parameters:
  • records – The source records to iterate over and filter.

  • expression (tuple or Filter) – 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 a Filter.

__init__(records, expression)

Create a new RecordsFilterIterator.

Parameters:
  • records – The source records to iterate over and filter.

  • expression (tuple or Filter) – 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:

NavigableDict

Raises:

StopIteration – If no more records are available

__weakref__

list of weak references to the object (if defined)