@@ -122,11 +122,7 @@ def __format_exponential(self, arg: celtypes.Value, precision: int) -> str:
122122 raise celpy .CELEvalError (msg )
123123
124124 def __format_int (self , arg : celtypes .Value ) -> str :
125- if (
126- isinstance (arg , celtypes .IntType )
127- or isinstance (arg , celtypes .UintType )
128- or isinstance (arg , celtypes .DoubleType )
129- ):
125+ if isinstance (arg , (celtypes .IntType , celtypes .UintType , celtypes .DoubleType )):
130126 result = self .__validate_number (arg )
131127 if result is not None :
132128 return result
@@ -138,9 +134,7 @@ def __format_int(self, arg: celtypes.Value) -> str:
138134 raise celpy .CELEvalError (msg )
139135
140136 def __format_hex (self , arg : celtypes .Value ) -> str :
141- if isinstance (arg , celtypes .IntType ):
142- return f"{ arg :x} "
143- if isinstance (arg , celtypes .UintType ):
137+ if isinstance (arg , (celtypes .IntType , celtypes .UintType )):
144138 return f"{ arg :x} "
145139 if isinstance (arg , celtypes .BytesType ):
146140 return arg .hex ()
@@ -153,9 +147,7 @@ def __format_hex(self, arg: celtypes.Value) -> str:
153147 raise celpy .CELEvalError (msg )
154148
155149 def __format_oct (self , arg : celtypes .Value ) -> str :
156- if isinstance (arg , celtypes .IntType ):
157- return f"{ arg :o} "
158- if isinstance (arg , celtypes .UintType ):
150+ if isinstance (arg , (celtypes .IntType , celtypes .UintType )):
159151 return f"{ arg :o} "
160152 msg = (
161153 "error during formatting: octal clause can only be used on integers, was given "
@@ -164,17 +156,12 @@ def __format_oct(self, arg: celtypes.Value) -> str:
164156 raise celpy .CELEvalError (msg )
165157
166158 def __format_bin (self , arg : celtypes .Value ) -> str :
167- if isinstance (arg , celtypes .IntType ):
168- return f"{ arg :b} "
169- if isinstance (arg , celtypes .UintType ):
170- return f"{ arg :b} "
171- if isinstance (arg , celtypes .BoolType ):
159+ if isinstance (arg , (celtypes .IntType , celtypes .UintType , celtypes .BoolType )):
172160 return f"{ arg :b} "
173161 msg = (
174162 "error during formatting: only integers and bools can be formatted as binary, was given "
175163 f"{ self .__type_str (type (arg ))} "
176164 )
177-
178165 raise celpy .CELEvalError (msg )
179166
180167 def __format_string (self , arg : celtypes .Value ) -> str :
@@ -189,6 +176,7 @@ def __format_string(self, arg: celtypes.Value) -> str:
189176 decoded = arg .decode ("utf-8" , errors = "replace" )
190177 # Collapse any contiguous placeholders into one
191178 return re .sub ("\\ ufffd+" , "\ufffd " , decoded )
179+
192180 if isinstance (arg , celtypes .DoubleType ):
193181 result = self .__validate_number (arg )
194182 if result is not None :
@@ -206,7 +194,7 @@ def __format_string(self, arg: celtypes.Value) -> str:
206194 if isinstance (arg , celtypes .MapType ):
207195 return self .__format_map (arg )
208196 if isinstance (arg , celtypes .StringType ):
209- return f" { arg } "
197+ return arg
210198 if isinstance (arg , celtypes .TimestampType ):
211199 base = arg .isoformat ()
212200 if arg .getMilliseconds () != 0 :
@@ -215,31 +203,11 @@ def __format_string(self, arg: celtypes.Value) -> str:
215203 return "unknown"
216204
217205 def __format_list (self , arg : celtypes .ListType ) -> str :
218- result = "["
219- for i in range (len (arg )):
220- if i > 0 :
221- result += ", "
222- result += self .__format_string (arg [i ])
223- result += "]"
224- return result
206+ return "[" + ", " .join (self .__format_string (val ) for val in arg ) + "]"
225207
226208 def __format_map (self , arg : celtypes .MapType ) -> str :
227- m = {}
228- for cel_key , cel_val in arg .items ():
229- key = self .__format_string (cel_key )
230- val = self .__format_string (cel_val )
231- m [key ] = val
232-
233- m = dict (sorted (m .items ()))
234-
235- result = "{"
236- for i , (key , val ) in enumerate (m .items ()):
237- if i > 0 :
238- result += ", "
239- result += key + ": " + val
240-
241- result += "}"
242- return result
209+ m = {self .__format_string (cel_key ): self .__format_string (cel_val ) for cel_key , cel_val in arg .items ()}
210+ return "{" + ", " .join (key + ": " + val for key , val in sorted (m .items ())) + "}"
243211
244212 def __format_duration (self , arg : celtypes .DurationType ) -> str :
245213 return f"{ arg .seconds + Decimal (arg .microseconds ) / Decimal (1_000_000 ):f} s"
0 commit comments