--- id: leval version: "1.4.0" license: MIT license_treatment: permissive maintenance: active --- # leval — Limited evaluator License: permissive · Maintenance: active · Downloads: 181.9K/mo ## What it is and what it does Leval is a Python expression evaluator designed to be safer and more flexible than ast.literal_eval() while remaining sandboxed. It parses expressions into an AST and walks it in pure Python, never using getattr or subscripting outside your control. You define which functions and variables are available, and can enforce depth and time limits on evaluation. The package offers two APIs: a simple_eval() function for straightforward cases (arithmetic, comparisons, function calls with your registered functions), and an advanced API with subclassable Evaluator and Universe classes for custom evaluation rules. Security depends entirely on what you register—any function you provide can be called with attacker-controlled arguments, and operators invoke dunder methods on your values, so avoid registering functions with side effects or objects with dangerous comparisons. Use it for: - Evaluate user-supplied mathematical or logical expressions in web forms or configuration files without executing arbitrary code. - Build a formula engine for applications where users define calculations with controlled variables and functions. - Implement conditional rule evaluation in data pipelines where expressions reference row values but cannot access the filesystem or network. - Parse and evaluate templated expressions in configuration systems with bounded complexity and execution time. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Leval evaluates limited expressions safely by parsing them into an AST and walking it in Python, supporting depth and time limits alongside optional variable and function mappings. Yes. Leval is a focused, well-maintained tool with zero dependencies, active development, and no known vulnerabilities. It solves a real security problem—safe dynamic expression evaluation—better than ast.literal_eval() for cases where you need variables and functions. Install it if you need to evaluate user or config expressions safely; skip it if you only need literal Python values. ## Install pip install leval uv add leval poetry add leval ## Installing leval Before you install: Very low friction: pure Python wheel with no runtime dependencies, and actively maintained with a release on 2026-08-13. License in practice: MIT license permits commercial and private use with minimal restrictions, requiring only attribution. Quickstart: from leval.simple import simple_eval assert simple_eval('1 + 2') == 3 assert simple_eval('x < -80 or x > 125 or x == 85', values={'x': 85}) assert simple_eval('abs(x) > 80', values={'x': -85}, functions={'abs': abs}) Requires Python 3.8 or later. Verify before relying: - Real-world performance characteristics when evaluating deeply nested or complex expressions. - Whether max_time enforcement is sufficient for your threat model, given it is cooperative rather than preemptive. ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 181.9K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags safe expression evaluation, limited ast evaluator, dynamic expression parser, sandboxed math evaluation, controlled formula evaluation, expression-evaluation, sandboxing [View on SkillFed](https://skillfed.io/packages/leval) · [View on PyPI](https://pypi.org/project/leval/)