Skip to main content

A rule engine written in python.

Project description

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/

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

Uploaded Source

Built Distribution

rule-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file rule-0.1.1.tar.gz.

File metadata

  • Download URL: rule-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for rule-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dde458f4229b71c5de0c97780f244cfc8d101ac17ac10d2345f3364b091cf961
MD5 b97e9e27ad351cff712a520a9fb9da08
BLAKE2b-256 df33d4d2304f60d8daa63e5952d1739e75b30f89fecda1d82d110e8e9e321293

See more details on using hashes here.

File details

Details for the file rule-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rule-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for rule-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 34e5acbde1ff501d883e6f8ad28b077b9b5ff6f4de7f417f02415f5652fcf5ab
MD5 1c0f4e7d1bc2d5ec7ec5c535229d21ca
BLAKE2b-256 57bcaf60966bf257be796c455b108e88ecf8abb16dda1cb6f6a9cbe06482ccd1

See more details on using hashes here.

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