A package that lets you safely evaluate arithmetic expressions.
Project description
arithmeval - Arithmetic Expression Evaluator
arithmeval
is a Python package that provides a utility function to evaluate arithmetic expressions, given a dictionary of values. It supports basic arithmetic operations and can handle simple mathematical calculations.
It also supports string concatenation, though it is not intended to be used as a general-purpose expression evaluator, therefore tests for string concatenation are not included.
Arithmeval is a secure, safety-first expression evaluator. It only supports the following list of operations:
- Addition (
+
) - True division (
/
) - Floor division (
//
) - Modulus (
%
) - Multiplication (
*
) - Exponentiation (
**
) - Subtraction (
-
)
Installation
You can install arithmeval
using pip
:
pip install arithmeval
Usage
Import the evaluate
function from the arithmetic_eval module:
from arithmetic_eval import evaluate
Use the evaluate function to calculate the value of an arithmetic expression:
expression = "a + b"
values_dict = {"a": 1, "b": 2}
result = evaluate(expression, values_dict)
print(result) # Output: 3
The evaluate function takes the following arguments:
expression (str)
: The arithmetic expression to be evaluated.value_dict (Optional[dict])
: A dictionary containing variable values used in the expression.arithmetic_exception_to_default_value (Optional[dict])
: A dictionary mapping specific arithmetic exceptions to default values.
The function returns an int, float, or str representing the result of the expression.
Error Handling
The evaluate
function raises various exceptions in case of errors:
SyntaxError
: If the expression is not a valid Python expression.NameError
: If a variable is not defined in the values_dict.NotImplementedError
: If the expression contains an unsupported operation.MaliciousInputError
: If the expression is a malicious input.
Examples
from arithmetic_eval import evaluate
expression = "2 * (x + y)"
value_dict = {"x": 3, "y": 4}
result = evaluate(expression, value_dict)
print(result) # Output: 14
from arithmetic_eval import evaluate
expression = "10 / (a - 5)"
value_dict = {"a": 5}
result = evaluate(
expression=expression,
value_dict=value_dict,
arithmetic_exception_to_default_value={
ZeroDivisionError: 0
}
)
print(result) # Output: 0
Note that it is not recommended to use the arithmetic_exception_to_default_value
argument. It is only provided for convenience and should be used with caution. The default behavior for any ArithmeticError is to return 0.
If you insist on overriding the default return value for arithmetic exceptions, make sure to use a dictionary mapping each ArithmeticError to a default value.
A list of arithmetic exceptions can be found here.
License
This package is distributed under the BSD 3-Clause License. See LICENSE
Contributing
Contributions to arithmeval are welcome! Feel free to open issues or pull requests on the GitHub repository.
Acknowledgements
This package was inspired by the need for a simple and safe arithmetic expression evaluator in Python.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file arithmeval-0.3.3.tar.gz
.
File metadata
- Download URL: arithmeval-0.3.3.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1413643ed42e65860e3b6b2a2af04c1f7855344de36e7ebef5212fb8391d4c14 |
|
MD5 | c6f4e768880be8201b4b4b9e694c5f99 |
|
BLAKE2b-256 | 136758e293c69c76dcac2ecfbbc74ed88faf282ddf602af2f8ce760238583502 |
File details
Details for the file arithmeval-0.3.3-py3-none-any.whl
.
File metadata
- Download URL: arithmeval-0.3.3-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66b2711dfc63cb4e91138b30cfb55c1e147c14d006d17a00ff9f29ae7a316c66 |
|
MD5 | 5ea87adb1a39a6e69206eb8644a4c208 |
|
BLAKE2b-256 | b5d76116cba8b63d44b02b54e4a8fb7ece9e4c97b53c8eb7e6aee204475c64f8 |