Skip to main content

parser and solve expressions

Project description

Description

Py-expression is an extensible expression evaluator and parser. Besides the operators, functions, variables, objects and arrays that are supported; it is possible to extend it with your own functions, operators, etc.

Features

  • Parse and evaluate
    • Arithmetic operators
    • assignment operators
    • comparison operators
    • Logical operators
    • Bitwise Operators
    • Variables
    • Constants
    • Functions
    • Objects
    • Arrayas
    • Enums
  • Simplify math operations where operands are constant
  • It allows to extend the model by adding or overwriting operators, functions and enums
  • Supports multiline expressions using the semicolon character to separate them
  • The evaluation receives the context where the variables will be read, written, and created. This context must be a dictionary or a class derived from a dictionary
  • When parsing a string that contains expression, an expression object is returned, which can be reused to evolve the expression with different contexts, in this way the performance is notably improved.
  • You can create a new expression object using expression objects and combining them with operators

Wiki

Home

Use

Exp

Exp is the main class of the library that contains the methods to parse, evaluate, get info of expression, etc . In order to use the library you need to create an instance of this class:

from py_expression.core import Exp
exp = Exp()

Parse

from py_expression.core import Exp
exp = Exp()
operand =exp.parse('a+4')

Eval

from py_expression.core import Exp

exp = Exp()
operand =exp.parse('a+4')
result = exp.eval(operand,{"a":2})
from py_expression.core import Exp

exp = Exp()
operand =exp.parse('a+4')
result = operand.eval({"a":2})
from py_expression.core import Exp

exp = Exp()
result =exp.parse('a+4').eval({"a":2})

Work with expressions

reuse the parsed expression:

from py_expression.core import Exp

exp = Exp()
op = exp.parse('sin(x)') 
xs=[]
ys=[] 
for x in range(-100,100):
    y=op.eval({"x":x})
    xs.append(x)
    ys.append(y)  

create a new expression based on two or more parsed expressions:

from py_expression.core import Exp

exp = Exp()
op1 = exp.parse('a+1')
op2 = exp.parse('b')
op3 = (op1+op2)*(op1-op2) >= (op1*2)

resutl1= op3.eval({"a":1,"b":2})
resutl2= op3.eval({"a":5,"b":9})

print(resutl1)
print(resutl2)

Project Examples

Test Graph

In this project, the py-expression library is used to parse and evaluate expressions that a variable uses (in this case x) and the result is assigned to y. then the point (x,y) is shown in a diagram. In this example x takes the values from -100 to 100

Lib Opencv

Extend the expression library by adding enums and related functions to opencv

Test Opencv

In this project, the expression library and an opencv library that adds enums and functions is used to execute multi-line expressions that transform an image

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

py-expression-1.3.3.tar.gz (12.1 kB view hashes)

Uploaded Source

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