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:

()
∩ (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


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.3.0.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

plusminus-0.3.0-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plusminus-0.3.0.tar.gz
  • Upload date:
  • Size: 32.9 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

Hashes for plusminus-0.3.0.tar.gz
Algorithm Hash digest
SHA256 56e206e034ebf37e642a1a081fc0567c5279a23324b63c21448caf335bb8074d
MD5 2cf7c46fa40c431edaefa437612e5543
BLAKE2b-256 4a2819cff202f4a53c3ea47b3c1f4ae3650bb10098c7a55a31ef9ec45470ea25

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plusminus-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 29.1 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

Hashes for plusminus-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0db8a144be279faf0335182dd0b152f519716f224fd8e8c638fcd61d65f9b90
MD5 64d0e4f7b7a1a47aecb1105042296b75
BLAKE2b-256 98db475af411b86078274a1010d4b321e759c90e96479927b29ca59310679e87

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