Python CEL - Common Expression Language

Fast, Safe, and Expressive evaluation of Google's Common Expression Language (CEL) in Python, powered by Rust.
The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, and safety. This Python package wraps the Rust implementation cel-interpreter v0.10.0, providing microsecond-level expression evaluation with seamless Python integration.
π Use Cases
- π‘οΈ Policy Enforcement: Define access control rules that can be updated without code changes
- βοΈ Configuration Validation: Validate complex settings with declarative rules
- π Data Transformation: Transform and filter data with safe, portable expressions
- π Business Rules: Implement decision logic that business users can understand
- π Query Filtering: Build dynamic filters for databases and APIs
- π― Feature Flags: Create sophisticated feature toggle conditions
Installation
pip install common-expression-language
Or using uv:
uv add common-expression-language
After installation, both the Python library and the cel command-line tool will be available.
π Full Documentation: https://python-common-expression-language.readthedocs.io/
Quick Start
Python API
from cel import evaluate
result = evaluate("1 + 2")
result = evaluate("'Hello ' + 'World'")
result = evaluate("age >= 18", {"age": 25})
result = evaluate(
'user.role == "admin" && "write" in permissions',
{
"user": {"role": "admin"},
"permissions": ["read", "write", "delete"]
}
)
Command Line Interface
cel '1 + 2'
cel 'age >= 18' --context '{"age": 25}'
cel --interactive
Custom Functions
from cel import Context, evaluate
def calculate_discount(price, rate):
return price * rate
context = Context()
context.add_function("calculate_discount", calculate_discount)
context.add_variable("price", 100)
result = evaluate("price - calculate_discount(price, 0.1)", context)
Real-World Example
from cel import evaluate, Context
policy = """
user.role == "admin" ||
(resource.owner == user.id && current_hour >= 9 && current_hour <= 17)
"""
context = Context()
context.update({
"user": {"id": "alice", "role": "user"},
"resource": {"owner": "alice"},
"current_hour": 14
})
access_granted = evaluate(policy, context)
Features
- β
Fast Evaluation: Microsecond-level expression evaluation via Rust
- β
Rich Type System: Integers, floats, strings, lists, maps, timestamps, durations
- β
Python Integration: Seamless type conversion and custom function support
- β
CLI Tools: Interactive REPL and batch processing capabilities
- β
Safety First: Non-Turing complete, safe for untrusted expressions
Documentation
π Complete documentation available at: https://python-common-expression-language.readthedocs.io/
Building Documentation Locally
To build and serve the documentation locally:
uv sync --group docs
uv run --group docs mkdocs build
uv run --group docs mkdocs serve
The documentation will be available at http://localhost:8000
Development
Testing
uv run pytest
uv run pytest --cov=cel
uv run --group docs pytest tests/test_docs.py -v
Building from Source
uv sync --dev
uv run maturin develop
uv run pytest
Contributing
Contributions are welcome! Please see our documentation for:
License
This project is licensed under the same terms as the original cel-interpreter crate.
Resources