9595 Hello world! I'm CEL.
9696
9797"""
98+
9899import json # noqa: F401
99100import logging
100101import sys
103104import lark
104105
105106import celpy .celtypes
106- from celpy .adapter import (CELJSONDecoder , CELJSONEncoder , # noqa: F401
107- json_to_cel )
107+ from celpy .adapter import ( # noqa: F401
108+ CELJSONDecoder ,
109+ CELJSONEncoder ,
110+ json_to_cel ,
111+ )
108112from celpy .celparser import CELParseError , CELParser # noqa: F401
109- from celpy .evaluation import (Activation , Annotation , # noqa: F401
110- CELEvalError , CELFunction , Context , Evaluator ,
111- Result , base_functions )
113+ from celpy .evaluation import ( # noqa: F401
114+ Activation ,
115+ Annotation ,
116+ CELEvalError ,
117+ CELFunction ,
118+ Context ,
119+ Evaluator ,
120+ Result ,
121+ base_functions ,
122+ )
112123
113124# A parsed AST.
114125Expression = lark .Tree
@@ -122,11 +133,12 @@ class Runner:
122133
123134 .. todo:: add type adapter and type provider registries.
124135 """
136+
125137 def __init__ (
126- self ,
127- environment : ' Environment' ,
128- ast : lark .Tree ,
129- functions : Optional [Dict [str , CELFunction ]] = None
138+ self ,
139+ environment : " Environment" ,
140+ ast : lark .Tree ,
141+ functions : Optional [Dict [str , CELFunction ]] = None ,
130142 ) -> None :
131143 self .logger = logging .getLogger (self .__class__ .__name__ )
132144 self .environment = environment
@@ -156,11 +168,12 @@ class InterpretedRunner(Runner):
156168
157169 .. todo:: Refractor the Evaluator constructor from evaluation.
158170 """
171+
159172 def evaluate (self , context : Context ) -> celpy .celtypes .Value :
160173 e = Evaluator (
161174 ast = self .ast ,
162175 activation = self .new_activation (context ),
163- functions = self .functions
176+ functions = self .functions ,
164177 )
165178 value = e .evaluate ()
166179 return value
@@ -175,11 +188,12 @@ class CompiledRunner(Runner):
175188 Transform the AST into Python, uses :py:func:`compile` to create a code object.
176189 Uses :py:func:`eval` to evaluate.
177190 """
191+
178192 def __init__ (
179- self ,
180- environment : ' Environment' ,
181- ast : lark .Tree ,
182- functions : Optional [Dict [str , CELFunction ]] = None
193+ self ,
194+ environment : " Environment" ,
195+ ast : lark .Tree ,
196+ functions : Optional [Dict [str , CELFunction ]] = None ,
183197 ) -> None :
184198 super ().__init__ (environment , ast , functions )
185199 # Transform AST to Python.
@@ -195,9 +209,9 @@ def evaluate(self, activation: Context) -> celpy.celtypes.Value:
195209# TODO: Becomes cel_protobuf.Int32Value
196210class Int32Value (celpy .celtypes .IntType ):
197211 def __new__ (
198- cls : Type [' Int32Value' ],
199- value : Any = 0 ,
200- ) -> ' Int32Value' :
212+ cls : Type [" Int32Value" ],
213+ value : Any = 0 ,
214+ ) -> " Int32Value" :
201215 """TODO: Check range. This seems to matter for protobuf."""
202216 if isinstance (value , celpy .celtypes .IntType ):
203217 return cast (Int32Value , super ().__new__ (cls , value ))
@@ -212,17 +226,17 @@ def __new__(
212226# This approach bypasses some of the range constraints that are part of these types.
213227# It may also cause values to compare as equal when they were originally distinct types.
214228googleapis = {
215- ' google.protobuf.Int32Value' : celpy .celtypes .IntType ,
216- ' google.protobuf.UInt32Value' : celpy .celtypes .UintType ,
217- ' google.protobuf.Int64Value' : celpy .celtypes .IntType ,
218- ' google.protobuf.UInt64Value' : celpy .celtypes .UintType ,
219- ' google.protobuf.FloatValue' : celpy .celtypes .DoubleType ,
220- ' google.protobuf.DoubleValue' : celpy .celtypes .DoubleType ,
221- ' google.protobuf.BoolValue' : celpy .celtypes .BoolType ,
222- ' google.protobuf.BytesValue' : celpy .celtypes .BytesType ,
223- ' google.protobuf.StringValue' : celpy .celtypes .StringType ,
224- ' google.protobuf.ListValue' : celpy .celtypes .ListType ,
225- ' google.protobuf.Struct' : celpy .celtypes .MessageType ,
229+ " google.protobuf.Int32Value" : celpy .celtypes .IntType ,
230+ " google.protobuf.UInt32Value" : celpy .celtypes .UintType ,
231+ " google.protobuf.Int64Value" : celpy .celtypes .IntType ,
232+ " google.protobuf.UInt64Value" : celpy .celtypes .UintType ,
233+ " google.protobuf.FloatValue" : celpy .celtypes .DoubleType ,
234+ " google.protobuf.DoubleValue" : celpy .celtypes .DoubleType ,
235+ " google.protobuf.BoolValue" : celpy .celtypes .BoolType ,
236+ " google.protobuf.BytesValue" : celpy .celtypes .BytesType ,
237+ " google.protobuf.StringValue" : celpy .celtypes .StringType ,
238+ " google.protobuf.ListValue" : celpy .celtypes .ListType ,
239+ " google.protobuf.Struct" : celpy .celtypes .MessageType ,
226240}
227241
228242
@@ -237,11 +251,12 @@ class Environment:
237251
238252 .. todo:: Add adapter and provider registries to the Environment.
239253 """
254+
240255 def __init__ (
241- self ,
242- package : Optional [str ] = None ,
243- annotations : Optional [Dict [str , Annotation ]] = None ,
244- runner_class : Optional [Type [Runner ]] = None
256+ self ,
257+ package : Optional [str ] = None ,
258+ annotations : Optional [Dict [str , Annotation ]] = None ,
259+ runner_class : Optional [Type [Runner ]] = None ,
245260 ) -> None :
246261 """
247262 Create a new environment.
@@ -277,9 +292,7 @@ def compile(self, text: str) -> Expression:
277292 return ast
278293
279294 def program (
280- self ,
281- expr : lark .Tree ,
282- functions : Optional [Dict [str , CELFunction ]] = None
295+ self , expr : lark .Tree , functions : Optional [Dict [str , CELFunction ]] = None
283296 ) -> Runner :
284297 """Transforms the AST into an executable runner."""
285298 self .logger .info ("Package %r" , self .package )
0 commit comments