Skip to main content

Evaluate expressions in dict/json objects

Project description

Library to evaluate expressions in dict/json objects.

Requirements

  • Python 3.6+

Basic Usage

Module dicteval will evaluate basic types with no modifications but it will evaluate dicts (or json objects) containing keys started with = (equal) symbol as an expression:

>>> from dicteval import dicteval
>>> dicteval(3)
3
>>> dicteval([3, 5])
[3, 5]
>>> dicteval((5, 3))
[5, 3]
>>> dicteval({"=sum": [3, 5]})
8
>>> dicteval({"=": 5})  # = symbol alone is a 'nop' function
5

You can provide a dictionary with context to be used during evaluation process.

>>> dicteval({"=": "!{var}"}, context={"var": 1.0})
1.0

You can also wrap your string content with @{} to force a Python eval() with the context provided:

>>> dicteval({"=sum": [3, "@{var + 2}"]}, context={"var": 3})
8

Functions

You can use the following builtin functions in your expressions:

Function =any

Returns True if any element of sequence is true.

>>> dicteval({"=any": [1, 2, 3]})
True
>>> dicteval({"=any": [0, 0]})
False

Function =eq

Returns True if all elements of sequence are equals:

>>> dicteval({"=eq": [1, 1, 1, 1]})
True

Function =if

Evaluates condition and returns first value if true, otherwise, returns second value. If no false value is supplied, it is assumed to be None.

>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes", "no"]}, context={"var": 6})
'yes'
>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes", "no"]}, context={"var": 4})
'no'
>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes"]}, context={"var": 4})

Function =neq

Returns True if elements of sequence are different:

>>> dicteval({"=neq": [1, 1, 1, 5]})
True

Function = (or nop)

Returns the same values passed as arguments:

>>> dicteval({"=": [1, 2, 3, 4]})
[1, 2, 3, 4]
>>> dicteval({"=nop": "spam"})
'spam'

Function =not

Returns the boolean inverse of argument:

>>> dicteval({"=not": False})
True
>>> dicteval({"=not": True})
False
>>> dicteval({"=not": None})
True
>>> dicteval({"=not": "XYZ"})
False

Function =sum

Returns a number with the sum of arguments:

>>> dicteval({"=sum": [3, 5]})
8

Function =mul

Returns a number with the product of arguments:

>>> dicteval({"=mul": [3, 5]})
15

Function =all

Return True if all elements of the iterable are true (or if the iterable is empty)

>>> dicteval({"=all": (True, False)})
False
>>> dicteval({"=all": (True, True)})
True

Function =divmod

Returns a tuple containing the quotient and remainder after division:

>>> dicteval({"=divmod": [8,3]})
(2, 2)
>>> dicteval({"=divmod": [7.5,2.5]})
(3.0, 0.0)

Function =zip

Return list of aggregate tuples constructed from elements of multiple iterables.

>>> dicteval({"=zip": [[1, 2, 3], [4, 5], [6, 7, 8, 9]]})
[(1, 4, 6), (2, 5, 7)]

To Do

  • Add more functions to the builtin language

Contribute

To contribute to dicteval:

  1. Clone this repository and cd into it

  2. Install dev dependencies with [pipenv](https://github.com/pypa/pipenv) `bash pipenv install --dev `

  3. Create a branch, like git checkout -b [feature_name]

  4. Git commit changes

  5. Pull request

License

This software is licensed under MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dicteval-0.0.6.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

dicteval-0.0.6-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file dicteval-0.0.6.tar.gz.

File metadata

  • Download URL: dicteval-0.0.6.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for dicteval-0.0.6.tar.gz
Algorithm Hash digest
SHA256 20ab0af2ac567f970ed82aadd9fcdbb63695a18be4cda112029b0fc4d1ed6933
MD5 f6939600190005f50d6d037d77010f98
BLAKE2b-256 fdaa48e49200798686d16aae8ecbb120f640ddc0d52ceb5a8be2616b3f50a43b

See more details on using hashes here.

File details

Details for the file dicteval-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: dicteval-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for dicteval-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8172c80b3ed259d18d4c622bb45b8a9e5fcc67845135e01619f415efb98ae87d
MD5 fed2ff46255d4b9880ae506fae653b0d
BLAKE2b-256 f7069b69ad3d9ed100f849e4d212fd7c0d1d47a4b2129f170a261ed86661a550

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page