Skip to main content

filtration - A library for parsing arbitrary filters

Project description

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 git+https://github.com/HurricaneLabs/filtration.git

Run the tests

You must install nose2, then run:

git clone https://github.com/HurricaneLabs/filtration.git
cd filtration
nose2

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, a special “LHS exists” syntax is assumed. 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
>>> Expression.parseString("a and b or c")
((a and b) or c)
>>> Expression.parseString("a and (b or c)")
(a and (b or c))
>>>

Filtration can also be used to parse query string syntax. See below for more information.

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 symbol in the LHS exists at all.

  • 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 resolves to true if the LHS key exists in the context. For example:

>>> c = {"a": 1, "b": 2, "c": 3}
>>> expr = Expression.parseString("a")
>>> expr(c)
True
>>> 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
>>>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filtration-2.3.2.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

filtration-2.3.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file filtration-2.3.2.tar.gz.

File metadata

  • Download URL: filtration-2.3.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for filtration-2.3.2.tar.gz
Algorithm Hash digest
SHA256 4046329684f53653a042c7168dda9f46d7e6b4a59aa6f7ab20566976ea1fc3ef
MD5 eeacc9d901e51d8fa076dd847d71c401
BLAKE2b-256 538b225d12a29ce327df9930312ef40658aca7eed9723f5249ec2e9b62a747fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for filtration-2.3.2.tar.gz:

Publisher: publish.yml on HurricaneLabs/filtration

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file filtration-2.3.2-py3-none-any.whl.

File metadata

  • Download URL: filtration-2.3.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for filtration-2.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd30680cfce84eb50f54738c1f9cc9b90fef7fe4b3eb576d00eeccd47577b59e
MD5 c835ea1d28621607d0c5d672c9d1e9f8
BLAKE2b-256 ed7c291cb1fa36ba57a8a16cb9e7af3389bb6103c08e6619fc7d93b39c611403

See more details on using hashes here.

Provenance

The following attestation bundles were made for filtration-2.3.2-py3-none-any.whl:

Publisher: publish.yml on HurricaneLabs/filtration

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page