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


The plusminus package provides a ready-to-run arithmetic parser and evaluator, based on pyparsing's infixNotation helper method.

Strings containing 6-function arithmetic expressions can be parsed and evaluated using the BaseArithmeticParser:

from plusminus import BaseArithmeticParser

parser = BaseArithmeticParser()

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:

∩ & (set intersection)
∪ | (set union)
* / // × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
and ∧
or ∨
? : (ternary)

Functions can be called:

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

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.

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.

Download files

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

Source Distribution

plusminus-0.6.0.tar.gz (35.8 kB view hashes)

Uploaded source

Built Distribution

plusminus-0.6.0-py3-none-any.whl (36.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page