Skip to main content

+/- 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:

()
-
**
* / × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
between-and within-and "in range from"-to (ternary) (between is exclusive, within is inclusive, and `in range from` is
  inclusive lower-bound and exclusive upper-bound)
not
and ∧
or ∨
? : (ternary)

Functions can be called:

  sin   sinh  abs    log2   rnd
  cos   cosh  round  log10  randint
  tan   tanh  trunc  gcd    min
  asin  rad   ceil   lcm    max
  acos  deg   floor  gamma
  atan  sgn   ln     hypot

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for plusminus, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size plusminus-0.1.1-py3-none-any.whl (22.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size plusminus-0.1.1.tar.gz (26.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page