Parser and evaluator for CEL in Python using Antlr
Project description
This project is not ready for production yet.
Parser and evaluator for CEL in Python using ANTLR4
This library provides parser and evaluator for Common Expression Language (CEL) expressions in Python projects. CEL is a language developed by Google that allows for safe and fast evaluation of expressions in a wide variety of applications, such as policy engines, rule engines, and more.
Features
Parse and evaluate CEL expressions directly within TypeScript projects.
Support for common arithmetic operations, logical operations, and comparisons.
Extensible design for adding custom functions and variables.
Error handling during parsing with custom error listeners.
Context-based evaluation to support dynamic expression evaluation.
Installation
You can install gresb_cel_python via pip:
pip install gresb_cel_python
Usage
Evaluate expression
To use the CEL parser and evaluator, you can instantiate the Runtime class with a CEL expression, and then evaluate it with a given context.
from gresb_cel_pyhon import Runtime
# Define a CEL expression
expression = "a + b * 10"
# Create a Runtime instance with the expression
runtime = Runtime(expression)
# Define a context with variables
context = {
"a": 5,
"b": 3
}
# Evaluate the expression with the context
result = runtime.evaluate(context)
print(f"Result: {result}") # Output: Result: 35
Parsing Valiation
from cel_in_py import Runtime
# Define a CEL expression
expression = "a + b * 10"
# Create a Runtime instance with the expression
runtime = Runtime(expression)
# Define a context with variables
context = {
"a": 5,
"b": 3
}
# Evaluate the expression with the context
result = runtime.evaluate(context)
print(f"Result: {result}") # Output: Result: 35
Advanced Usage
The VisitorInterp class allows for extending the functionality by adding custom functions or modifying the evaluation logic.
from cel_in_py.visitor_interp import VisitorInterp
# Define a custom function
def custom_function(x):
return x * x
# Extend the visitor with the custom function
class CustomVisitor(VisitorInterp):
def __init__(self, context):
super().__init__(context)
self.function_registry["custom_function"] = custom_function
# Use the custom visitor in the runtime
expression = "custom_function(5)"
runtime = Runtime(expression)
visitor = CustomVisitor({})
result = visitor.visit(runtime.ast)
print(f"Result: {result}") # Output: Result: 25
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gresb_cel_python-0.0.2.tar.gz
.
File metadata
- Download URL: gresb_cel_python-0.0.2.tar.gz
- Upload date:
- Size: 44.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae728c22a48b25248ea729d49c12ecb0243c7787dd637047d10c19cf72a7d11a |
|
MD5 | 9a58da48263ed5bfc091b48968e3b047 |
|
BLAKE2b-256 | f9b685ee8e16c15be8c17de159f04dc37b63bdcff00592b963486873345abc0e |
File details
Details for the file gresb_cel_python-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: gresb_cel_python-0.0.2-py3-none-any.whl
- Upload date:
- Size: 41.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd09ea0c1c1556c2836edfd8ccaa98bce1cc85896273ee6b382cecddb5b12d3b |
|
MD5 | 50056265632e2f11033bb0a870ccda69 |
|
BLAKE2b-256 | 5dcc1cc70f67ff3940795de62e44c1cd3c56d7dbcd227f5a0be92eefd60b74b9 |