Skip to content

Simple boolean expression evaluation engine for python.

License

Notifications You must be signed in to change notification settings

tailsdotcom/boolrule

Repository files navigation

boolrule

https://readthedocs.org/projects/boolrule/badge/?version=latest

Simple boolean expression evaluation engine.

Features

Compare simple boolean statements:

>>> rule = BoolRule('5 > 3')
>>> rule.test()
True
>>> rule = BoolRule('5 < 3')
>>> rule.test()
False

Evaluate boolean statements against a context dict:

>>> can_buy_beer = BoolRule('user.age_years >= 18')
>>> can_buy_beer.test({'user':{'age_years': 12}})
False
>>> can_buy_beer.test({'user':{'age_years': 20}})
True

Combine conditions with and and or operators to produce complex expressions:

>>> is_hipster = BoolRule('address.postcode.outcode in ("E1","E2") or user.has_beard = true')
>>> address = {
>>>   'postcode': {
>>>      'outcode': 'E1'
>>>   }
>>> }
>>> is_hipster.test({'has_beard': False, 'address': address})
True

Credits

Made possible by the excellent pyparsing library.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.