json_logic_qubit


Namejson_logic_qubit JSON
Version 0.9.1 PyPI version JSON
download
home_pagehttps://github.com/qubitproducts/json-logic-py
SummaryBuild complex rules, serialize them as JSON, and execute them in Python
upload_time2018-08-15 14:41:05
maintainer
docs_urlNone
authormohsin.niazi
requires_python
licenseMIT
keywords json-logic
VCS
bugtrack_url
requirements pandoc pkginfo requests requests-toolbelt sh six twine wheel
Travis-CI
coveralls test coverage No coveralls.
            json-logic-py
=============

This parser accepts `JsonLogic <http://jsonlogic.com>`__ rules and
executes them in Python.

This is a Python porting of the excellent GitHub project by
`jwadhams <https://github.com/jwadhams>`__ for JavaScript:
`json-logic-js <https://github.com/jwadhams/json-logic-js>`__.

All credit goes to him, this is simply an implementation of the same
logic in Python (small differences below).

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

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-qubit

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/qubitproducts/json-logic-py",
    "name": "json_logic_qubit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "json-logic",
    "author": "mohsin.niazi",
    "author_email": "mohsin.niazi@qubit.com",
    "download_url": "",
    "platform": "",
    "description": "json-logic-py\n=============\n\nThis parser accepts `JsonLogic <http://jsonlogic.com>`__ rules and\nexecutes them in Python.\n\nThis is a Python porting of the excellent GitHub project by\n`jwadhams <https://github.com/jwadhams>`__ for JavaScript:\n`json-logic-js <https://github.com/jwadhams/json-logic-js>`__.\n\nAll credit goes to him, this is simply an implementation of the same\nlogic in Python (small differences below).\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\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-qubit\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Build complex rules, serialize them as JSON, and execute them in Python",
    "version": "0.9.1",
    "split_keywords": [
        "json-logic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d8622b023e1dcc6917d40a9f5687916d1c647e26af06c579f200bd0f3b91481a",
                "md5": "e5ab212bec0493fbe4e02816726c0225",
                "sha256": "d024ff0c77659eb97ddf742e0bd1b7caacd44bc53fdfc8f3977f4062a3c3b056"
            },
            "downloads": -1,
            "filename": "json_logic_qubit-0.9.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e5ab212bec0493fbe4e02816726c0225",
            "packagetype": "bdist_wheel",
            "python_version": "3.6",
            "requires_python": null,
            "size": 13196,
            "upload_time": "2018-08-15T14:41:05",
            "upload_time_iso_8601": "2018-08-15T14:41:05.867938Z",
            "url": "https://files.pythonhosted.org/packages/d8/62/2b023e1dcc6917d40a9f5687916d1c647e26af06c579f200bd0f3b91481a/json_logic_qubit-0.9.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-08-15 14:41:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "qubitproducts",
    "github_project": "json-logic-py",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pandoc",
            "specs": [
                [
                    "==",
                    "1.0.0"
                ]
            ]
        },
        {
            "name": "pkginfo",
            "specs": [
                [
                    "==",
                    "1.2.1"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.8.1"
                ]
            ]
        },
        {
            "name": "requests-toolbelt",
            "specs": [
                [
                    "==",
                    "0.5.0"
                ]
            ]
        },
        {
            "name": "sh",
            "specs": [
                [
                    "==",
                    "1.11"
                ]
            ]
        },
        {
            "name": "six",
            "specs": [
                [
                    "==",
                    "1.11.0"
                ]
            ]
        },
        {
            "name": "twine",
            "specs": [
                [
                    "==",
                    "1.6.4"
                ]
            ]
        },
        {
            "name": "wheel",
            "specs": [
                [
                    "==",
                    "0.26.0"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "json_logic_qubit"
}
        
Elapsed time: 0.06350s