Code Monkey home page Code Monkey logo

plusminus's Introduction

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.

plusminus's People

Contributors

nicolasalmerge avatar ptmcg avatar toonarmycaptain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

plusminus's Issues

More variances in variable names

Hi,

Thanks for your great work on pyparsing and plusminus.

I am searching for a parser working on pandas Series with variables.
This looks to work out of the box with plusminus (mapping all series of a dataframe to variables)
For a 1:1 adaption it would be fine to have a different identifier definition.

As the data columns i am working (they come from external CSV) on may contain spaces i would like to define a variable as

var_name = pp.QuotedString('[', endQuoteChar=']').setName("identifier")

which currently requires a copy and paste of your library.

Christoph

Refactor parsers to separate Python eval() features from standard Arithmetic features

plusminus is a project of 2 minds:

  • being a safe alternative to Python eval()
  • implementing common arithmetic notations and operations, often using Unicode operators or notation that are not supported in Python (such as |x| for abs(x))

These two goals should be split into separate parser classes, so that "programming-ish" concepts do not necessarily bleed over into arithmetic notations, and arithmetic expressions like |x| are not included in safe Python eval() support.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.