| 1 |
Metadata-Version: 1.2 |
| 2 |
Name: dicteval |
| 3 |
Version: 0.0.6 |
| 4 |
Summary: Evaluate expressions in dict/json objects |
| 5 |
Home-page: https://github.com/osantana/dicteval |
| 6 |
Author: Osvaldo Santana Neto |
| 7 |
Author-email: [email protected] |
| 8 |
License: MIT |
| 9 |
Description: |
| 10 |
dicteval |
| 11 |
======== |
| 12 |
|
| 13 |
Library to evaluate expressions in dict/json objects. |
| 14 |
|
| 15 |
|
| 16 |
Requirements |
| 17 |
------------ |
| 18 |
|
| 19 |
* Python 3.6+ |
| 20 |
|
| 21 |
|
| 22 |
Basic Usage |
| 23 |
----------- |
| 24 |
|
| 25 |
Module ``dicteval`` will evaluate basic types with no modifications but it will |
| 26 |
evaluate dicts (or json objects) containing keys started with ``=`` (equal) |
| 27 |
symbol as an expression: |
| 28 |
|
| 29 |
>>> from dicteval import dicteval |
| 30 |
>>> dicteval(3) |
| 31 |
3 |
| 32 |
>>> dicteval([3, 5]) |
| 33 |
[3, 5] |
| 34 |
>>> dicteval((5, 3)) |
| 35 |
[5, 3] |
| 36 |
>>> dicteval({"=sum": [3, 5]}) |
| 37 |
8 |
| 38 |
>>> dicteval({"=": 5}) # = symbol alone is a 'nop' function |
| 39 |
5 |
| 40 |
|
| 41 |
You can provide a dictionary with context to be used during evaluation process. |
| 42 |
|
| 43 |
>>> dicteval({"=": "!{var}"}, context={"var": 1.0}) |
| 44 |
1.0 |
| 45 |
|
| 46 |
You can also wrap your string content with ``@{}`` to force a Python ``eval()`` |
| 47 |
with the context provided: |
| 48 |
|
| 49 |
>>> dicteval({"=sum": [3, "@{var + 2}"]}, context={"var": 3}) |
| 50 |
8 |
| 51 |
|
| 52 |
.. warning:: |
| 53 |
This functionality will be removed (or changed) in future releases for |
| 54 |
security reasons. |
| 55 |
|
| 56 |
|
| 57 |
Functions |
| 58 |
--------- |
| 59 |
|
| 60 |
You can use the following builtin functions in your expressions: |
| 61 |
|
| 62 |
|
| 63 |
Function ``=any`` |
| 64 |
''''''''''''''''' |
| 65 |
|
| 66 |
Returns ``True`` if any element of sequence is true. |
| 67 |
|
| 68 |
>>> dicteval({"=any": [1, 2, 3]}) |
| 69 |
True |
| 70 |
>>> dicteval({"=any": [0, 0]}) |
| 71 |
False |
| 72 |
|
| 73 |
|
| 74 |
Function ``=eq`` |
| 75 |
'''''''''''''''' |
| 76 |
|
| 77 |
Returns ``True`` if all elements of sequence are equals: |
| 78 |
|
| 79 |
>>> dicteval({"=eq": [1, 1, 1, 1]}) |
| 80 |
True |
| 81 |
|
| 82 |
|
| 83 |
Function ``=if`` |
| 84 |
'''''''''''''''' |
| 85 |
|
| 86 |
Evaluates condition and returns first value if true, otherwise, returns second value. |
| 87 |
If no false value is supplied, it is assumed to be ``None``. |
| 88 |
|
| 89 |
>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes", "no"]}, context={"var": 6}) |
| 90 |
'yes' |
| 91 |
>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes", "no"]}, context={"var": 4}) |
| 92 |
'no' |
| 93 |
>>> dicteval({"=if": [{"=": "@{var > 5}"}, "yes"]}, context={"var": 4}) |
| 94 |
|
| 95 |
|
| 96 |
Function ``=neq`` |
| 97 |
''''''''''''''''' |
| 98 |
|
| 99 |
Returns ``True`` if elements of sequence are different: |
| 100 |
|
| 101 |
>>> dicteval({"=neq": [1, 1, 1, 5]}) |
| 102 |
True |
| 103 |
|
| 104 |
|
| 105 |
Function ``=`` (or ``nop``) |
| 106 |
''''''''''''''''''''''''''' |
| 107 |
|
| 108 |
Returns the same values passed as arguments: |
| 109 |
|
| 110 |
>>> dicteval({"=": [1, 2, 3, 4]}) |
| 111 |
[1, 2, 3, 4] |
| 112 |
>>> dicteval({"=nop": "spam"}) |
| 113 |
'spam' |
| 114 |
|
| 115 |
|
| 116 |
Function ``=not`` |
| 117 |
''''''''''''''''' |
| 118 |
|
| 119 |
Returns the boolean inverse of argument: |
| 120 |
|
| 121 |
>>> dicteval({"=not": False}) |
| 122 |
True |
| 123 |
>>> dicteval({"=not": True}) |
| 124 |
False |
| 125 |
>>> dicteval({"=not": None}) |
| 126 |
True |
| 127 |
>>> dicteval({"=not": "XYZ"}) |
| 128 |
False |
| 129 |
|
| 130 |
|
| 131 |
Function ``=sum`` |
| 132 |
''''''''''''''''' |
| 133 |
|
| 134 |
Returns a number with the sum of arguments: |
| 135 |
|
| 136 |
>>> dicteval({"=sum": [3, 5]}) |
| 137 |
8 |
| 138 |
|
| 139 |
|
| 140 |
Function ``=mul`` |
| 141 |
''''''''''''''''' |
| 142 |
|
| 143 |
Returns a number with the product of arguments: |
| 144 |
|
| 145 |
>>> dicteval({"=mul": [3, 5]}) |
| 146 |
15 |
| 147 |
|
| 148 |
|
| 149 |
Function ``=all`` |
| 150 |
''''''''''''''''' |
| 151 |
|
| 152 |
Return True if all elements of the iterable are true (or if the iterable is empty) |
| 153 |
|
| 154 |
>>> dicteval({"=all": (True, False)}) |
| 155 |
False |
| 156 |
>>> dicteval({"=all": (True, True)}) |
| 157 |
True |
| 158 |
|
| 159 |
|
| 160 |
Function ``=divmod`` |
| 161 |
'''''''''''''''''''' |
| 162 |
|
| 163 |
Returns a tuple containing the quotient and remainder after division: |
| 164 |
|
| 165 |
>>> dicteval({"=divmod": [8,3]}) |
| 166 |
(2, 2) |
| 167 |
>>> dicteval({"=divmod": [7.5,2.5]}) |
| 168 |
(3.0, 0.0) |
| 169 |
|
| 170 |
|
| 171 |
Function ``=zip`` |
| 172 |
''''''''''''''''' |
| 173 |
|
| 174 |
Return list of aggregate tuples constructed from elements of multiple iterables. |
| 175 |
|
| 176 |
>>> dicteval({"=zip": [[1, 2, 3], [4, 5], [6, 7, 8, 9]]}) |
| 177 |
[(1, 4, 6), (2, 5, 7)] |
| 178 |
|
| 179 |
|
| 180 |
To Do |
| 181 |
----- |
| 182 |
|
| 183 |
- Add more functions to the builtin language |
| 184 |
|
| 185 |
|
| 186 |
Contribute |
| 187 |
---------- |
| 188 |
|
| 189 |
To contribute to `dicteval`: |
| 190 |
|
| 191 |
1. Clone this repository and `cd` into it |
| 192 |
2. Install dev dependencies with [pipenv](https://github.com/pypa/pipenv) |
| 193 |
```bash |
| 194 |
pipenv install --dev |
| 195 |
``` |
| 196 |
3. Create a branch, like `git checkout -b [feature_name]` |
| 197 |
4. Git commit changes |
| 198 |
5. Pull request |
| 199 |
|
| 200 |
|
| 201 |
License |
| 202 |
------- |
| 203 |
|
| 204 |
This software is licensed under MIT license. |
| 205 |
|
| 206 |
Platform: UNKNOWN |
| 207 |
Classifier: License :: OSI Approved :: MIT License |
| 208 |
Classifier: Programming Language :: Python |
| 209 |
Classifier: Programming Language :: Python :: 3 |
| 210 |
Classifier: Programming Language :: Python :: 3.7 |
| 211 |
Classifier: Programming Language :: Python :: Implementation :: CPython |
| 212 |
Classifier: Programming Language :: Python :: Implementation :: PyPy |
| 213 |
Requires-Python: >=3.7.0 |