11# Python sandboxed expression parser
22
33[ ![ PyPI] ( https://img.shields.io/pypi/v/expression-parser.svg )] ( https://pypi.python.org/pypi/expression-parser )
4- [ ![ Build
5- Status] ( https://travis-ci.org/lhelwerd/expression-parser.svg?branch=master )] ( https://travis-ci.org/lhelwerd/expression-parser )
6- [ ![ Coverage
7- Status] ( https://coveralls.io/repos/github/lhelwerd/expression-parser/badge.svg?branch=master )] ( https://coveralls.io/github/lhelwerd/expression-parser?branch=master )
4+ [ ![ PyPI Versions] ( https://img.shields.io/pypi/pyversions/expression-parser.svg )] ( https://pypi.python.org/pypi/expression-parser/#files )
5+ [ ![ CI Status] ( https://github.com/lhelwerd/expression-parser/actions/workflows/ci.yml/badge.svg )] ( https://github.com/lhelwerd/expression-parser/actions/workflows/ci.yml )
6+ [ ![ Coverage Status] ( https://coveralls.io/repos/github/lhelwerd/expression-parser/badge.svg?branch=master )] ( https://coveralls.io/github/lhelwerd/expression-parser?branch=master )
87
9- This parser can calculate the results of a single simple expression,
8+ This parser calculates the results of a single simple expression,
109disallowing any complicated functions or control structures, with support for
1110custom variable and function environment contexts.
1211
1312## Features
1413
1514- Support for all boolean, binary, unary, and comparative operators as in
16- Python itself.
15+ Python itself, including short-circuiting and chained comparison logic .
1716- Support for inline ` if..else ` expressions.
1817- Support for assignments and augmented assignments like ` += ` , only if enabled
1918 explicitly.
2019- All other control structures and multiple expressions are disallowed.
2120- Isolation from execution context using a restricted scope.
2221- Separate scope for variables and functions to avoid abusing one or the other.
23- - Function calls may use specific keyword arguments.
22+ - Function calls must use direct names (function attributes and lambda
23+ functions are rejected) and must not provide starred arguments.
2424- Errors from parsing or evaluating the expression are raised as ` SyntaxError `
2525 with appropriate context parameters to make error display easier (works with
2626 default traceback output).
2727- A successful parse yields the result of the evaluated expression. Successful
2828 assignments of variables are stored in a property ` modified_variables ` .
2929 A separate property ` used_variables ` provides a set of variable names used in
3030 the evaluation of the expression excluding the assignment targets.
31- - Supports both Python 2.7 and 3.6 AST syntax trees.
31+ - Supports Python 2.7 and 3.6-3.14 AST syntax trees.
3232- Python 3+ conventions are used whenever possible: Specifically, the division
3333 operator ` / ` always returns floats instead of integers, and ` True ` , ` False `
3434 and ` None ` are reserved named constants and cannot be overridden through the
@@ -47,14 +47,14 @@ Not supported (often by design):
4747
4848## Requirements
4949
50- The expression parser has been tested to work on Python 2.7 and 3.6 . This
50+ The expression parser has been tested to work on Python 2.7 and 3.8+ . This
5151package has no other dependencies and works with only core Python modules.
5252
5353## Installation
5454
5555Install the latest version from PyPI using:
5656
57- ```
57+ ``` bash
5858pip install expression-parser
5959```
6060
@@ -92,8 +92,11 @@ arguments, such as `parser = expression.Expression_Parser()`, or by passing the
9292scope along to it in dictionaries of values and functions, respectively:
9393
9494``` python
95- parser = expression.Expression_Parser(variables = variables, functions = functions,
96- assignment = bool (assignment))
95+ parser = expression.Expression_Parser(
96+ variables = variables,
97+ functions = functions,
98+ assignment = bool (assignment)
99+ )
97100```
98101
99102You can also set a new dictionary for ` variables ` , and enable or disable
@@ -103,22 +106,22 @@ via the properties of the created object.
103106Now you can use this parser to evaluate any valid expression:
104107
105108``` python
106- print (parser.parse(' 1+2' ))
109+ >> print (parser.parse(' 1+2' ))
1071103
108- print (parser.parse(' pi > 3' ))
111+ >> print (parser.parse(' pi > 3' ))
109112True
110- print (parser.parse(' int(log(e))' ))
113+ >> print (parser.parse(' int(log(e))' ))
1111141
112115```
113116
114117## Development
115118
116- - [ Travis ] ( https://travis-ci.org /lhelwerd/expression-parser ) is used to run
117- unit tests and report on coverage.
119+ - [ GitHub Actions ] ( https://github.com /lhelwerd/expression-parser/actions ) is
120+ used to run unit tests and report on coverage.
118121- [ Coveralls] ( https://coveralls.io/github/lhelwerd/expression-parser ) receives
119122 coverage reports and tracks them.
120- - You can perform local lint checks, tests and coverage during development
121- using ` make pylint ` , ` make test ` and ` make coverage ` , respectively.
123+ - You can perform local lint checks, tests or coverage during development using
124+ ` make ruff ` , ` make pylint ` , ` make test ` or ` make coverage ` , respectively.
122125- We publish releases to [ PyPI] ( https://pypi.python.org/pypi/expression-parser )
123126 using ` make release ` which performs lint and unit test checks.
124127
0 commit comments