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 6-function arithmetic expressions can be parsed and evaluated using the BaseArithmeticParser:

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:

()
∩ & (set intersection)
∪ | (set union)
-
**
* / // × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
not
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.

Project details


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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file plusminus-0.6.0.tar.gz.

File metadata

  • Download URL: plusminus-0.6.0.tar.gz
  • Upload date:
  • Size: 35.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for plusminus-0.6.0.tar.gz
Algorithm Hash digest
SHA256 75f8f53efcbb8c004fcf781e63718533070a16ebbaf5dbb6dd80a6afe1daa4b1
MD5 75b449e894d7c99ca161fcdf0b506171
BLAKE2b-256 c81beabdfb9d90268bad6917482c766f0b24a03bf3992249087b359eb202f5aa

See more details on using hashes here.

File details

Details for the file plusminus-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: plusminus-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 36.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for plusminus-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3515aa2361e58d7f9176b517660dc317b5a3b83bbe0699ede48b7d0a4a5a537
MD5 010a373e8f3cce6cc190eadf8ef38eae
BLAKE2b-256 fed72ec7f97180bbd81e684b5845ba68ab700bd29bb6e49ff3b4263671ffe273

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page