SimpleEval is a Python library designed for safely evaluating simple expressions provided by untrusted users. It acts as a controlled alternative to Python's built-in `eval()` function, parsing expressions using the `ast` module to restrict executable operations, functions, and names. This prevents malicious code execution while allowing flexible, user-defined calculations. The current version is 1.0.7, and the library maintains an active development and release cadence.
pip install simpleevalVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic expression evaluation using `simple_eval` and more advanced usage with the `SimpleEval` class, including custom variables, functions, and safe attribute access.
Upgrade your Python environment to 3.9 or higher, or pin simpleeval to a version below 1.0.0 (e.g., `pip install simpleeval<1.0.0`).
Upgrade to simpleeval version 1.0.7 or later immediately. Carefully review any objects, functions, or modules you expose to the evaluator via `names` or `functions` parameters, ensuring they do not transitively expose dangerous functionality.
While defaults are safe, be aware that you can modify `simpleeval.MAX_POWER`, `simpleeval.MAX_STRING_LENGTH`, or `simpleeval.MAX_COMPREHENSION_LENGTH` if your use case genuinely requires higher limits. Exercise caution as this increases DoS risk.
If exponentiation is desired, you must explicitly replace the operator by modifying `s.operators[ast.BitXor] = simpleeval.safe_power` on a `SimpleEval` instance, or use the `**` operator.
To allow safe attribute access, pass `allowed_attrs=BASIC_ALLOWED_ATTRS` to `SimpleEval`. For controlled module exposure, use `ModuleWrapper`. If you need to expose custom functions, wrap them carefully to avoid security pitfalls.
pip install simpleeval
```python
from simpleeval import SimpleEval
# For variables
s = SimpleEval(names={"my_variable": 10})
result = s.eval("my_variable + 5")
print(result)
# For functions
def my_custom_func(x):
return x * 2
s = SimpleEval(functions={"my_custom_func": my_custom_func})
result = s.eval("my_custom_func(7)")
print(result)
``````python
from simpleeval import SimpleEval
s = SimpleEval()
# Corrected: ensure the expression is valid Python syntax
result = s.eval("(10 + 2) * 3") # Example: user might have written "(10 + 2 * 3"
print(result)
``````python
from simpleeval import SimpleEval
s = SimpleEval()
# SimpleEval does not allow complex constructs like lambda functions.
# Instead, define functions in Python and pass them to SimpleEval:
def calculate_discount(price, rate):
return price * (1 - rate)
s.functions['calculate_discount'] = calculate_discount
result = s.eval('calculate_discount(100, 0.1)')
print(result) # Output: 90.0
```Pass the required names or functions as dictionaries to the SimpleEval constructor to make them available within the evaluated expression.
```python
from simpleeval import SimpleEval
s = SimpleEval(names={'x': 10, 'y': 20}, functions={'add': lambda a, b: a + b})
result = s.eval('add(x, y)')
```No dependency data recorded yet.