simpleeval
A simple, safe single expression evaluator library.
Downloads: 0 (30 days)
Description
simpleeval (Simple Eval)
========================
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=main
:target: https://github.com/danthedeckie/simpleeval/actions/
:alt: Build Status
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/main/graph/badge.svg?token=isRnN1yrca
:target: https://codecov.io/gh/danthedeckie/simpleeval
:alt: Code Coverage Status
.. |pypi-version| image:: https://img.shields.io/pypi/v/simpleeval
:target: https://badge.fury.io/py/simpleeval
:alt: PyPI - Version
.. |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
:alt: Static Badge
.. |pypi-monthly-downloads| image:: https://img.shields.io/pypi/dm/SimpleEval
:alt: PyPI - Downloads
.. |formatting-with-ruff| image:: https://img.shields.io/badge/-ruff-black?logo=lightning&logoColor=%2300ff00&link=https%3A%2F%2Fdocs.astral.sh%2Fruff%2F
:alt: Static Badge
|build-status| |code-coverage| |pypi-version| |python-versions| |pypi-monthly-downloads| |formatting-with-ruff|
A single file library for easily adding evaluatable expressions into
python projects. Say you want to allow a user to set an alarm volume, which
could depend on the time of day, alarm level, how many previous alarms had gone
off, and if there is music playing at the time.
Or if you want to allow simple formulae in a web application, but don't want to
give full eval() access, or don't want to run in javascript on the client side.
It's deliberately trying to stay simple to use and not have millions of features,
pull it in from PyPI (pip or easy_install), or even just a single file you can dump
into a project.
Internally, it's using the amazing python ``ast`` module to parse the
expression, which allows very fine control of what is and isn't allowed. It
should be completely safe in terms of what operations can be performed by the
expression.
The only issue I know to be aware of is that you can create an expression which
takes a long time to evaluate, or which evaluating requires an awful lot of
memory, which leaves the potential for DOS attacks. There is basic protection
against this, and you can lock it down further if you desire. (see the
Operators_ section below)
You should be aware of this when deploying in a public setting.
The defaults are pretty locked down and basic, and it's easy to add
whatever extra specific functionality you need (your own functions,
variable/name lookup, etc).
Basic Usage
-----------
To get very simple evaluating:
.. code-block:: python
from simpleeval import simple_eval
simple_eval("21 + 21")
returns ``42``.
Expressions can be as complex and convoluted as you want:
.. code-block:: python
simple_eval("21 + 19 / 7 + (8 % 3) ** 9")
returns ``535.714285714``.
You can add your own functions in as well.
.. code-block:: python
simple_eval("square(11)", functions={"square": lambda x: x*x})
returns ``121``.
For more details of working with functions, read further down.
Note:
~~~~~
all further examples use ``>>>`` to designate python code, as if you are using
the python interactive prompt.
.. _Operators:
Operators
---------
You can add operators yourself, using the ``operators`` argument, but these are
the defaults:
+--------+------------------------------------+
| ``+`` | add two things. ``x + y`` |
| | ``1 + 1`` -> ``2`` |
+--------+------------------------------------+
| ``-`` | subtract two things ``x - y`` |
| | ``100 - 1`` -> ``99`` |
+--------+------------------------------------+
| ``/`` | divide one thing by another |
| | ``x / y`` |
| | ``100/10`` -> ``10`` |
+--------+------------------------------------+
| ``*`` | multiple one thing by another |
| | ``x * y`` |
| | ``10 * 10`` -> ``100`` |
+--------+------------------------------------+
| ``**`` | 'to the power of' ``x**y`` |
| | ``2 ** 10`` -> ``1024`` |
+--------+------------------------------------+
| ``%`` | modulus. (remainder) ``x % y`` |
| | ``15 % 4`` -> ``3`` |
+--------+------------------------------------+
| ``==`` | equals ``x == y`` |
| | ``15 == 4`` -> ``False`` |
+--------+------------------------------------+
| ``<`` | Less than. ``x < y`` |
| | ``1 < 4`` -> ``True`` |
+--------+------------------------------------+
| ``>`` | Greater than. ``x > y`` |
| | ``1 > 4`` -> ``False`` |
+--------+------------------------------------+
| ``<=`` | Less than or Equal to. ``x <= y`` |
| | ``1 < 4`` -> ``True`` |
+--------+------------------------------------+
| ``>=`` | Greater or Equal to ``x >= 21`` |
| | ``1 >= 4`` -> ``False`` |
+--------+------------------------------------+
| ``>>`` | "Right shift" the number. |
| | ``100 >> 2`` -> ``25`` |
+--------+------------------------------------+
| ``<<`` | "Left shift" the number. |
| | ``100 << 2`` -> ``400`` |
+--------+------------------------------------+
| ``in`` | is something contained within |
| | something else. |
| | ``"spam" in "my breakfast"`` |
| | -> ``False`` |
+--------+------------------------------------+
| ``^`` | "bitwise exclusive OR" (xor) |
| | ``62 ^ 20`` -> ``42`` |
+--------+------------------------------------+
| ``|`` | "bitwise OR" |
| | ``8 | 34`` -> ``42`` |
+--------+------------------------------------+
| ``&`` | "bitwise AND" |
| | ``100 & 63`` -> ``36`` |
+--------+------------------------------------+
| ``~`` | "bitwise invert" |
| | ``~ -43`` -> ``42`` |
+--------+------------------------------------+
The ``^`` operator is often mistaken for a exponent operator, not the bitwise
operation that it is in python, so if you want ``3 ^ 2`` to equal ``9``, you can
replace the operator like this:
.. code-block:: pycon
>>> import ast
>>> from simpleeval import safe_power
>>> s = SimpleEval()
>>> s.operators[ast.BitXor] = safe_power
>>> s.eval("3 ^ 2")
9
for example.
Limited Power
~~~~~~~~~~~~~
Also note, the ``**`` operator has been locked down by default to have a
maximum input value of ``4000000``, which makes it somewhat harder to make
expressions which go on for ever. You can change this limit by changing the
``simpleeval.MAX_POWER`` module level value to whatever is an appropriate value
for you (and the hardware that you're running on) or if you want to completely
remove all limitations, you can set the ``s.operators[ast.Pow] = operator.pow``
or make your own function.
On my computer, ``9**9**5`` evaluates almost instantly, but ``9**9**6`` takes
over 30 seconds. Since ``9**7`` is ``4782969``, and so over the ``MAX_POWER``
limit, it throws a ``NumberTooHigh`` exception for you. (Otherwise it would go
on for hours, or until the computer runs out of memory)
Strings (and other Iterables) Safety
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There are also limits on string length (100000 characters,
``MAX_STRING_LENGTH``). This can be changed if you wish.
Related to this, if you try to create a silly long string/bytes/list, by doing
``'i want to break free'.split() * 9999999999`` for instance, it will block you.
If Expressions
--------------
You can use python style ``if x then y else z`` type expressions:
.. code-block:: pycon
>>> simple_eval("'equal' if x == y else 'not equal'",
names={"x": 1, "y": 2})
'not equal'
which, of course, can be nested:
.. code-block:: pycon
>>> simple_eval("'a' if 1 == 2 else 'b' if 2 == 3 else 'c'")
'c'
Functions
---------
You can define functions which you'd like the expressions to have access to:
.. code-block:: pycon
>>> simple_eval("double(21)", functions={"double": lambda x:x*2})
42
You can define "real" functions to pass in rather than lambdas, of course too,
and even re-name them so that expressions can be shorter
.. code-block:: pycon
>>> def double(x):
return x * 2
>>> simple_eval("d(100) + double(1)", functions={"d": double, "double":double})
202
If you don't provide your own ``functions`` dict, then the the following defaults
are provided in the ``DEFAULT_FUNCTIONS`` dict:
+----------------+--------------------------------------------------+
| ``randint(x)`` | Return a random ``int`` below ``x`` |
+----------------+--------------------------------------------------+
| ``rand()`` | Return a random ``float`` between 0 and 1 |
+----------------+--------------------------------------------------+
| ``int(x)`` | Convert ``x`` to an ``int``. |
+----------------+--------------------------------------------------+
| ``float(x)`` | Convert ``x`` to a ``float``. |
+----------------+--------------------------------------------------+
| ``str(x)`` | Convert ``x`` to a ``str`` |
+----------------+--------------------------------------------------+
If you want to provide a list of functions, but want to keep these as well,
then you can do a normal python ``.copy()`` & ``.update``:
.. code-block:: pycon
>>> my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
>>> my_functions.update(
square=(lambda x:x*x),
double=(lambda x:x+x),
)
>>> simple_eval('square(randint(100))', functions=my_functions)
Names
-----
Sometimes it's useful to have variables available