Package: maykin-json-logic-py
This parser acceptsJsonLogicrules and
executes them in Python.This is a fork ofjson-logic-pybynadir.izr, which is a Python porting of the
GitHub project byjwadhamsfor JavaScript:json-logic-js.The JsonLogic format is designed to allow you to share rules (logic)
between front-end and back-end code (regardless of language difference),
even to store logic along with a record in a database. JsonLogic is
documented extensively atJsonLogic.com,
including examples of everysupported
operationand a place totry
out rules in your browser.The same format can also be executed in PHP by the libraryjson-logic-phpExamplesSimplefromjson_logicimportjsonLogicjsonLogic({"==":[1,1]})# TrueThis is a simple test, equivalent to1 == 1. A few things about the
format:The operator is always in the “key” position. There is only one key
per JsonLogic rule.The values are typically an array.Each value can be a string, number, boolean, array (non-associative),
or nullCompoundHere we’re beginning to nest rules.jsonLogic({"and":[{">":[3,1]},{"<":[1,3]}]})# TrueIn an infix language (like Python) this could be written as:((3>1)and(1<3))Data-DrivenObviously these rules aren’t very interesting if they can only take
static literal data. TypicallyjsonLogicwill be called with a rule
object and a data object. You can use thevaroperator to get
attributes of the data object:jsonLogic({"var":["a"]},# Rule{a:1,b:2}# Data)# 1If you like, we supportsyntactic
sugaron unary
operators to skip the array around values:jsonLogic({"var":"a"},{a:1,b:2})# 1You can also use thevaroperator to access an array by numeric
index:jsonLogic({"var":1},["apple","banana","carrot"])# "banana"Here’s a complex rule that mixes literals and data. The pie isn’t ready
to eat unless it’s cooler than 110 degrees,andfilled with apples.rules={"and":[{"<":[{"var":"temp"},110]},{"==":[{"var":"pie.filling"},"apple"]}]}data={"temp":100,"pie":{"filling":"apple"}}jsonLogic(rules,data)# TrueDatesYou can use thedateoperator to include dates in the json logic. The dates are internally converted todatetime.dateobjects, and then the comparison is performed.rule={"<=":[{"date":{"var":"testDate"}},{"date":"2021-01-01"}]}data={"testDate":"2020-01-01"}jsonLogic(rule,data)# TrueThe operator{"today": []}gets the current date. It is also possible to add/subtract years to a date. This makes use
ofrelativedeltafromdateutils.rule={"-":[{"date":"2021-01-01"},{"years":18}]}jsonLogic(rule)# date(2003, 1, 1)DatetimesYou can use thedatetimeoperator to include datetimes in the json logic. The datetimes are internally converted todatetime.datetimeobjects, and then the comparison is performed.rule={"<=":[{"datetime":{"var":"testDatetime"}},{"datetime":"2022-12-01T10:00:00.000+02:00"},]}data={"testDatetime":"2022-11-01T10:00:00.000+02:00"}jsonLogic(rule,data)# TrueAlways and NeverSometimes the rule you want to process is “Always” or “Never.” If the
first parameter passed tojsonLogicis a non-object,
non-associative-array, it is returned immediately.#AlwaysjsonLogic(True,data_will_be_ignored);# True#NeverjsonLogic(False,i_wasnt_even_supposed_to_be_here);# FalseInstallationThe best way to install this library is viaPIP:pipinstalljson-logicIf that doesn’t suit you, and you want to manage updates yourself, the
entire library is self-contained injson_logic.pyand you can
download it straight into your project as you see fit.curl-Ohttps://raw.githubusercontent.com/nadirizr/json-logic-py/master/json_logic.py