All Projects → osantana → dicteval

osantana / dicteval

Licence: MIT license
Evaluate expressions in dict/json objects

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to dicteval

FavouriteAnime-Hacktoberfest
Add your favorite Anime with your name
Stars: ✭ 20 (-23.08%)
Mutual labels:  hacktoberfest2018
Alex
Catch insensitive, inconsiderate writing
Stars: ✭ 4,124 (+15761.54%)
Mutual labels:  hacktoberfest2018
jducers
A js transducers-like implementation using ES9
Stars: ✭ 25 (-3.85%)
Mutual labels:  hacktoberfest2018
algorithms-in-C-Cplusplus-Java-Python-JavaScript
This is a repository created for create a curated list of common search,sorting,insertion,deletion algorithms in C
Stars: ✭ 22 (-15.38%)
Mutual labels:  hacktoberfest2018
nevinha-js
More than just framework... A component framework to make the web animations development easier
Stars: ✭ 25 (-3.85%)
Mutual labels:  hacktoberfest2018
GitMe
A fun workshop exercise for taking first steps on submiting a Pull Request on GitHub
Stars: ✭ 16 (-38.46%)
Mutual labels:  hacktoberfest2018
Hacktoberfest-2018
Make your first Pull Request and earn a free tee from GitHub!
Stars: ✭ 22 (-15.38%)
Mutual labels:  hacktoberfest2018
go-four-in-a-row
A simple command-line implementation of the game `four in the row`
Stars: ✭ 12 (-53.85%)
Mutual labels:  hacktoberfest2018
Decentralized eCom
A decentralized e-commerce platform! Tech Stack: Ethereum (Solidity) on the backend, Web3 binding to ReactJS frontend!
Stars: ✭ 82 (+215.38%)
Mutual labels:  hacktoberfest2018
I-Do-Code
A workspace to sharpen you favourite programming language basics
Stars: ✭ 19 (-26.92%)
Mutual labels:  hacktoberfest2018
operationcode-pybot
Operation Code's Official Slackbot
Stars: ✭ 29 (+11.54%)
Mutual labels:  hacktoberfest2018
Demo-Portfolio-Website
A very simple portfolio website where you can find explanation to every line of code.
Stars: ✭ 59 (+126.92%)
Mutual labels:  hacktoberfest2018
Upwork-coverletter
Hi Guys! I want try to make a cover letter for submit project on upwork . So , lets check and use :)
Stars: ✭ 39 (+50%)
Mutual labels:  hacktoberfest2018
Landing-Page-Animation
Landing page animation made using CSS
Stars: ✭ 45 (+73.08%)
Mutual labels:  hacktoberfest2018
resources api
Flask API for programming and cyber security learning resources
Stars: ✭ 63 (+142.31%)
Mutual labels:  hacktoberfest2018
Programming-Quotes
Repository containing various programming quotes
Stars: ✭ 26 (+0%)
Mutual labels:  hacktoberfest2018
Hello World
Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐
Stars: ✭ 1,464 (+5530.77%)
Mutual labels:  hacktoberfest2018
Guide to contribute in Hacktoberfest
This repository is the guide to contribute in Hacktoberfest
Stars: ✭ 21 (-19.23%)
Mutual labels:  hacktoberfest2018
Hactoberfest-2021
Make your first PR! ~ A beginner friendly repository made specifically for open source beginners. Add your profile, a blog or any program under any language (it can be anything from a hello-world program to a complex data structure algorithm) or update the existing one. Just make sure to add the file under the correct directory. Happy hacking!
Stars: ✭ 134 (+415.38%)
Mutual labels:  hacktoberfest2018
iusegit
Anybody can add his name to this project as an exercise to using git/github
Stars: ✭ 15 (-42.31%)
Mutual labels:  hacktoberfest2018

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.

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

Warning

This functionality will be removed (or changed) in future releases for security reasons.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].