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.
pip install cel-pythonVerified import paths — ran on the pinned version, not inferred.
This example demonstrates compiling and evaluating a simple CEL expression with a string variable, using the Environment and celtypes classes.
Ensure explicit type conversion within CEL expressions (e.g., `double(1) + 2.5` instead of `1 + 2.5`) if mixed types are intended, or ensure consistent typing of inputs.
Be aware of the truncation behavior for division operations and test expressions thoroughly, especially with negative numbers.
Implement proper try-except blocks to catch `celpy.evaluation.EvalError` when evaluating expressions to handle potential runtime issues gracefully.
Install the library using pip: `pip install cel-python`. Ensure imports are `import celpy` or `from celpy import ...`.
Ensure all variables and functions used in the CEL expression are either provided in the `activation` dictionary passed to `evaluate()` or explicitly declared in the `celpy.Environment` configuration.
Adjust the types of the arguments in the CEL expression to match an available function overload, or define a custom function binding in the `celpy.Environment` that supports the desired argument types.
Carefully review the CEL expression for typos, missing operators, unmatched parentheses, or other grammatical mistakes.