GithubHelp home page GithubHelp logo

dicteval's Introduction

dicteval

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. Context will be used, eg, to evaluate string objects:

>>> dicteval({"=": "var = {var}"}, context={"var": 1})
'var = 1'

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

License

This software is licensed under MIT license.

dicteval's People

Contributors

heyvito avatar osantana avatar randall-fulton avatar ronaldotd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.