Skip to main content

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

Project description

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

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.

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

rule_engine-5.0.0a3.tar.gz (94.8 kB view details)

Uploaded Source

Built Distribution

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

rule_engine-5.0.0a3-py3-none-any.whl (119.4 kB view details)

Uploaded Python 3

File details

Details for the file rule_engine-5.0.0a3.tar.gz.

File metadata

  • Download URL: rule_engine-5.0.0a3.tar.gz
  • Upload date:
  • Size: 94.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rule_engine-5.0.0a3.tar.gz
Algorithm Hash digest
SHA256 a11d287b44da8748ed2d4e4c0dd71a7b1dc6ed93ab32f481a520b843af409a86
MD5 9b9145aa1bf4621e21440eef637977af
BLAKE2b-256 ddbe27e4ec1dec47c70033f715aed090f548ff3a4fbfcfd20ad158d6ac165de1

See more details on using hashes here.

File details

Details for the file rule_engine-5.0.0a3-py3-none-any.whl.

File metadata

  • Download URL: rule_engine-5.0.0a3-py3-none-any.whl
  • Upload date:
  • Size: 119.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rule_engine-5.0.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 b4374785e61a0b2a1f7013307f41c9b5b50369b413bae7d66fa55b6a412c1841
MD5 dde14072a5d4fe0e11436f192dac5648
BLAKE2b-256 3e58796572a2ffaaefea0d11c7a3bbf9e74949db9d6121960c191f1f50441106

See more details on using hashes here.

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