GitXplorerGitXplorer
V

jsonlogic

public
5 stars
0 forks
5 issues

Commits

List of commits on branch main.
Verified
68e1272ee1197c6ba0d9d33752329f07861179b5

Add support for nested operators inside arrays (#25)

VViicos committed 8 months ago
Verified
f60caf6fabb07ec70fbc25af664c345dbb3887fa

Add docs for variables resolving (#24)

VViicos committed 8 months ago
Verified
208f1366dd3e57baec6f3cfb48ef87262d24b697

Fix RTD build, again (#23)

VViicos committed 8 months ago
Verified
cb163fa893edba7161b0711b6a99a85e1e78c962

Fix RTD build (#22)

VViicos committed 8 months ago
Verified
d673bfe44591e597b9b92e4582b84e08ce1cb9ef

Fix typo in README (#21)

VViicos committed 8 months ago
Verified
d96289027c5f28ed703a55c4b276d3bab954fb2d

Implement evaluation of operators (#19)

VViicos committed 8 months ago

README

The README file for this repository.

================ python-jsonlogic

|Pythons| |PyPI| |Ruff|

.. |Pythons| image:: https://img.shields.io/pypi/pyversions/python-jsonlogic.svg :alt: Supported Python versions :target: https://pypi.org/project/python-jsonlogic/

.. |PyPI| image:: https://img.shields.io/pypi/v/python-jsonlogic.svg :alt: PyPI - Version :target: https://pypi.org/project/python-jsonlogic/

.. |Ruff| image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json :alt: PyPI - Version :target: https://github.com/astral-sh/ruff

python-jsonlogic is an extensible and sane implementation of JsonLogic, making use of the JSON Schema specification.

.. _JSON Schema: https://json-schema.org/

Motivation

While the JsonLogic_ format can be great to serialize logic, it lacks a formal specification and some aspects are unclear/unspecified:

  • operators <https://jsonlogic.com/operations.html>_ arguments can take any value. For instance, comparison operators <https://jsonlogic.com/operations.html#---and->_ are said to work with "numeric" values, however the JavaScript playground <https://jsonlogic.com/play.html>_ doesn't validate inputs. It is also convenient to allow such comparison operators for date and datetime objects as well.
  • Operators accessing data <https://jsonlogic.com/operations.html#accessing-data>_ use a dot-like notation, which is ambiguous when dealing with keys such as my.key.
  • Operators such as map <https://jsonlogic.com/operations.html#map-reduce-and-filter>_ provides their own data scope, making it impossible to access higher-level data inside the operator expression.

For these reasons, python-jsonlogic provides a way to typecheck your JSON Logic expressions at "compile" time, before applying input data to them.

.. _JsonLogic: https://jsonlogic.com/

Installation

From PyPI:

.. code:: bash

pip install python-jsonlogic

The library can be imported from the jsonlogic module.

Usage

.. code-block:: python

# 1. Create or use an already existing operator registry:
from jsonlogic.operators import operator_registry

# 2. Parse the JSON Logic expression:
from jsonlogic import JSONLogicExpression

expr = JSONLogicExpression.from_json({"map": [
    [1, 2],
    {"*": [{"var": ""}, {"var": "/my_int@1"}]},
]})

# 3. Create an operator tree:
root_op = expr.as_operator_tree(operator_registry)

# 4. Typecheck the expression:
from jsonlogic.typechecking import typecheck

typ, diagnostics = typecheck(
    root_op,
    data_schema={
      "type": "object",
      "properties": {
            "my_int": {"type": "integer"}
        },
    }
)
print(typ)
#> ArrayType(IntegerType())

# 5. Evaluate with data:
from jsonlogic.evaluation import evaluate
value = evaluate(
    root_op,
    data={"my_int": 2},
    data_schema=None,
)
print(value)
#> [2, 4]