+/- plusminus is a module that builds on the pyparsing infixNotation helper method to build easy-to-code and
Strings containing 6-function arithmetic expressions can be parsed and evaluated using the
from plusminus import BaseArithmeticParser parser = BaseArithmeticParser() print(parser.evaluate("2+3/10"))
The parser can also return an Abstract Syntax Tree of
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
BaseArithmeticParser also supports assignment of variables:
r = 5 area = π × r²
This last expression could be assigned using
@= formula assignment:
area @= π × r²
r is updated, evaluating
area will be reevaluated using the new value.
ArithmeticParser class is also defined, with more extensive operators,
! - 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
Example parsers are included for other specialized applications
- dice rolling (
"3d6 + d20")
- time delta expressions (
"today() + 2d + 12h")
- retail and business expressions (
"20% off of 19.99")
- combinatoric expressions (
These parsers can be incorporated into other applications to support the safe evaluation of user-defined domain-specific expressions.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for plusminus-0.7.0-py3-none-any.whl