+/- plusminus is a module that builds on the pyparsing infixNotation helper method to build easy-to-code and easy-to-use parsers for parsing and evaluating infix arithmetic expressions. plusminus's ArithmeticParser class includes separate parse and evaluate methods, handling operator precedence, override with parentheses, presence or absence of whitespace, built-in functions, and pre-defined and user-defined variables, functions, and operators.
Project description
plusminus
The plusminus package provides a ready-to-run arithmetic parser and evaluator,
based on pyparsing
's
infix_notation
helper method.
Strings containing 6-function arithmetic expressions can be parsed and evaluated using the
ArithmeticParser
:
from plusminus import BaseArithmeticParser
parser = BaseArithmeticParser()
print(parser.evaluate("2+3/10"))
The parser can also return an Abstract Syntax Tree of ArithNode
objects:
parsed_elements = parser.parse("2+3/10")
Arithmetic expressions are evaluated following standard rules for operator precedence, allowing for use of parentheses to override:
()
|x|
∩ & ∪ | - ^ ∆ (set operations)
**
-
* / // × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
in ∈ ∉ (element in/not in set)
not
and ∧
or ∨
? : (ternary)
Functions can be called:
abs ceil max
round floor str
trunc min bool
The BaseArithmeticParser
also supports assignment of variables:
r = 5
area = π × r²
This last expression could be assigned using @=
formula assignment:
area @= π × r²
As r
is updated, evaluating area
will be reevaluated using the new value.
An ArithmeticParser
class is also defined, with more extensive operators,
including:
! - factorial
° - degree-radian conversion
√ ⁿ√ - square root and n'th root (2-9)
⁻¹ ⁰ ¹ ² ³ - common exponents as superscripts
and additional pre-defined functions:
sin asin rad gcd
cos acos deg lcm
tan atan ln rnd
sgn sinh log randint
gamma cosh log2
hypot tanh log10
This parser class can be used in applications using algebra or trigonometry expressions.
Custom expressions can be defined using a simple
API
.
Example parsers are included for other specialized applications
and domains:
- dice rolling (
"3d6 + d20"
) - time delta expressions (
"today() + 2d + 12h"
) - retail and business expressions (
"20% off of 19.99"
) - combinatoric expressions (
"6C2"
or"5P3"
)
These parsers can be incorporated into other applications to support the safe evaluation of user-defined domain-specific expressions.
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 plusminus-0.8.1.tar.gz
.
File metadata
- Download URL: plusminus-0.8.1.tar.gz
- Upload date:
- Size: 36.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7252664da7dffb1b405787e5bebc6a32702f48d6551a81c857a1c9ead9319ddd |
|
MD5 | 693df639b2b80b4e618550ef9f712729 |
|
BLAKE2b-256 | 909d0a5c73502ce3b1091970b87c6b23f05dde59edf4e54bab83a51c7cf4d9a0 |
File details
Details for the file plusminus-0.8.1-py3-none-any.whl
.
File metadata
- Download URL: plusminus-0.8.1-py3-none-any.whl
- Upload date:
- Size: 28.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9131e6232a996f63afe0f47a0f516e257709f4e8115911695638b958f941ffa9 |
|
MD5 | c8483e69e5ff9ac8c4282df00799cc10 |
|
BLAKE2b-256 | 11cb0e59c6a68f2709906e845e7b462a42bb3f386e70e015d38a238d0ae94c2d |