Skip to main content

A tiny, composable, extensible rule engine for Python (Specification + Transformer).

Project description

easy-rule-engine

A tiny, composable rule engine for Python.

  • Specification: composable predicates (&, |, ~)
  • Transformer: immutable transformations (returns a new item)
  • Rule: condition + transform (+ stop_on_match)
  • RuleEngine: applies rules to items (match_mode=all|first, keep_unmatched)

Install

uv add easy-rule-engine

Quick start

from easy_rule_engine import Rule, RuleEngine, attr_spec, attr_transform, dict_setter

users = [
    {"name": "Tom", "age": 15, "status": "UNKNOWN"},
    {"name": "Alice", "age": 20, "status": "OK"},
]

get_age = lambda u: u["age"]
get_status = lambda u: u["status"]

is_minor = attr_spec(get_age, lambda v: v < 18)
set_minor = attr_transform(
    getter=get_status,
    setter=dict_setter("status"),
    value_func=lambda _old: "MINOR",
)

engine = RuleEngine(
    rules=[Rule(condition=is_minor, transform=set_minor, name="minor")],
    keep_unmatched=True,
)

print(engine.process(users))

Examples

Example scripts live in examples/.

Recommended (after syncing the project environment; see Development):

uv run python examples/dict_filter_and_transform.py
uv run python examples/dataclass_transform.py
uv run python examples/order_discounts_and_shipping.py
uv run python examples/risk_scoring_with_priorities.py
uv run python examples/two_phase_pipeline.py

Development

Create/update the project environment (recommended; installs the project editable by default):

uv sync

Run unit tests:

uv run python -m unittest -q

If you want to disable editable installs (not recommended for development):

uv sync --no-editable

If you prefer not to install, you can still run from source with:

PYTHONPATH=src uv run python -m unittest -q
PYTHONPATH=src uv run python examples/dict_filter_and_transform.py

Build distributions (sdist + wheel):

uv pip install -U build
uv run python -m build

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

easy_rule_engine-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

easy_rule_engine-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file easy_rule_engine-0.1.0.tar.gz.

File metadata

  • Download URL: easy_rule_engine-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for easy_rule_engine-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7c04d6c9a19a67c1aca8f19106e3225646d3dcd8cfb0b5aa5564223668060a4b
MD5 b0b03c92ab607342e555e536a3ec3910
BLAKE2b-256 86f13c9c506efee671b732a0c8da8db74b89677d05a33300eb83c89069bd9aa1

See more details on using hashes here.

File details

Details for the file easy_rule_engine-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_rule_engine-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 334f6294ba3ce5e723dd1ebb5637ce0204852b2e963672d27673f7d48726bddb
MD5 2609029e816a73945076a125970cabc1
BLAKE2b-256 efaaaa6c6cef1c1a98312671af2de557daefffeb4814bf346d2cee7ed3f4a0a9

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