celparser - Python Common Expression Language Parser
A Python implementation of Google's Common Expression Language (CEL) parser and evaluator.
Overview
celparser is a Python package that provides parsing and evaluation of Google's Common Expression Language (CEL). CEL is a simple expression language that lets you check whether a condition is true or false at runtime. It's designed to be simple, portable, and safe to execute in constrained environments.
CEL is used in various Google products and open-source projects for policy enforcement, data filtering, and configuration.
Features
- Parse and evaluate CEL expressions
- Support for basic CEL syntax and operations
- Support for common data types (string, number, boolean, lists, maps)
- Comprehensive error reporting
- Simple API for integration with Python applications
- Built-in functions for common operations
- Extensible with custom functions
Installation
Using pip
pip install celparser
Using uv
uv pip install celparser
From source
git clone https://github.com/celparser/celparser.git
cd celparser
pip install .
Requirements
- Python 3.8 or higher
Usage Examples
Basic Usage
from celparser import compile
# Compile an expression
expression = compile("a + b * 2")
# Evaluate with a context
result = expression({"a": 10, "b": 5})
print(result) # Output: 20
# Reuse the same expression with different contexts
result2 = expression({"a": 5, "b": 3})
print(result2) # Output: 11
Using parse and evaluate directly
from celparser.parser import parse
from celparser.evaluator import evaluate
# Parse the expression into an AST
ast = parse("(a + b) * 2")
# Evaluate the AST with a context
result = evaluate(ast, {"a": 10, "b": 5})
print(result) # Output: 30
Working with Different Data Types
from celparser import compile
context = {
"name": "Alice",
"age": 30,
"isAdmin": True,
"tags": ["user", "member"],
"profile": {
"email": "alice@example.com",
"active": True
}
}
# String concatenation
expr1 = compile("name + ' is ' + string(age) + ' years old'")
print(expr1(context)) # Output: "Alice is 30 years old"
# Ternary operator
expr2 = compile("isAdmin ? 'Administrator' : 'Regular user'")
print(expr2(context)) # Output: "Administrator"
# List indexing
expr3 = compile("tags[0] + ' account'")
print(expr3(context)) # Output: "user account"
# Map access
expr4 = compile("profile.email")
print(expr4(context)) # Output: "alice@example.com"
# Built-in functions
expr5 = compile("size(tags)")
print(expr5(context)) # Output: 2
expr6 = compile("contains(tags, 'admin')")
print(expr6(context)) # Output: False
expr7 = compile("type(age)")
print(expr7(context)) # Output: "int"
expr8 = compile("startsWith(name, 'A')")
print(expr8(context)) # Output: True
Error Handling
from celparser import compile
from celparser.errors import CELSyntaxError, CELEvaluationError
# Syntax error
try:
expr = compile("a + * b")
except CELSyntaxError as e:
print(f"Syntax error caught: {e}")
# Evaluation error (division by zero)
try:
expr = compile("a / b")
result = expr({"a": 10, "b": 0})
except CELEvaluationError as e:
print(f"Evaluation error caught: {e}")
# Type error
try:
expr = compile("a < b")
result = expr({"a": 10, "b": "not a number"})
except CELEvaluationError as e:
print(f"Type error caught: {e}")
# Undefined variable
try:
expr = compile("a + b", allow_undeclared_vars=False)
result = expr({"a": 10}) # 'b' is missing
except CELEvaluationError as e:
print(f"Undefined variable error caught: {e}")
Complex Example: Permission Checking
from celparser import compile
# User data
user = {
"name": "Alice",
"role": "editor",
"department": "Engineering",
"permissions": ["read", "write"],
"active": True,
"manager": {
"name": "Bob",
"role": "admin"
},
"projects": [
{"id": "proj1", "access": "full"},
{"id": "proj2", "access": "read-only"}
]
}
# Complex permission check
permission_check = compile("""
active &&
(role == 'admin' ||
(contains(permissions, 'write') &&
(department == 'Engineering' || manager.role == 'admin')))
""")
has_permission = permission_check(user)
print(f"User has required permissions: {has_permission}") # Output: True
# Complex data access and manipulation
project_info = compile("""
size(projects) > 0 ?
projects[0].id + ' (' + projects[0].access + ')' :
'No projects'
""")
result = project_info(user)
print(f"First project info: {result}") # Output: "proj1 (full)"
API Reference
Main Functions
compile(expression, allow_undeclared_vars=True): Compile a CEL expression for later evaluationparse(expression): Parse a CEL expression into an ASTevaluate(ast, context=None, allow_undeclared_vars=True): Evaluate a parsed CEL expression
Classes
Evaluator: Main class for evaluating CEL expressionsCELSyntaxError: Exception raised for syntax errorsCELEvaluationError: Base exception for evaluation errorsCELTypeError: Exception raised for type errorsCELUndefinedError: Exception raised for undefined variables
Built-in Functions
size(obj): Get the size of a string, list, or mapcontains(container, item): Check if a container contains an itemstartsWith(s, prefix)/starts_with(s, prefix): Check if a string starts with a prefixendsWith(s, suffix)/ends_with(s, suffix): Check if a string ends with a suffixmatches(s, pattern): Check if a string matches a regex patternint(value): Convert a value to an integerfloat(value): Convert a value to a floatbool(value): Convert a value to a booleanstring(value): Convert a value to a stringtype(value): Get the type of a value as a string
Development
Setup
- Clone the repository
- Install dependencies using
uv sync --all-extras --dev - Run tests using
uv run pytest tests
Publishing to PyPI
This project uses GitHub Actions to automatically publish to PyPI when a new release is created.
For Maintainers
To publish a new version to PyPI:
- Update the version number in
setup.py - Create a new release on GitHub with a tag matching the version (e.g.,
v0.1.0) - The GitHub Action will automatically build and publish the package to PyPI
Setting up PyPI API Token
To set up the PyPI API token for automated publishing:
- Create an API token on PyPI (https://pypi.org/manage/account/token/)
- Add the token as a GitHub secret named
PYPI_API_TOKENin the repository settings
Metadata
Release files for celparser 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| celparser-0.1.2.tar.gz | 20.3 kB | Without provenance | Details for celparser-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| celparser-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for celparser-0.1.2-py3-none-any.whl |
Total release size: 40.8 kB
Release files / celparser-0.1.2.tar.gz
| Download URL | celparser-0.1.2.tar.gz |
|---|---|
| Size | 20.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e91715b8a1f1b162cdb04dd4b1c618666427eb308803b855ff70107c22c305c0
|
|
BLAKE2b-256 checksum How to use checksums |
2616d29ebee431217ff058423c6f511aa936477a4ab64e7747c15e2a2a877867
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.12
|
Release files / celparser-0.1.2-py3-none-any.whl
| Download URL | celparser-0.1.2-py3-none-any.whl |
|---|---|
| Size | 20.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23d6363218132b8da863b20c2c2eb818dae54fdf16d394167dd38f39c7113e0b
|
|
BLAKE2b-256 checksum How to use checksums |
0f97af15f34126e5948f575c5eba3b7aec1b31e55865f2af629c2175cce3fc25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.12
|
Security for celparser 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.