simpleeval 1.0.0__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {simpleeval-1.0.0 → simpleeval-1.0.2}/PKG-INFO +12 -13
- {simpleeval-1.0.0 → simpleeval-1.0.2}/README.rst +4 -4
- simpleeval-1.0.2/pyproject.toml +78 -0
- simpleeval-1.0.0/MANIFEST.in +0 -3
- simpleeval-1.0.0/Makefile +0 -31
- simpleeval-1.0.0/pyproject.toml +0 -33
- simpleeval-1.0.0/setup.cfg +0 -29
- simpleeval-1.0.0/setup.py +0 -3
- simpleeval-1.0.0/simpleeval.egg-info/PKG-INFO +0 -501
- simpleeval-1.0.0/simpleeval.egg-info/SOURCES.txt +0 -14
- simpleeval-1.0.0/simpleeval.egg-info/dependency_links.txt +0 -1
- simpleeval-1.0.0/simpleeval.egg-info/top_level.txt +0 -1
- {simpleeval-1.0.0 → simpleeval-1.0.2}/.gitignore +0 -0
- {simpleeval-1.0.0 → simpleeval-1.0.2}/LICENCE +0 -0
- {simpleeval-1.0.0 → simpleeval-1.0.2}/simpleeval.py +0 -0
- {simpleeval-1.0.0 → simpleeval-1.0.2}/test_simpleeval.py +0 -0
|
@@ -1,35 +1,34 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: simpleeval
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: A simple, safe single expression evaluator library.
|
|
5
|
-
|
|
6
|
-
Author: Daniel Fairhead
|
|
7
|
-
|
|
8
|
-
License:
|
|
9
|
-
Keywords: eval,
|
|
5
|
+
Project-URL: Source code, https://github.com/danthedeckie/simpleeval
|
|
6
|
+
Author-email: Daniel Fairhead <danthedeckie@gmail.com>
|
|
7
|
+
Maintainer-email: Daniel Fairhead <danthedeckie@gmail.com>
|
|
8
|
+
License-File: LICENCE
|
|
9
|
+
Keywords: ast,eval,expression,parse,simple
|
|
10
10
|
Classifier: Development Status :: 5 - Production/Stable
|
|
11
11
|
Classifier: Intended Audience :: Developers
|
|
12
12
|
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
13
|
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
15
|
Requires-Python: >=3.9
|
|
16
16
|
Description-Content-Type: text/x-rst
|
|
17
|
-
License-File: LICENCE
|
|
18
17
|
|
|
19
18
|
simpleeval (Simple Eval)
|
|
20
19
|
========================
|
|
21
20
|
|
|
22
|
-
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=
|
|
21
|
+
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=main
|
|
23
22
|
:target: https://github.com/danthedeckie/simpleeval/actions/
|
|
24
23
|
:alt: Build Status
|
|
25
24
|
|
|
26
|
-
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/
|
|
25
|
+
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/main/graph/badge.svg?token=isRnN1yrca
|
|
27
26
|
:target: https://codecov.io/gh/danthedeckie/simpleeval
|
|
28
27
|
:alt: Code Coverage Status
|
|
29
28
|
|
|
30
|
-
.. |pypi-version| image:: https://
|
|
29
|
+
.. |pypi-version| image:: https://img.shields.io/pypi/v/simpleeval
|
|
31
30
|
:target: https://badge.fury.io/py/simpleeval
|
|
32
|
-
:alt: PyPI Version
|
|
31
|
+
:alt: PyPI - Version
|
|
33
32
|
|
|
34
33
|
.. |python-versions| image:: https://img.shields.io/badge/python-3.9_%7C_3.10_%7C_3.11_%7C_3.12_%7C_3.13_%7C_PyPy3.9_%7C_PyPy3.10-blue
|
|
35
34
|
:alt: Static Badge
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
simpleeval (Simple Eval)
|
|
2
2
|
========================
|
|
3
3
|
|
|
4
|
-
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=
|
|
4
|
+
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=main
|
|
5
5
|
:target: https://github.com/danthedeckie/simpleeval/actions/
|
|
6
6
|
:alt: Build Status
|
|
7
7
|
|
|
8
|
-
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/
|
|
8
|
+
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/main/graph/badge.svg?token=isRnN1yrca
|
|
9
9
|
:target: https://codecov.io/gh/danthedeckie/simpleeval
|
|
10
10
|
:alt: Code Coverage Status
|
|
11
11
|
|
|
12
|
-
.. |pypi-version| image:: https://
|
|
12
|
+
.. |pypi-version| image:: https://img.shields.io/pypi/v/simpleeval
|
|
13
13
|
:target: https://badge.fury.io/py/simpleeval
|
|
14
|
-
:alt: PyPI Version
|
|
14
|
+
:alt: PyPI - Version
|
|
15
15
|
|
|
16
16
|
.. |python-versions| image:: https://img.shields.io/badge/python-3.9_%7C_3.10_%7C_3.11_%7C_3.12_%7C_3.13_%7C_PyPy3.9_%7C_PyPy3.10-blue
|
|
17
17
|
:alt: Static Badge
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "simpleeval"
|
|
3
|
+
version = "1.0.2"
|
|
4
|
+
requires-python = ">=3.9"
|
|
5
|
+
readme = "README.rst"
|
|
6
|
+
description = "A simple, safe single expression evaluator library."
|
|
7
|
+
licence = "MIT"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Daniel Fairhead", email = "danthedeckie@gmail.com" }
|
|
10
|
+
]
|
|
11
|
+
maintainers = [
|
|
12
|
+
{ name = "Daniel Fairhead", email = "danthedeckie@gmail.com" }
|
|
13
|
+
]
|
|
14
|
+
keywords = [
|
|
15
|
+
"eval", "simple", "expression", "parse", "ast"
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 5 - Production/Stable",
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
"Programming Language :: Python",
|
|
24
|
+
]
|
|
25
|
+
dependencies = []
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
"Source code" = "https://github.com/danthedeckie/simpleeval"
|
|
29
|
+
|
|
30
|
+
[pycodestyle]
|
|
31
|
+
max_line_length = 99
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["hatchling"]
|
|
35
|
+
build-backend = "hatchling.build"
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.sdist]
|
|
38
|
+
include = [
|
|
39
|
+
"simpleeval.py",
|
|
40
|
+
"test_simpleeval.py",
|
|
41
|
+
"README.rst",
|
|
42
|
+
"LICENCE",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.hatch.version]
|
|
46
|
+
path = "simpleeval.py"
|
|
47
|
+
|
|
48
|
+
[tool.hatch.envs.hatch-test]
|
|
49
|
+
default-args = ["test_simpleeval.py"]
|
|
50
|
+
|
|
51
|
+
[tool.black]
|
|
52
|
+
line-length = 99
|
|
53
|
+
target-version = ['py310']
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 99
|
|
57
|
+
|
|
58
|
+
[tool.isort]
|
|
59
|
+
combine_as_imports = true
|
|
60
|
+
float_to_top = true
|
|
61
|
+
line_length = 99
|
|
62
|
+
include_trailing_comma = true
|
|
63
|
+
force_grid_wrap = 0
|
|
64
|
+
multi_line_output = 3
|
|
65
|
+
|
|
66
|
+
[tool.pylint.messages_control]
|
|
67
|
+
disable = [
|
|
68
|
+
"fixme",
|
|
69
|
+
"consider-using-f-string",
|
|
70
|
+
"raise-missing-from",
|
|
71
|
+
"invalid-name",
|
|
72
|
+
"too-few-public-methods",
|
|
73
|
+
"too-many-public-methods",
|
|
74
|
+
"super-with-arguments",
|
|
75
|
+
"useless-object-inheritance",
|
|
76
|
+
"unnecessary-pass",
|
|
77
|
+
"bad-super-call",
|
|
78
|
+
]
|
simpleeval-1.0.0/MANIFEST.in
DELETED
simpleeval-1.0.0/Makefile
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
test:
|
|
2
|
-
python -Werror test_simpleeval.py
|
|
3
|
-
|
|
4
|
-
autotest:
|
|
5
|
-
find . -name \*.py -not -path .\/.v\* | entr make test
|
|
6
|
-
|
|
7
|
-
.PHONY: test
|
|
8
|
-
|
|
9
|
-
dist/: setup.cfg simpleeval.py README.rst
|
|
10
|
-
python -m build
|
|
11
|
-
twine check dist/*
|
|
12
|
-
|
|
13
|
-
pypi: test dist/
|
|
14
|
-
twine check dist/*
|
|
15
|
-
twine upload dist/*
|
|
16
|
-
|
|
17
|
-
clean:
|
|
18
|
-
rm -rf build
|
|
19
|
-
rm -rf dist
|
|
20
|
-
|
|
21
|
-
coverage:
|
|
22
|
-
coverage run test_simpleeval.py
|
|
23
|
-
|
|
24
|
-
lint:
|
|
25
|
-
ruff check simpleeval.py test_simpleeval.py
|
|
26
|
-
ruff format --check simpleeval.py test_simpleeval.py
|
|
27
|
-
mypy simpleeval.py test_simpleeval.py
|
|
28
|
-
|
|
29
|
-
format:
|
|
30
|
-
ruff check --fix-only simpleeval.py test_simpleeval.py
|
|
31
|
-
ruff format simpleeval.py test_simpleeval.py
|
simpleeval-1.0.0/pyproject.toml
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["setuptools>=30.3.0", "wheel"]
|
|
3
|
-
build-backend = "setuptools.build_meta"
|
|
4
|
-
|
|
5
|
-
[tool.black]
|
|
6
|
-
line-length = 99
|
|
7
|
-
target-version = ['py310']
|
|
8
|
-
|
|
9
|
-
[tool.ruff]
|
|
10
|
-
line-length = 99
|
|
11
|
-
|
|
12
|
-
[tool.isort]
|
|
13
|
-
combine_as_imports = true
|
|
14
|
-
float_to_top = true
|
|
15
|
-
line_length = 99
|
|
16
|
-
include_trailing_comma = true
|
|
17
|
-
force_grid_wrap = 0
|
|
18
|
-
multi_line_output = 3
|
|
19
|
-
|
|
20
|
-
[tool.pylint.messages_control]
|
|
21
|
-
disable = [
|
|
22
|
-
"fixme",
|
|
23
|
-
"consider-using-f-string",
|
|
24
|
-
"raise-missing-from",
|
|
25
|
-
"invalid-name",
|
|
26
|
-
"too-few-public-methods",
|
|
27
|
-
"too-many-public-methods",
|
|
28
|
-
"super-with-arguments",
|
|
29
|
-
"useless-object-inheritance",
|
|
30
|
-
"unnecessary-pass",
|
|
31
|
-
"bad-super-call",
|
|
32
|
-
]
|
|
33
|
-
|
simpleeval-1.0.0/setup.cfg
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
[metadata]
|
|
2
|
-
name = simpleeval
|
|
3
|
-
version = 1.0.0
|
|
4
|
-
author = Daniel Fairhead
|
|
5
|
-
author_email = danthedeckie@gmail.com
|
|
6
|
-
description = A simple, safe single expression evaluator library.
|
|
7
|
-
keywords = eval, simple, expression, parse, ast
|
|
8
|
-
url = https://github.com/danthedeckie/simpleeval
|
|
9
|
-
license = MIT
|
|
10
|
-
long_description = file: README.rst
|
|
11
|
-
long_description_content_type = text/x-rst
|
|
12
|
-
classifiers =
|
|
13
|
-
Development Status :: 5 - Production/Stable
|
|
14
|
-
Intended Audience :: Developers
|
|
15
|
-
License :: OSI Approved :: MIT License
|
|
16
|
-
Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
-
Programming Language :: Python
|
|
18
|
-
|
|
19
|
-
[options]
|
|
20
|
-
py_modules = simpleeval
|
|
21
|
-
python_requires = >=3.9
|
|
22
|
-
|
|
23
|
-
[pycodestyle]
|
|
24
|
-
max_line_length = 99
|
|
25
|
-
|
|
26
|
-
[egg_info]
|
|
27
|
-
tag_build =
|
|
28
|
-
tag_date = 0
|
|
29
|
-
|
simpleeval-1.0.0/setup.py
DELETED
|
@@ -1,501 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: simpleeval
|
|
3
|
-
Version: 1.0.0
|
|
4
|
-
Summary: A simple, safe single expression evaluator library.
|
|
5
|
-
Home-page: https://github.com/danthedeckie/simpleeval
|
|
6
|
-
Author: Daniel Fairhead
|
|
7
|
-
Author-email: danthedeckie@gmail.com
|
|
8
|
-
License: MIT
|
|
9
|
-
Keywords: eval,simple,expression,parse,ast
|
|
10
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
-
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
-
Classifier: Programming Language :: Python
|
|
15
|
-
Requires-Python: >=3.9
|
|
16
|
-
Description-Content-Type: text/x-rst
|
|
17
|
-
License-File: LICENCE
|
|
18
|
-
|
|
19
|
-
simpleeval (Simple Eval)
|
|
20
|
-
========================
|
|
21
|
-
|
|
22
|
-
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=gh-actions-build
|
|
23
|
-
:target: https://github.com/danthedeckie/simpleeval/actions/
|
|
24
|
-
:alt: Build Status
|
|
25
|
-
|
|
26
|
-
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/master/graph/badge.svg?token=isRnN1yrca
|
|
27
|
-
:target: https://codecov.io/gh/danthedeckie/simpleeval
|
|
28
|
-
:alt: Code Coverage Status
|
|
29
|
-
|
|
30
|
-
.. |pypi-version| image:: https://badge.fury.io/py/simpleeval.svg
|
|
31
|
-
:target: https://badge.fury.io/py/simpleeval
|
|
32
|
-
:alt: PyPI Version
|
|
33
|
-
|
|
34
|
-
.. |python-versions| image:: https://img.shields.io/badge/python-3.9_%7C_3.10_%7C_3.11_%7C_3.12_%7C_3.13_%7C_PyPy3.9_%7C_PyPy3.10-blue
|
|
35
|
-
:alt: Static Badge
|
|
36
|
-
|
|
37
|
-
.. |pypi-monthly-downloads| image:: https://img.shields.io/pypi/dm/SimpleEval
|
|
38
|
-
:alt: PyPI - Downloads
|
|
39
|
-
|
|
40
|
-
.. |formatting-with-ruff| image:: https://img.shields.io/badge/-ruff-black?logo=lightning&logoColor=%2300ff00&link=https%3A%2F%2Fdocs.astral.sh%2Fruff%2F
|
|
41
|
-
:alt: Static Badge
|
|
42
|
-
|
|
43
|
-
|build-status| |code-coverage| |pypi-version| |python-versions| |pypi-monthly-downloads| |formatting-with-ruff|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
A single file library for easily adding evaluatable expressions into
|
|
47
|
-
python projects. Say you want to allow a user to set an alarm volume, which
|
|
48
|
-
could depend on the time of day, alarm level, how many previous alarms had gone
|
|
49
|
-
off, and if there is music playing at the time.
|
|
50
|
-
|
|
51
|
-
Or if you want to allow simple formulae in a web application, but don't want to
|
|
52
|
-
give full eval() access, or don't want to run in javascript on the client side.
|
|
53
|
-
|
|
54
|
-
It's deliberately trying to stay simple to use and not have millions of features,
|
|
55
|
-
pull it in from PyPI (pip or easy_install), or even just a single file you can dump
|
|
56
|
-
into a project.
|
|
57
|
-
|
|
58
|
-
Internally, it's using the amazing python ``ast`` module to parse the
|
|
59
|
-
expression, which allows very fine control of what is and isn't allowed. It
|
|
60
|
-
should be completely safe in terms of what operations can be performed by the
|
|
61
|
-
expression.
|
|
62
|
-
|
|
63
|
-
The only issue I know to be aware of is that you can create an expression which
|
|
64
|
-
takes a long time to evaluate, or which evaluating requires an awful lot of
|
|
65
|
-
memory, which leaves the potential for DOS attacks. There is basic protection
|
|
66
|
-
against this, and you can lock it down further if you desire. (see the
|
|
67
|
-
Operators_ section below)
|
|
68
|
-
|
|
69
|
-
You should be aware of this when deploying in a public setting.
|
|
70
|
-
|
|
71
|
-
The defaults are pretty locked down and basic, and it's easy to add
|
|
72
|
-
whatever extra specific functionality you need (your own functions,
|
|
73
|
-
variable/name lookup, etc).
|
|
74
|
-
|
|
75
|
-
Basic Usage
|
|
76
|
-
-----------
|
|
77
|
-
|
|
78
|
-
To get very simple evaluating:
|
|
79
|
-
|
|
80
|
-
.. code-block:: python
|
|
81
|
-
|
|
82
|
-
from simpleeval import simple_eval
|
|
83
|
-
|
|
84
|
-
simple_eval("21 + 21")
|
|
85
|
-
|
|
86
|
-
returns ``42``.
|
|
87
|
-
|
|
88
|
-
Expressions can be as complex and convoluted as you want:
|
|
89
|
-
|
|
90
|
-
.. code-block:: python
|
|
91
|
-
|
|
92
|
-
simple_eval("21 + 19 / 7 + (8 % 3) ** 9")
|
|
93
|
-
|
|
94
|
-
returns ``535.714285714``.
|
|
95
|
-
|
|
96
|
-
You can add your own functions in as well.
|
|
97
|
-
|
|
98
|
-
.. code-block:: python
|
|
99
|
-
|
|
100
|
-
simple_eval("square(11)", functions={"square": lambda x: x*x})
|
|
101
|
-
|
|
102
|
-
returns ``121``.
|
|
103
|
-
|
|
104
|
-
For more details of working with functions, read further down.
|
|
105
|
-
|
|
106
|
-
Note:
|
|
107
|
-
~~~~~
|
|
108
|
-
all further examples use ``>>>`` to designate python code, as if you are using
|
|
109
|
-
the python interactive prompt.
|
|
110
|
-
|
|
111
|
-
.. _Operators:
|
|
112
|
-
|
|
113
|
-
Operators
|
|
114
|
-
---------
|
|
115
|
-
You can add operators yourself, using the ``operators`` argument, but these are
|
|
116
|
-
the defaults:
|
|
117
|
-
|
|
118
|
-
+--------+------------------------------------+
|
|
119
|
-
| ``+`` | add two things. ``x + y`` |
|
|
120
|
-
| | ``1 + 1`` -> ``2`` |
|
|
121
|
-
+--------+------------------------------------+
|
|
122
|
-
| ``-`` | subtract two things ``x - y`` |
|
|
123
|
-
| | ``100 - 1`` -> ``99`` |
|
|
124
|
-
+--------+------------------------------------+
|
|
125
|
-
| ``/`` | divide one thing by another |
|
|
126
|
-
| | ``x / y`` |
|
|
127
|
-
| | ``100/10`` -> ``10`` |
|
|
128
|
-
+--------+------------------------------------+
|
|
129
|
-
| ``*`` | multiple one thing by another |
|
|
130
|
-
| | ``x * y`` |
|
|
131
|
-
| | ``10 * 10`` -> ``100`` |
|
|
132
|
-
+--------+------------------------------------+
|
|
133
|
-
| ``**`` | 'to the power of' ``x**y`` |
|
|
134
|
-
| | ``2 ** 10`` -> ``1024`` |
|
|
135
|
-
+--------+------------------------------------+
|
|
136
|
-
| ``%`` | modulus. (remainder) ``x % y`` |
|
|
137
|
-
| | ``15 % 4`` -> ``3`` |
|
|
138
|
-
+--------+------------------------------------+
|
|
139
|
-
| ``==`` | equals ``x == y`` |
|
|
140
|
-
| | ``15 == 4`` -> ``False`` |
|
|
141
|
-
+--------+------------------------------------+
|
|
142
|
-
| ``<`` | Less than. ``x < y`` |
|
|
143
|
-
| | ``1 < 4`` -> ``True`` |
|
|
144
|
-
+--------+------------------------------------+
|
|
145
|
-
| ``>`` | Greater than. ``x > y`` |
|
|
146
|
-
| | ``1 > 4`` -> ``False`` |
|
|
147
|
-
+--------+------------------------------------+
|
|
148
|
-
| ``<=`` | Less than or Equal to. ``x <= y`` |
|
|
149
|
-
| | ``1 < 4`` -> ``True`` |
|
|
150
|
-
+--------+------------------------------------+
|
|
151
|
-
| ``>=`` | Greater or Equal to ``x >= 21`` |
|
|
152
|
-
| | ``1 >= 4`` -> ``False`` |
|
|
153
|
-
+--------+------------------------------------+
|
|
154
|
-
| ``>>`` | "Right shift" the number. |
|
|
155
|
-
| | ``100 >> 2`` -> ``25`` |
|
|
156
|
-
+--------+------------------------------------+
|
|
157
|
-
| ``<<`` | "Left shift" the number. |
|
|
158
|
-
| | ``100 << 2`` -> ``400`` |
|
|
159
|
-
+--------+------------------------------------+
|
|
160
|
-
| ``in`` | is something contained within |
|
|
161
|
-
| | something else. |
|
|
162
|
-
| | ``"spam" in "my breakfast"`` |
|
|
163
|
-
| | -> ``False`` |
|
|
164
|
-
+--------+------------------------------------+
|
|
165
|
-
| ``^`` | "bitwise exclusive OR" (xor) |
|
|
166
|
-
| | ``62 ^ 20`` -> ``42`` |
|
|
167
|
-
+--------+------------------------------------+
|
|
168
|
-
| ``|`` | "bitwise OR" |
|
|
169
|
-
| | ``8 | 34`` -> ``42`` |
|
|
170
|
-
+--------+------------------------------------+
|
|
171
|
-
| ``&`` | "bitwise AND" |
|
|
172
|
-
| | ``100 & 63`` -> ``36`` |
|
|
173
|
-
+--------+------------------------------------+
|
|
174
|
-
| ``~`` | "bitwise invert" |
|
|
175
|
-
| | ``~ -43`` -> ``42`` |
|
|
176
|
-
+--------+------------------------------------+
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
The ``^`` operator is often mistaken for a exponent operator, not the bitwise
|
|
180
|
-
operation that it is in python, so if you want ``3 ^ 2`` to equal ``9``, you can
|
|
181
|
-
replace the operator like this:
|
|
182
|
-
|
|
183
|
-
.. code-block:: pycon
|
|
184
|
-
|
|
185
|
-
>>> import ast
|
|
186
|
-
>>> from simpleeval import safe_power
|
|
187
|
-
|
|
188
|
-
>>> s = SimpleEval()
|
|
189
|
-
>>> s.operators[ast.BitXor] = safe_power
|
|
190
|
-
|
|
191
|
-
>>> s.eval("3 ^ 2")
|
|
192
|
-
9
|
|
193
|
-
|
|
194
|
-
for example.
|
|
195
|
-
|
|
196
|
-
Limited Power
|
|
197
|
-
~~~~~~~~~~~~~
|
|
198
|
-
|
|
199
|
-
Also note, the ``**`` operator has been locked down by default to have a
|
|
200
|
-
maximum input value of ``4000000``, which makes it somewhat harder to make
|
|
201
|
-
expressions which go on for ever. You can change this limit by changing the
|
|
202
|
-
``simpleeval.MAX_POWER`` module level value to whatever is an appropriate value
|
|
203
|
-
for you (and the hardware that you're running on) or if you want to completely
|
|
204
|
-
remove all limitations, you can set the ``s.operators[ast.Pow] = operator.pow``
|
|
205
|
-
or make your own function.
|
|
206
|
-
|
|
207
|
-
On my computer, ``9**9**5`` evaluates almost instantly, but ``9**9**6`` takes
|
|
208
|
-
over 30 seconds. Since ``9**7`` is ``4782969``, and so over the ``MAX_POWER``
|
|
209
|
-
limit, it throws a ``NumberTooHigh`` exception for you. (Otherwise it would go
|
|
210
|
-
on for hours, or until the computer runs out of memory)
|
|
211
|
-
|
|
212
|
-
Strings (and other Iterables) Safety
|
|
213
|
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
214
|
-
|
|
215
|
-
There are also limits on string length (100000 characters,
|
|
216
|
-
``MAX_STRING_LENGTH``). This can be changed if you wish.
|
|
217
|
-
|
|
218
|
-
Related to this, if you try to create a silly long string/bytes/list, by doing
|
|
219
|
-
``'i want to break free'.split() * 9999999999`` for instance, it will block you.
|
|
220
|
-
|
|
221
|
-
If Expressions
|
|
222
|
-
--------------
|
|
223
|
-
|
|
224
|
-
You can use python style ``if x then y else z`` type expressions:
|
|
225
|
-
|
|
226
|
-
.. code-block:: pycon
|
|
227
|
-
|
|
228
|
-
>>> simple_eval("'equal' if x == y else 'not equal'",
|
|
229
|
-
names={"x": 1, "y": 2})
|
|
230
|
-
'not equal'
|
|
231
|
-
|
|
232
|
-
which, of course, can be nested:
|
|
233
|
-
|
|
234
|
-
.. code-block:: pycon
|
|
235
|
-
|
|
236
|
-
>>> simple_eval("'a' if 1 == 2 else 'b' if 2 == 3 else 'c'")
|
|
237
|
-
'c'
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
Functions
|
|
241
|
-
---------
|
|
242
|
-
|
|
243
|
-
You can define functions which you'd like the expresssions to have access to:
|
|
244
|
-
|
|
245
|
-
.. code-block:: pycon
|
|
246
|
-
|
|
247
|
-
>>> simple_eval("double(21)", functions={"double": lambda x:x*2})
|
|
248
|
-
42
|
|
249
|
-
|
|
250
|
-
You can define "real" functions to pass in rather than lambdas, of course too,
|
|
251
|
-
and even re-name them so that expressions can be shorter
|
|
252
|
-
|
|
253
|
-
.. code-block:: pycon
|
|
254
|
-
|
|
255
|
-
>>> def double(x):
|
|
256
|
-
return x * 2
|
|
257
|
-
>>> simple_eval("d(100) + double(1)", functions={"d": double, "double":double})
|
|
258
|
-
202
|
|
259
|
-
|
|
260
|
-
If you don't provide your own ``functions`` dict, then the the following defaults
|
|
261
|
-
are provided in the ``DEFAULT_FUNCTIONS`` dict:
|
|
262
|
-
|
|
263
|
-
+----------------+--------------------------------------------------+
|
|
264
|
-
| ``randint(x)`` | Return a random ``int`` below ``x`` |
|
|
265
|
-
+----------------+--------------------------------------------------+
|
|
266
|
-
| ``rand()`` | Return a random ``float`` between 0 and 1 |
|
|
267
|
-
+----------------+--------------------------------------------------+
|
|
268
|
-
| ``int(x)`` | Convert ``x`` to an ``int``. |
|
|
269
|
-
+----------------+--------------------------------------------------+
|
|
270
|
-
| ``float(x)`` | Convert ``x`` to a ``float``. |
|
|
271
|
-
+----------------+--------------------------------------------------+
|
|
272
|
-
| ``str(x)`` | Convert ``x`` to a ``str`` |
|
|
273
|
-
+----------------+--------------------------------------------------+
|
|
274
|
-
|
|
275
|
-
If you want to provide a list of functions, but want to keep these as well,
|
|
276
|
-
then you can do a normal python ``.copy()`` & ``.update``:
|
|
277
|
-
|
|
278
|
-
.. code-block:: pycon
|
|
279
|
-
|
|
280
|
-
>>> my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
|
|
281
|
-
>>> my_functions.update(
|
|
282
|
-
square=(lambda x:x*x),
|
|
283
|
-
double=(lambda x:x+x),
|
|
284
|
-
)
|
|
285
|
-
>>> simple_eval('square(randint(100))', functions=my_functions)
|
|
286
|
-
|
|
287
|
-
Names
|
|
288
|
-
-----
|
|
289
|
-
|
|
290
|
-
Sometimes it's useful to have variables available, which in python terminology
|
|
291
|
-
are called 'names'.
|
|
292
|
-
|
|
293
|
-
.. code-block:: pycon
|
|
294
|
-
|
|
295
|
-
>>> simple_eval("a + b", names={"a": 11, "b": 100})
|
|
296
|
-
111
|
|
297
|
-
|
|
298
|
-
You can also hand the handling of names over to a function, if you prefer:
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
.. code-block:: pycon
|
|
302
|
-
|
|
303
|
-
>>> def name_handler(node):
|
|
304
|
-
return ord(node.id[0].lower(a))-96
|
|
305
|
-
|
|
306
|
-
>>> simple_eval('a + b', names=name_handler)
|
|
307
|
-
3
|
|
308
|
-
|
|
309
|
-
That was a bit of a silly example, but you could use this for pulling values
|
|
310
|
-
from a database or file, looking up spreadsheet cells, say, or doing some kind of caching system.
|
|
311
|
-
|
|
312
|
-
In general, when it attempts to find a variable by name, if it cannot find one,
|
|
313
|
-
then it will look in the ``functions`` for a function of that name. If you want your name handler
|
|
314
|
-
function to return an "I can't find that name!", then it should raise a ``simpleeval.NameNotDefined``
|
|
315
|
-
exception. Eg:
|
|
316
|
-
|
|
317
|
-
.. code-block:: pycon
|
|
318
|
-
|
|
319
|
-
>>> def name_handler(node):
|
|
320
|
-
... if node.id[0] == 'a':
|
|
321
|
-
... return 21
|
|
322
|
-
... raise NameNotDefined(node.id[0], "Not found")
|
|
323
|
-
...
|
|
324
|
-
... simple_eval('a + a', names=name_handler, functions={"b": 100})
|
|
325
|
-
|
|
326
|
-
42
|
|
327
|
-
|
|
328
|
-
>>> simple_eval('a + b', names=name_handler, functions={'b': 100})
|
|
329
|
-
121
|
|
330
|
-
|
|
331
|
-
(Note: in that example, putting a number directly into the ``functions`` dict was done just to
|
|
332
|
-
show the fall-back to functions. Normally only put actual callables in there.)
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
The two default names that are provided are ``True`` and ``False``. So if you want to provide
|
|
336
|
-
your own names, but want ``True`` and ``False`` to keep working, either provide them yourself,
|
|
337
|
-
or ``.copy()`` and ``.update`` the ``DEFAULT_NAMES``. (See functions example above).
|
|
338
|
-
|
|
339
|
-
Creating an Evaluator Class
|
|
340
|
-
---------------------------
|
|
341
|
-
|
|
342
|
-
Rather than creating a new evaluator each time, if you are doing a lot of
|
|
343
|
-
evaluations, you can create a SimpleEval object, and pass it expressions each
|
|
344
|
-
time (which should be a bit quicker, and certainly more convenient for some use
|
|
345
|
-
cases):
|
|
346
|
-
|
|
347
|
-
.. code-block:: pycon
|
|
348
|
-
|
|
349
|
-
>>> s = SimpleEval()
|
|
350
|
-
|
|
351
|
-
>>> s.eval("1 + 1")
|
|
352
|
-
2
|
|
353
|
-
|
|
354
|
-
>>> s.eval('100 * 10')
|
|
355
|
-
1000
|
|
356
|
-
|
|
357
|
-
# and so on...
|
|
358
|
-
|
|
359
|
-
One useful feature of using the ``SimpleEval`` object is that you can parse an expression
|
|
360
|
-
once, and then evaluate it mulitple times using different ``names``:
|
|
361
|
-
|
|
362
|
-
.. code-block:: python
|
|
363
|
-
|
|
364
|
-
# Set up & Cache the parse tree:
|
|
365
|
-
expression = "foo + bar"
|
|
366
|
-
parsed = s.parse(expression)
|
|
367
|
-
|
|
368
|
-
# evaluate the expression multiple times:
|
|
369
|
-
for names in [{"foo": 1, "bar": 10}, {"foo": 100, "bar": 42}]:
|
|
370
|
-
s.names = names
|
|
371
|
-
print(s.eval(expression, previously_parsed=parsed))
|
|
372
|
-
|
|
373
|
-
for instance. This may help with performance.
|
|
374
|
-
|
|
375
|
-
You can assign / edit the various options of the ``SimpleEval`` object if you
|
|
376
|
-
want to. Either assign them during creation (like the ``simple_eval``
|
|
377
|
-
function)
|
|
378
|
-
|
|
379
|
-
.. code-block:: python
|
|
380
|
-
|
|
381
|
-
def boo():
|
|
382
|
-
return 'Boo!'
|
|
383
|
-
|
|
384
|
-
s = SimpleEval(functions={"boo": boo})
|
|
385
|
-
|
|
386
|
-
or edit them after creation:
|
|
387
|
-
|
|
388
|
-
.. code-block:: python
|
|
389
|
-
|
|
390
|
-
s.names['fortytwo'] = 42
|
|
391
|
-
|
|
392
|
-
this actually means you can modify names (or functions) with functions, if you
|
|
393
|
-
really feel so inclined:
|
|
394
|
-
|
|
395
|
-
.. code-block:: python
|
|
396
|
-
|
|
397
|
-
s = SimpleEval()
|
|
398
|
-
def set_val(name, value):
|
|
399
|
-
s.names[name.value] = value.value
|
|
400
|
-
return value.value
|
|
401
|
-
|
|
402
|
-
s.functions = {'set': set_val}
|
|
403
|
-
|
|
404
|
-
s.eval("set('age', 111)")
|
|
405
|
-
|
|
406
|
-
Say. This would allow a certain level of 'scriptyness' if you had these
|
|
407
|
-
evaluations happening as callbacks in a program. Although you really are
|
|
408
|
-
reaching the end of what this library is intended for at this stage.
|
|
409
|
-
|
|
410
|
-
Compound Types
|
|
411
|
-
--------------
|
|
412
|
-
|
|
413
|
-
Compound types (``dict``, ``tuple``, ``list``, ``set``) in general just work if
|
|
414
|
-
you pass them in as named objects. If you want to allow creation of these, the
|
|
415
|
-
``EvalWithCompoundTypes`` class works. Just replace any use of ``SimpleEval`` with
|
|
416
|
-
that.
|
|
417
|
-
|
|
418
|
-
The ``EvalWithCompoundTypes`` class also contains support for simple comprehensions.
|
|
419
|
-
eg: ``[x + 1 for x in [1,2,3]]``. There's a safety `MAX_COMPREHENSION_LENGTH` to control
|
|
420
|
-
how many items it'll allow before bailing too. This also takes into account nested
|
|
421
|
-
comprehensions.
|
|
422
|
-
|
|
423
|
-
Since the primary intention of this library is short expressions - an extra 'sweetener' is
|
|
424
|
-
enabled by default. You can access a dict (or similar's) keys using the .attr syntax:
|
|
425
|
-
|
|
426
|
-
.. code-block:: pycon
|
|
427
|
-
|
|
428
|
-
>>> simple_eval("foo.bar", names={"foo": {"bar": 42}})
|
|
429
|
-
42
|
|
430
|
-
|
|
431
|
-
for instance. You can turn this off either by setting the module global `ATTR_INDEX_FALLBACK`
|
|
432
|
-
to `False`, or on the ``SimpleEval`` instance itself. e.g. ``evaller.ATTR_INDEX_FALLBACK=False``.
|
|
433
|
-
|
|
434
|
-
Extending
|
|
435
|
-
---------
|
|
436
|
-
|
|
437
|
-
The ``SimpleEval`` class is pretty easy to extend. For instance, to create a
|
|
438
|
-
version that disallows method invocation on objects:
|
|
439
|
-
|
|
440
|
-
.. code-block:: python
|
|
441
|
-
|
|
442
|
-
import ast
|
|
443
|
-
import simpleeval
|
|
444
|
-
|
|
445
|
-
class EvalNoMethods(simpleeval.SimpleEval):
|
|
446
|
-
def _eval_call(self, node):
|
|
447
|
-
if isinstance(node.func, ast.Attribute):
|
|
448
|
-
raise simpleeval.FeatureNotAvailable("No methods please, we're British")
|
|
449
|
-
return super(EvalNoMethods, self)._eval_call(node)
|
|
450
|
-
|
|
451
|
-
and then use ``EvalNoMethods`` instead of the ``SimpleEval`` class.
|
|
452
|
-
|
|
453
|
-
Other...
|
|
454
|
-
--------
|
|
455
|
-
|
|
456
|
-
The library supports Python 3.9 and higher.
|
|
457
|
-
|
|
458
|
-
Object attributes that start with ``_`` or ``func_`` are disallowed by default.
|
|
459
|
-
If you really need that (BE CAREFUL!), then modify the module global
|
|
460
|
-
``simpleeval.DISALLOW_PREFIXES``.
|
|
461
|
-
|
|
462
|
-
A few builtin functions are listed in ``simpleeval.DISALLOW_FUNCTIONS``. ``type``, ``open``, etc.
|
|
463
|
-
If you need to give access to this kind of functionality to your expressions, then be very
|
|
464
|
-
careful. You'd be better wrapping the functions in your own safe wrappers.
|
|
465
|
-
|
|
466
|
-
The initial idea came from J.F. Sebastian on Stack Overflow
|
|
467
|
-
( http://stackoverflow.com/a/9558001/1973500 ) with modifications and many improvements,
|
|
468
|
-
see the head of the main file for contributors list.
|
|
469
|
-
|
|
470
|
-
Please read the ``test_simpleeval.py`` file for other potential gotchas or
|
|
471
|
-
details. I'm very happy to accept pull requests, suggestions, or other issues.
|
|
472
|
-
Enjoy!
|
|
473
|
-
|
|
474
|
-
Developing
|
|
475
|
-
----------
|
|
476
|
-
|
|
477
|
-
Run tests::
|
|
478
|
-
|
|
479
|
-
$ make test
|
|
480
|
-
|
|
481
|
-
Or to set the tests running on every file change:
|
|
482
|
-
|
|
483
|
-
$ make autotest
|
|
484
|
-
|
|
485
|
-
(requires ``entr``)
|
|
486
|
-
|
|
487
|
-
I'm trying to keep the codebase relatively clean with Black, isort, pylint & mypy.
|
|
488
|
-
See::
|
|
489
|
-
|
|
490
|
-
$ make format
|
|
491
|
-
|
|
492
|
-
and::
|
|
493
|
-
|
|
494
|
-
$ make lint
|
|
495
|
-
|
|
496
|
-
BEWARE
|
|
497
|
-
------
|
|
498
|
-
|
|
499
|
-
I've done the best I can with this library - but there's no warranty, no guarantee, nada. A lot of
|
|
500
|
-
very clever people think the whole idea of trying to sandbox CPython is impossible. Read the code
|
|
501
|
-
yourself, and use it at your own risk.
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
.gitignore
|
|
2
|
-
LICENCE
|
|
3
|
-
MANIFEST.in
|
|
4
|
-
Makefile
|
|
5
|
-
README.rst
|
|
6
|
-
pyproject.toml
|
|
7
|
-
setup.cfg
|
|
8
|
-
setup.py
|
|
9
|
-
simpleeval.py
|
|
10
|
-
test_simpleeval.py
|
|
11
|
-
simpleeval.egg-info/PKG-INFO
|
|
12
|
-
simpleeval.egg-info/SOURCES.txt
|
|
13
|
-
simpleeval.egg-info/dependency_links.txt
|
|
14
|
-
simpleeval.egg-info/top_level.txt
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
simpleeval
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|