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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file py-expression-1.3.3.tar.gz
.
File metadata
- Download URL: py-expression-1.3.3.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92d40ceb44347b2c37142903b25031d1e8c6a3a8e4ea4da908716e7e992451eb |
|
MD5 | b2ccfcdef621204409e7a179cd41540e |
|
BLAKE2b-256 | 11b0062d5a948fedb265320ca7d341bc92ab8d6b7ecdbfd131155d55d6a823b5 |