Skip to main content

Filtration

A library for parsing arbitrary filters provided by a UI or as a Query String

Features

  • Compatible with Python 3

  • Parses human readable filters

Get Filtration

pip install filtration

Run the tests

With uv installed, run:

git clone https://github.com/HurricaneLabs/filtration.git
cd filtration
uv run pytest

Usage

Filtration is used to parse “filter expressions” made up of one or more “statements”. Each statement is comprised of a “left hand side” (LHS), and optionally an operator and a “right hand side” (RHS). When no operator and RHS are specified, the statement checks whether the LHS is truthy. See “Expression Evaluation” for more details.

Statements are joined together using “AND” or “OR” to form an expression. Parentheses may be used when joining statements together to enforce precedence. For example:

>>> from filtration import Expression
>>> c = {"a": False, "b": False, "c": True}
>>> Expression.parseString("a and b or c")(c)  # (a and b) or c
True
>>> Expression.parseString("a and (b or c)")(c)
False
>>>

The whole filter must parse. Leftover text, such as the b in a == 1 b, raises a pyparsing.ParseException instead of being ignored. Pass parse_all=False to get the older behavior, which parses as much of the start of the filter as it can and ignores the rest.

Expression Format

Operators

These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus the RHS is also omitted), the statement returns True if the LHS has a truthy value.

  • Equal (“==”)

  • Not equal (“!=”)

  • Less than (“<”)

  • Less than or equal (“<=”)

  • Greater than (“>”)

  • Greater than or equal (“>=”)

  • Contains (“in”)
    • RHS must be a list or a Subnet

  • Regular expression (“=~”)
    • RHS must be a regex token

Tokens

These tokens are used in the LHS or RHS of a statement.

Regex

A regular expression must be wrapped in “/” and may contain the characters “i”, “m” or “s” after the closing “/” to represent the corresponding regex flags. Examples:

/abc/
/^abc/i
/^abc$/ms

Subnet

A subnet is an IPv4 subnet in CIDR notation:

127.0.0.0/8
192.168.0.0/24

Symbol

A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and can be followed by any number of letters, numbers, dots (“.”) or underscores. Dots have special meaning and are used to indicate dictionary traversal (see Expression Evaluation below).

Value

A value can be a date, time, date/time, quoted string, or an integer. A date is represented in YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the “T” is optional and could instead be represented by a space. This format is roughly ISO 8601, but is lacking microseconds.

List

A list is two-or-more Value tokens separated by commas.

Expression Evaluation

Expression objects are callable, with a single “context” argument. When used in this way, either true or false is returned, based on whether the “context” matches the filter. For example, given this context:

>>> c = {"a": 1, "b": 2, "c": 3}

and this expression:

>>> expr = Expression.parseString("a == 1 and b == 2")

calling the expression would return True:

>>> expr(c)
True
>>>

whereas this expression would return False:

>>> expr = Expression.parseString("a == 2 and b == 2")
>>> expr(c)
False
>>>

When no operator/RHS is provided, the statement is True if the LHS key is in the context and its value is truthy. A missing key, 0, "", False, None or an empty list gives False. For example:

>>> c = {"a": 1, "b": 0, "c": 3}
>>> expr = Expression.parseString("a")
>>> expr(c)
True
>>> expr = Expression.parseString("b")
>>> expr(c)
False
>>> expr = Expression.parseString("d")
>>> expr(c)
False
>>>

Dictionary Traversal

Symbols are able to traverse nested dictionaries in the context. Given this context:

>>> c = {"a": {"b": {"c": 1}}}

This expression will return true:

>>> expr = Expression.parseString("a.b.c == 1")
>>> expr(c)
True
>>>

Metadata

Release files for filtration 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for filtration 3.0.0
File Size Uploaded Provenance Actions
filtration-3.0.0.tar.gz 12.5 kB With provenance Details for filtration-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for filtration 3.0.0
File Interpreter ABI Platform Actions
filtration-3.0.0-py3-none-any.whl Python 3 none any Details for filtration-3.0.0-py3-none-any.whl

Total release size: 19.2 kB

Release files / filtration-3.0.0.tar.gz

Download URL filtration-3.0.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a57b706aa629c9635ee0816b55342a3970144bb66a4d7244ca1437b33178b120
BLAKE2b-256 checksum
How to use checksums
3b1f8180ffbbd9fbe9fa9c998c413ebe9a63819d6c881dc09f3edb97192e93e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / filtration-3.0.0-py3-none-any.whl

Download URL filtration-3.0.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ea9a8cd4a8888f4de24acc8eaed0fd57b2c0c2961405d66091e1c205a91b813
BLAKE2b-256 checksum
How to use checksums
5dd501e0ddd8384b70253b822cf9e6e66f746d1f86734e5fa43cc049be728e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for filtration 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for filtration-3.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page