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

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.1.1.tar.gz (16.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for evalis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b1dde1a3ac0fc3fff35251c56b6af9b54dd073f8206450e4b1c333d07545824a
MD5 e218079c738378b1323eac26edb4afeb
BLAKE2b-256 570bb555b4da03feb342ec33ae651a18d6d60e6b5765c93f6b7edabeeb83edd6

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