Introducing boolrule
boolrule is an open source Boolean expression evaluation engine for Python.
We originally built boolrule to evaluate conditional edges between nodes in a graph structure, which we then used to model a dynamic signup flow that changed automatically depending on the state of the pet or customer at a given point. The boolean expressions were essentially configuration data, and the use of a simple DSL means they could be read and edited by someone without a programming background.
We've actually found a multitude of other uses for it since, including using it in our promotion system to apply conditional discounts and in our order generation systems to conditionally include certain items in an order.
Probably the simplest (though not precicely useful) example of boolrule in action is evaluating a static expression:
>>> from boolrule import BoolRule
>>> rule = BoolRule('5 > 3')
>>> rule.test()
True
...but the real power comes when evaluating the boolean expression against a dynamically-generated 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
You can of course combine expressions using standard boolean operators like and
, or
and not
along with parentheses:
>>> 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
If you're interested in finding out more, you can read the documentation, head over to the github repo or just pip install boolrule
and start playing around.
P.S. If you're interested in solving complex technical challenges whilst making a real difference to dogs and their owners, we're hiring. Get in touch or head to tails.com/careers to find out more about the team and what we're up to. We'd also love to hear from you if you end up using boolrule in your projects.