Skip to main content

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

Metadata

Release files for py-expression 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-expression 1.3.3
File Size Uploaded
py-expression-1.3.3.tar.gz 12.1 kB Details

Release files / py-expression-1.3.3.tar.gz

Download URL py-expression-1.3.3.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
92d40ceb44347b2c37142903b25031d1e8c6a3a8e4ea4da908716e7e992451eb
BLAKE2b-256 checksum
How to use checksums
11b0062d5a948fedb265320ca7d341bc92ab8d6b7ecdbfd131155d55d6a823b5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.3.3 This release

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.2.0

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.10

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page