+/- 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
infixNotation
helper method.
Strings containing 5-function arithmetic expressions can be parsed and evaluated using the BasicArithmeticParser
:
from plusminus import BasicArithmeticParser
parser = BasicArithmeticParser()
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 ()
's
to override:
()
∩ (set intersection)
∪ (set union)
-
**
* / × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
in ∈ ∉
not
and ∧
or ∨
? : (ternary)
Functions can be called:
sgn min asin rad lcm
abs max acos deg gamma
round str atan ln hypot
trunc sin sinh log2 nhypot
ceil cos cosh log10 rnd
floor tan tanh gcd
The Basic ArithmeticParser 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.
Custom expressions can be defined using a simple API. Example parsers are included for dice rolling, combination/permutation expressions, and common business calculations. 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.2.0.tar.gz
.
File metadata
- Download URL: plusminus-0.2.0.tar.gz
- Upload date:
- Size: 30.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b018b16a2b1ffd16c13b2094bd01372a2e52ffda31678213f2c1437407b16466 |
|
MD5 | b904f7a9d06aa82c4047117af93b0a23 |
|
BLAKE2b-256 | e793b41fd81404d45cbee06c1b4c0c0ba356731ec9ee3fbc401a4cedfea30849 |
File details
Details for the file plusminus-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: plusminus-0.2.0-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcdd2b260f03ee0b29d9bbf135259565724c797d8f13c43da93e6d8e7bb89249 |
|
MD5 | 6f73776be71cd392d2ff9958aad81cc4 |
|
BLAKE2b-256 | e2827576dae61e8e7eb4e2a5739b340a466329bb9d5cf04879aec0760c299866 |