json-logic-py
=============
This parser accepts `JsonLogic <http://jsonlogic.com>`__ rules and
executes them in Python.
This is a fork of `json-logic-py <https://github.com/nadirizr/json-logic-py>`__ by
`nadir.izr <https://github.com/nadirizr>`__, which is a Python porting of the
GitHub project by `jwadhams <https://github.com/jwadhams>`__ for JavaScript:
`json-logic-js <https://github.com/jwadhams/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 at `JsonLogic.com <http://jsonlogic.com>`__,
including examples of every `supported
operation <http://jsonlogic.com/operations.html>`__ and a place to `try
out rules in your browser <http://jsonlogic.com/play.html>`__.
The same format can also be executed in PHP by the library
`json-logic-php <https://github.com/jwadhams/json-logic-php/>`__
Examples
--------
Simple
~~~~~~
.. code:: python
from json_logic import jsonLogic
jsonLogic( { "==" : [1, 1] } )
# True
This is a simple test, equivalent to ``1 == 1``. A few things about the
format:
1. The operator is always in the "key" position. There is only one key
per JsonLogic rule.
2. The values are typically an array.
3. Each value can be a string, number, boolean, array (non-associative),
or null
Compound
~~~~~~~~
Here we're beginning to nest rules.
.. code:: python
jsonLogic(
{"and" : [
{ ">" : [3,1] },
{ "<" : [1,3] }
] }
)
# True
In an infix language (like Python) this could be written as:
.. code:: python
( (3 > 1) and (1 < 3) )
Data-Driven
~~~~~~~~~~~
Obviously these rules aren't very interesting if they can only take
static literal data. Typically ``jsonLogic`` will be called with a rule
object and a data object. You can use the ``var`` operator to get
attributes of the data object:
.. code:: python
jsonLogic(
{ "var" : ["a"] }, # Rule
{ a : 1, b : 2 } # Data
)
# 1
If you like, we support `syntactic
sugar <https://en.wikipedia.org/wiki/Syntactic_sugar>`__ on unary
operators to skip the array around values:
.. code:: python
jsonLogic(
{ "var" : "a" },
{ a : 1, b : 2 }
)
# 1
You can also use the ``var`` operator to access an array by numeric
index:
.. code:: python
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, *and* filled with apples.
.. code:: python
rules = { "and" : [
{"<" : [ { "var" : "temp" }, 110 ]},
{"==" : [ { "var" : "pie.filling" }, "apple" ] }
] }
data = { "temp" : 100, "pie" : { "filling" : "apple" } }
jsonLogic(rules, data)
# True
Dates
~~~~~
You can use the ``date`` operator to include dates in the json logic. The dates are internally converted to ``datetime.date``
objects, and then the comparison is performed.
.. code:: python
rule = {"<=": [{"date": {"var": "testDate"}}, {"date": "2021-01-01"}]}
data = {"testDate": "2020-01-01"}
jsonLogic(rule, data)
# True
The operator ``{"today": []}`` gets the current date. It is also possible to add/subtract years to a date. This makes use
of ``relativedelta`` from ``dateutils``.
.. code:: python
rule = {"-": [{"date": "2021-01-01"}, {"years": 18}]}
jsonLogic(rule)
# date(2003, 1, 1)
Datetimes
~~~~~~~~~
You can use the ``datetime`` operator to include datetimes in the json logic. The datetimes are internally converted to ``datetime.datetime``
objects, and then the comparison is performed.
.. code:: python
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)
# True
Always and Never
~~~~~~~~~~~~~~~~
Sometimes the rule you want to process is "Always" or "Never." If the
first parameter passed to ``jsonLogic`` is a non-object,
non-associative-array, it is returned immediately.
.. code:: python
#Always
jsonLogic(True, data_will_be_ignored);
# True
#Never
jsonLogic(False, i_wasnt_even_supposed_to_be_here);
# False
Installation
------------
The best way to install this library is via
`PIP <https://pypi.python.org/pypi/>`__:
.. code:: bash
pip install json-logic
If that doesn't suit you, and you want to manage updates yourself, the
entire library is self-contained in ``json_logic.py`` and you can
download it straight into your project as you see fit.
.. code:: bash
curl -O https://raw.githubusercontent.com/nadirizr/json-logic-py/master/json_logic.py
Raw data
{
"_id": null,
"home_page": "https://github.com/maykinmedia/json-logic-py",
"name": "maykin-json-logic-py",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "jsonLogic",
"author": "nadir.izr,maykin",
"author_email": "nadir@soundmindtech.com,support@maykinmedia.nl",
"download_url": "https://files.pythonhosted.org/packages/e1/42/0684249769461ff2c3ca8279015f8924c6e91f40ff678ad48d0a443eb6be/maykin-json-logic-py-0.13.0.tar.gz",
"platform": null,
"description": "json-logic-py\n=============\n\nThis parser accepts `JsonLogic <http://jsonlogic.com>`__ rules and\nexecutes them in Python.\n\nThis is a fork of `json-logic-py <https://github.com/nadirizr/json-logic-py>`__ by\n`nadir.izr <https://github.com/nadirizr>`__, which is a Python porting of the\nGitHub project by `jwadhams <https://github.com/jwadhams>`__ for JavaScript:\n`json-logic-js <https://github.com/jwadhams/json-logic-js>`__.\n\n\nThe JsonLogic format is designed to allow you to share rules (logic)\nbetween front-end and back-end code (regardless of language difference),\neven to store logic along with a record in a database. JsonLogic is\ndocumented extensively at `JsonLogic.com <http://jsonlogic.com>`__,\nincluding examples of every `supported\noperation <http://jsonlogic.com/operations.html>`__ and a place to `try\nout rules in your browser <http://jsonlogic.com/play.html>`__.\n\nThe same format can also be executed in PHP by the library\n`json-logic-php <https://github.com/jwadhams/json-logic-php/>`__\n\nExamples\n--------\n\nSimple\n~~~~~~\n\n.. code:: python\n\n from json_logic import jsonLogic\n jsonLogic( { \"==\" : [1, 1] } )\n # True\n\nThis is a simple test, equivalent to ``1 == 1``. A few things about the\nformat:\n\n1. The operator is always in the \"key\" position. There is only one key\n per JsonLogic rule.\n2. The values are typically an array.\n3. Each value can be a string, number, boolean, array (non-associative),\n or null\n\nCompound\n~~~~~~~~\n\nHere we're beginning to nest rules.\n\n.. code:: python\n\n jsonLogic(\n {\"and\" : [\n { \">\" : [3,1] },\n { \"<\" : [1,3] }\n ] }\n )\n # True\n\nIn an infix language (like Python) this could be written as:\n\n.. code:: python\n\n ( (3 > 1) and (1 < 3) )\n\nData-Driven\n~~~~~~~~~~~\n\nObviously these rules aren't very interesting if they can only take\nstatic literal data. Typically ``jsonLogic`` will be called with a rule\nobject and a data object. You can use the ``var`` operator to get\nattributes of the data object:\n\n.. code:: python\n\n jsonLogic(\n { \"var\" : [\"a\"] }, # Rule\n { a : 1, b : 2 } # Data\n )\n # 1\n\nIf you like, we support `syntactic\nsugar <https://en.wikipedia.org/wiki/Syntactic_sugar>`__ on unary\noperators to skip the array around values:\n\n.. code:: python\n\n jsonLogic(\n { \"var\" : \"a\" },\n { a : 1, b : 2 }\n )\n # 1\n\nYou can also use the ``var`` operator to access an array by numeric\nindex:\n\n.. code:: python\n\n jsonLogic(\n {\"var\" : 1 },\n [ \"apple\", \"banana\", \"carrot\" ]\n )\n # \"banana\"\n\nHere's a complex rule that mixes literals and data. The pie isn't ready\nto eat unless it's cooler than 110 degrees, *and* filled with apples.\n\n.. code:: python\n\n rules = { \"and\" : [\n {\"<\" : [ { \"var\" : \"temp\" }, 110 ]},\n {\"==\" : [ { \"var\" : \"pie.filling\" }, \"apple\" ] }\n ] }\n\n data = { \"temp\" : 100, \"pie\" : { \"filling\" : \"apple\" } }\n\n jsonLogic(rules, data)\n # True\n\nDates\n~~~~~\n\nYou can use the ``date`` operator to include dates in the json logic. The dates are internally converted to ``datetime.date``\nobjects, and then the comparison is performed.\n\n.. code:: python\n\n rule = {\"<=\": [{\"date\": {\"var\": \"testDate\"}}, {\"date\": \"2021-01-01\"}]}\n data = {\"testDate\": \"2020-01-01\"}\n\n jsonLogic(rule, data)\n # True\n\n\nThe operator ``{\"today\": []}`` gets the current date. It is also possible to add/subtract years to a date. This makes use\nof ``relativedelta`` from ``dateutils``.\n\n.. code:: python\n\n rule = {\"-\": [{\"date\": \"2021-01-01\"}, {\"years\": 18}]}\n\n jsonLogic(rule)\n # date(2003, 1, 1)\n\n\nDatetimes\n~~~~~~~~~\n\nYou can use the ``datetime`` operator to include datetimes in the json logic. The datetimes are internally converted to ``datetime.datetime``\nobjects, and then the comparison is performed.\n\n.. code:: python\n\n rule = {\n \"<=\": [\n {\"datetime\": {\"var\": \"testDatetime\"}},\n {\"datetime\": \"2022-12-01T10:00:00.000+02:00\"},\n ]\n }\n data = {\"testDatetime\": \"2022-11-01T10:00:00.000+02:00\"}\n\n jsonLogic(rule, data)\n # True\n\nAlways and Never\n~~~~~~~~~~~~~~~~\n\nSometimes the rule you want to process is \"Always\" or \"Never.\" If the\nfirst parameter passed to ``jsonLogic`` is a non-object,\nnon-associative-array, it is returned immediately.\n\n.. code:: python\n\n #Always\n jsonLogic(True, data_will_be_ignored);\n # True\n\n #Never\n jsonLogic(False, i_wasnt_even_supposed_to_be_here);\n # False\n\nInstallation\n------------\n\nThe best way to install this library is via\n`PIP <https://pypi.python.org/pypi/>`__:\n\n.. code:: bash\n\n pip install json-logic\n\nIf that doesn't suit you, and you want to manage updates yourself, the\nentire library is self-contained in ``json_logic.py`` and you can\ndownload it straight into your project as you see fit.\n\n.. code:: bash\n\n curl -O https://raw.githubusercontent.com/nadirizr/json-logic-py/master/json_logic.py\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Build complex rules, serialize them as JSON, and execute them in Python",
"version": "0.13.0",
"project_urls": {
"Homepage": "https://github.com/maykinmedia/json-logic-py"
},
"split_keywords": [
"jsonlogic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4ec7c62b20afa598c4a71bea59618e97b3392bef498d7043c9261421712cdab6",
"md5": "0aeeeaf5d7b57470f332f2bb5c6c9f17",
"sha256": "ff712855d3bb13e1078d391f0336a3874e0dfb53541ab95c4233f120642b4703"
},
"downloads": -1,
"filename": "maykin_json_logic_py-0.13.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0aeeeaf5d7b57470f332f2bb5c6c9f17",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11204,
"upload_time": "2023-10-06T14:47:22",
"upload_time_iso_8601": "2023-10-06T14:47:22.412235Z",
"url": "https://files.pythonhosted.org/packages/4e/c7/c62b20afa598c4a71bea59618e97b3392bef498d7043c9261421712cdab6/maykin_json_logic_py-0.13.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e1420684249769461ff2c3ca8279015f8924c6e91f40ff678ad48d0a443eb6be",
"md5": "1f91465b3bd4a14493ebb3a5a9c58c14",
"sha256": "4cd508d8fa03b17c2ad8719df1d30c1d0cbda8a0dd4427d11691d2f253eacf0a"
},
"downloads": -1,
"filename": "maykin-json-logic-py-0.13.0.tar.gz",
"has_sig": false,
"md5_digest": "1f91465b3bd4a14493ebb3a5a9c58c14",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17583,
"upload_time": "2023-10-06T14:47:24",
"upload_time_iso_8601": "2023-10-06T14:47:24.206413Z",
"url": "https://files.pythonhosted.org/packages/e1/42/0684249769461ff2c3ca8279015f8924c6e91f40ff678ad48d0a443eb6be/maykin-json-logic-py-0.13.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-06 14:47:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "maykinmedia",
"github_project": "json-logic-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "maykin-json-logic-py"
}