Skip to main content

+/- plusminus is a module that builds on the pyparsing infixNotation helper method to build easy-to-code and

Project description

plusminus

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

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

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:

()
|x|
∩ & ∪ | - ^ ∆ (set operations)
**
-
* / // × ÷ mod
+ -
< > <= >= == != ≠ ≤ ≥
in ∈ ∉ (element in/not in set)
not
and ∧
or ∨
? : (ternary)

Functions can be called:

abs    ceil   max
round  floor  str
trunc  min    bool

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.

An ArithmeticParser class is also defined, with more extensive operators, including:

!     - factorial  
°     - degree-radian conversion
√ ⁿ√  - square root and n'th root (2-9)
⁻¹  ⁰  ¹  ²  ³ - common exponents as superscripts

and additional pre-defined functions:

sin    asin  rad    gcd
cos    acos  deg    lcm
tan    atan  ln     rnd
sgn    sinh  log    randint
gamma  cosh  log2
hypot  tanh  log10

This parser class can be used in applications using algebra or trigonometry expressions.

Custom expressions can be defined using a simple API. Example parsers are included for other specialized applications and domains:

  • dice rolling ("3d6 + d20")
  • time delta expressions ("today() + 2d + 12h")
  • retail and business expressions ("20% off of 19.99")
  • combinatoric expressions ("6C2" or "5P3" )

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

Uploaded Source

Built Distribution

plusminus-0.7.0-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plusminus-0.7.0.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for plusminus-0.7.0.tar.gz
Algorithm Hash digest
SHA256 9e769188b6784b6fae3c8e1266940d8d2bc5b733c28fe0e07825d9e5219fcecd
MD5 574398fe331f5ccce2dd68d80b812cb9
BLAKE2b-256 61d3f9a96cd35a04ccdc9dea30d766ac66cb7ff9f66eff722f977a786bf2a1e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plusminus-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for plusminus-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2889d75b99c1d29a79bce8f92f49c4c19f0fa4715d96d12238123a1f10751f30
MD5 bb3fe61c9d73b447ce961e7f1a6efa2a
BLAKE2b-256 628539b699c97f609d27b372658a26d57e9f361dc463926bcf594ef23d2bfa49

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