Skip to main content

Rule

A rule engine written in python.

The rule is a json/yaml string or python object of a list expression. The expression is like [op, arg0, arg1, ..., argn], the op is the operator, and arg0..n is the arguments for the operator. Any argument can be another expression.

For writing convenience, the first argument will be tried to resolve as the context parameter. Or, you can just use the special var operator to indicate the context parameter.

Usage

>>> from rule import Rule
>>>
>>> context = dict(a=1, world='hello')
>>> Rule(['=', ['var', 'a'], 1]).match(context)
True
>>> Rule("['=', ['var', 'a'], 1]").match(context)
True
>>> Rule(['=', 'a', 1]).match(context)
True
>>> Rule(['=', 'hello', 'hello']).match(context)
True
>>> Rule(['=', 'world', 'hello']).match(context)
True
>>> Rule(['<', 'a', 10]).match(context)
True
>>> Rule(['=', ['>', 'a', 10], False]).match(context)
True
>>>
>>> context = dict(ldap_id='Harry', hosts='sa,sb,sc', reason='hehe', nologin=False,
...                group='wheel,sysadmin,platform', package='dev-python/sa-tools',
...                branch='release', cc='Tony,Mike',)
>>> Rule(['contains', 'hosts', 'sa,']).match(context)
True
>>> Rule(['contains', ['split', 'hosts', ','], 'sa']).match(context)
True
>>> Rule(['in', 'branch', 'master', 'release']).match(context)
True
>>> Rule(['=', 'ldap_id', 'Harry']).match(context)
True
>>> Rule(['match', 'package', 'dev-python/*']).match(context)
True
>>> Rule(['regex', 'package', 'dev-python/.*']).match(context)
True
>>> Rule(['is', 'nologin', False]).match(context)
True

See rule/op.py for more supported operators.

License

http://tclh123.mit-license.org/

Metadata

Release files for rule 0.1.1

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 0.1.1
File Size Uploaded
rule-0.1.1.tar.gz 3.9 kB Details

Built distribution (wheel)

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

Total release size: 9.1 kB

Release files / rule-0.1.1.tar.gz

Download URL rule-0.1.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dde458f4229b71c5de0c97780f244cfc8d101ac17ac10d2345f3364b091cf961
BLAKE2b-256 checksum
How to use checksums
df33d4d2304f60d8daa63e5952d1739e75b30f89fecda1d82d110e8e9e321293
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release files / rule-0.1.1-py3-none-any.whl

Download URL rule-0.1.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34e5acbde1ff501d883e6f8ad28b077b9b5ff6f4de7f417f02415f5652fcf5ab
BLAKE2b-256 checksum
How to use checksums
57bcaf60966bf257be796c455b108e88ecf8abb16dda1cb6f6a9cbe06482ccd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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