Skip to main content

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 + 38
  • 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 evaluate
  • context (dict): Variable context for the expression
  • options (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 evaluate
  • context (dict): Variable context for evaluation
  • options (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): Return None instead 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

evalis-0.2.0.tar.gz (18.0 kB view details)

Uploaded Source

File details

Details for the file evalis-0.2.0.tar.gz.

File metadata

  • Download URL: evalis-0.2.0.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for evalis-0.2.0.tar.gz
Algorithm Hash digest
SHA256 109ff557efc6ab706f1b1c02c17607c326d6cd6b40c90a15161c7ff85b4b2ace
MD5 16300f14aafc1b340e8d81735bb81022
BLAKE2b-256 57f65f7b1d29ac6c4ff7098c262ea3f31ce2b03c21179b890b6c770c2a046d54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page