A polyglot, safe, user-friendly expression evaluator.
Project description
Evalis
A secure, user-friendly expression evaluator for Python.
Early Release: This is an early release with core functionality. More features are in active development. Check the main project for roadmap and updates.
Installation
pip install evalis
Quick Start
from evalis import evaluate_expression
context = {"a": {"b": 5}}
value = evaluate_expression("a.b + 2", context)
print(value) # 7
Features
- Safe evaluation - No
eval(). No access to global namespace. - Simple syntax - Familiar expressions with property access, operators, and comprehensions.
- Type-safe - Full type hints included for static type checking.
Supported Operations
- Arithmetic:
+,-,*,/ - Comparison:
==,!=,<,<=,>,>= - Logical:
and,or,not - Property access:
obj.property,obj['key'] - Array access:
arr[0] - List comprehensions:
[x * 2 for x in numbers] - Membership:
x in collection - String literals: Both
'single'and"double"quotes supported
Note on + operator:
- Numbers:
5 + 3→8 - Strings:
"hello" + "world"→"helloworld"(coerces primitives to string) - Arrays:
[1, 2] + [3, 4]→[1, 2, 3, 4] - Mixed types (list + string) will raise
EvalisError
API Reference
evaluate_expression(expression, context, options)
Evaluates an expression string with the given context.
Parameters:
expression(str): The expression to evaluatecontext(dict): Variable context for the expressionoptions(EvaluatorOptions, optional): Evaluation options
Returns: The evaluated result
Example:
from evalis import evaluate_expression
result = evaluate_expression("x * 2", {"x": 21})
# result = 42
parse_ast(expression)
Parses an expression into an AST without evaluating it.
Parameters:
expression(str): The expression to parse
Returns: An AST node representing the expression
Example:
from evalis import parse_ast
ast = parse_ast("a + b")
# Returns: BinaryOpNode(op=BinaryOpType.ADD, left=..., right=...)
evaluate_ast(node, context, options)
Evaluates a pre-parsed AST node.
Parameters:
node(EvalisNode): The AST node to evaluatecontext(dict): Variable context for evaluationoptions(EvaluatorOptions, optional): Evaluation options
Returns: The evaluated result
Example:
from evalis import parse_ast, evaluate_ast
ast = parse_ast("x + 1")
result = evaluate_ast(ast, {"x": 5})
# result = 6
EvaluatorOptions
Configuration options for evaluation.
Fields:
should_null_on_bad_access(bool, default=False): ReturnNoneinstead of raising errors on invalid property access
Example:
from evalis import evaluate_expression, EvaluatorOptions
options = EvaluatorOptions(should_null_on_bad_access=True)
result = evaluate_expression("foo.missing", {}, options)
# result = None (instead of raising an error)
More Information
This is the Python implementation of Evalis. For more details about the project:
License
MIT
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 evalis-0.2.1.tar.gz.
File metadata
- Download URL: evalis-0.2.1.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
500891c873f36eb2ed6f0451296dbb513ef8d0ed64b1a5722a3d02fffbe053a8
|
|
| MD5 |
ff62313b05f2b838ccaa534960d25a33
|
|
| BLAKE2b-256 |
5cba5f831b4bc5acf9a0dce6d23047a0af7dc1e3ccc5c33c85b30a92924f0da2
|