Rules Engine
A simple, generic rules engine. Inspired by Martin Fowler's 2009 blog post.
This package was extracted from Funnel's production code and made open source.
For the story behind its inception, raison d'être, and overall concept, you can watch the talk Rules Rule held by Lennart Fridén at PyCon Sweden 2021.
Installation
Install the package funnel_rules_engine version 1.3+ from PyPi.
The recommended requirements.txt line is funnel_rules_engine~=1.3.
Current Functionality
Rule
A combination of a condition and an action, both of which are callables such as functions or lambdas. The callables must have an arity of one. If the conditional returns true given a single object, the action will be called with the same object as its argument.
When creating a rule, it can also be tagged with a list of tags (see RulesEngine.reject and RulesEngine.select).
Otherwise
Special case of Rule where the action always fires. Suitable as a catch-all last rule.
NoAction
Special case of Rule where None is returned if the condition is met. Useful for halting the execution of .run()
RulesEngine
A generic rules engine that accepts a list of Rules and some input to apply the rules to. The rules engine can either apply the first rule that matches (run) or all the rules that matches (run_all). In addition, rules can be evaluated and executed in parallel (run_all_in_parallel). The latter two cases can optionally be lazily executed by returning a generator rather than a list as the result.
For more on rules engines, see Martin Fowler's blog post.
run
Only apply the first rule that matches and return its result. This is comparable to the behaviour of a structured switch statement or an arbitrary conditional statement.
run_all
Apply all rules that match. The result is returned as a list, or as a generator if the optional parameter lazy is passed as True.
run_all_in_parallel
Evaluate and apply all rules in parallel. The result is returned as a list, or as a generator if the optional parameter lazy is passed as True.
select and reject
These methods return a new rules engine containing the rules matching/not matching the given tags.
when and then
These are convenience functions for creating simple conditions and actions. They both accept a single value that in the case of when will be used to check equality with the passed state and in the case of then will be returned, ignoring the passed state.
Metadata
Release files for funnel-rules-engine 1.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| funnel_rules_engine-1.3.1.tar.gz | 4.7 kB | Without provenance | Details for funnel_rules_engine-1.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| funnel_rules_engine-1.3.1-py3-none-any.whl | Python 3 | none | any | Details for funnel_rules_engine-1.3.1-py3-none-any.whl |
Total release size: 9.7 kB
Release files / funnel_rules_engine-1.3.1.tar.gz
| Download URL | funnel_rules_engine-1.3.1.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5e8a9c601103de0e4494fdc5cf8ab191f4ce27ad67cc4b4de003ae6d19fbfd8
|
|
BLAKE2b-256 checksum How to use checksums |
029867a6b67bbecbfeec5d683d9e54e1fae5af66a2853eb3a553b528e201c686
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Release files / funnel_rules_engine-1.3.1-py3-none-any.whl
| Download URL | funnel_rules_engine-1.3.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
738992873bbcbb7fe640f6c360e2e5d780bda631b3f2a6bea6a05debe05e433a
|
|
BLAKE2b-256 checksum How to use checksums |
6dff5db870669864dc407dd56095297576498fb35d8ca8875ab6414064a1e23a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Security for funnel-rules-engine 1.3.1
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.