Skip to main content

Rule Engine

GitHub Workflow Status (branch) PyPI

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.

Documentation is available at https://zeroSteiner.github.io/rule-engine/.

Warning:

Version 6.0 will remove support for Python version 3.10. There is currently no timeline for its release.

Warning:

Version 5.0 removes support for Python versions 3.6, 3.7, 3.8, and 3.9 and deprecates attribute-style access on MAPPING values (use bracket syntax mapping["key"] instead). See the Change Log for details.

Rule Engine expressions are written in their own language, defined as strings in Python. The syntax is most similar to Python with some inspiration from Ruby. Some features of this language includes:

  • Optional type hinting

  • Matching strings with regular expressions

  • Datetime datatypes

  • Compound datatypes (equivalents for Python dict, list and set types)

  • Data attributes

  • Thread safety

  • Rule chains that return the value of the first matching rule

Example Usage

The following example demonstrates the basic usage of defining a rule object and applying it to two dictionaries, showing that one matches while the other does not. See Getting Started for more information.

import rule_engine
# match a literal first name and applying a regex to the email
rule = rule_engine.Rule(
    'first_name == "Luke" and email =~ ".*@rebels.org$"'
) # => <Rule text='first_name == "Luke" and email =~ ".*@rebels.org$"' >
rule.matches({
    'first_name': 'Luke', 'last_name': 'Skywalker', 'email': 'luke@rebels.org'
}) # => True
rule.matches({
   'first_name': 'Darth', 'last_name': 'Vader', 'email': 'dvader@empire.net'
}) # => False

The next example demonstrates the optional type system. A custom context is created that defines two symbols, one string and one float. Because symbols are defined, an exception will be raised if an unknown symbol is specified or an invalid operation is used. See Type Hinting for more information.

import rule_engine
# define the custom context with two symbols
context = rule_engine.Context(type_resolver=rule_engine.type_resolver_from_dict({
    'first_name': rule_engine.DataType.STRING,
    'age': rule_engine.DataType.FLOAT
}))

# receive an error when an unknown symbol is used
rule = rule_engine.Rule('last_name == "Vader"', context=context)
# => SymbolResolutionError: last_name

# receive an error when an invalid operation is used
rule = rule_engine.Rule('first_name + 1', context=context)
# => EvaluationError: data type mismatch

Want to give the rule expression language a try? Checkout the Debug REPL that makes experimentation easy. After installing just run python -m rule_engine.debug_repl.

Installation

Install the latest release from PyPi using pip install rule-engine. Releases follow Semantic Versioning to indicate in each new version whether it fixes bugs, adds features or breaks backwards compatibility. See the Change Log for a curated list of changes.

Credits

  • Spencer McIntyre - zeroSteiner GitHub followers

License

The Rule Engine library is released under the BSD 3-Clause license. It is able to be used for both commercial and private purposes. For more information, see the LICENSE file.

Metadata

Release files for rule-engine 5.1.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 rule-engine 5.1.0
File Size Uploaded Provenance Actions
rule_engine-5.1.0.tar.gz 99.7 kB Without provenance Details for rule_engine-5.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rule-engine 5.1.0
File Interpreter ABI Platform Actions
rule_engine-5.1.0-py3-none-any.whl Python 3 none any Details for rule_engine-5.1.0-py3-none-any.whl

Total release size: 239.8 kB

Release files / rule_engine-5.1.0.tar.gz

Download URL rule_engine-5.1.0.tar.gz
Size 99.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a5f7f0a406fd643168c251677499c06a58815b24e728ec5f4cd99fc263c75c1e
BLAKE2b-256 checksum
How to use checksums
56070b312a26b32a4e8a8d046ce44d218d26de84fda43eec4973692c9ca07a43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / rule_engine-5.1.0-py3-none-any.whl

Download URL rule_engine-5.1.0-py3-none-any.whl
Size 140.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32d04cf7a8d37a6b255604fc6fa687a075310506c466ae72a44f9efa551fbb96
BLAKE2b-256 checksum
How to use checksums
918f67ec6718b89402f5fe80f70eacb4cc3ce024137cf579cc15d027dfa1fce5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for rule-engine 5.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

5.1.0 This release

2 release files No provenance

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