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 supports the following list of operations:
- Addition (
+) - True division (
/) - Floor division (
//) - Modulus (
%) - Multiplication (
*) - Exponentiation (
**) - Subtraction (
-) - Logical AND (
and) - Logical OR (
or) - Logical NOT (
not)
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.
Metadata
Release files for arithmeval 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| arithmeval-0.3.4.tar.gz | 6.1 kB | Without provenance | Details for arithmeval-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| arithmeval-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for arithmeval-0.3.4-py3-none-any.whl |
Total release size: 13.0 kB
Release files / arithmeval-0.3.4.tar.gz
| Download URL | arithmeval-0.3.4.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
44c466cb3fd30ef875b0c1c135c4497f0f8a81cadc3a57589c079bc6b9057fa3
|
|
BLAKE2b-256 checksum How to use checksums |
757a9bd6d92dd5f1306922f16b72c1bd833aff987e2689bd15659284a8e59857
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / arithmeval-0.3.4-py3-none-any.whl
| Download URL | arithmeval-0.3.4-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8f4f8f4a68c07124763b89801c754a6153f4be8319d0e594784b743b82456c06
|
|
BLAKE2b-256 checksum How to use checksums |
0702d938ecbd362f6af25f15cddfd48df6dbb0095dfa81383480d2376ee41f73
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for arithmeval 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.