Skip to main content

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

Project description

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

Rule Engine expressions are written in their own language, defined as strings in Python. Some features of this language includes:

  • Optional type hinting

  • Matching strings with regular expressions

  • Datetime datatypes

  • Data attributes

Example

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

Credits

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-2.2.1.tar.gz (27.2 kB view details)

Uploaded Source

File details

Details for the file rule-engine-2.2.1.tar.gz.

File metadata

  • Download URL: rule-engine-2.2.1.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.10

File hashes

Hashes for rule-engine-2.2.1.tar.gz
Algorithm Hash digest
SHA256 184875ed76430719a6ce6b47b08f3d4d32f3d9680ca42c4dcc5c25bc654b609e
MD5 393b1d3ec0d6a06a42016517b42f6db8
BLAKE2b-256 510c4df8e0bfca500ca29871345fce43a88ff27d66f0f01f77fb7698877e6aa3

See more details on using hashes here.

Provenance

Supported by

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