Pure Python Common Expression Language (CEL)
cel-python is a pure Python implementation of Google's Common Expression Language (CEL), currently at version 0.5.0. It provides common semantics for expression evaluation, enabling applications to interoperate through a fast, embeddable expression language. The library aims for minimal dependencies and is primarily used for security policies, such as within Cloud Custodian. Its release cadence is tied to its primary consumers.
Warnings
- gotcha CEL's type semantics differ from native Python, and type coercion is generally not performed implicitly. This can lead to unexpected `TypeError`s if not handled explicitly in expressions.
- gotcha Division behavior (`/`) in CEL truncates towards zero, which is different from Python's integer division (`//`) that truncates towards negative infinity. This can lead to subtle differences in arithmetic results.
- gotcha CEL runtime errors are mapped to Python exceptions, specifically `celpy.evaluation.EvalError`. These errors provide a message similar to the CEL error, along with an underlying Python exception.
Install
-
pip install cel-python
Imports
- Environment
from celpy import Environment
- celtypes
from celpy import celtypes
Quickstart
from celpy import Environment, celtypes
decls = {"name": celtypes.StringType}
env = Environment(annotations=decls)
ast = env.compile("'Hello world! I\'m " + name + ".'")
out = env.program(ast).evaluate({"name": "CEL"})
print(out)
# Expected: Hello world! I'm CEL.