@@ -187,7 +187,10 @@ def __init__(self, expression, func=None):
187187 item = "" .join (item )
188188 if item in self ._mathematical_constants :
189189 item = self ._mathematical_constants [item ]
190- self ._expression .append (float (item ))
190+ try :
191+ self ._expression .append (float (item ))
192+ except ValueError :
193+ raise ExpressionFormatException ("Could not convert string to float: '{}'" .format (item ))
191194
192195 def __str__ (self ):
193196 """
@@ -234,6 +237,7 @@ def _calculate_boolean_expression(self):
234237 def _calculate_mathematical_expression (self ):
235238 operation = None
236239 first_negative = False
240+
237241 # Validate first negative numbers in expression
238242 if self ._expression [0 ] == "-" :
239243 first_negative = True
@@ -254,7 +258,7 @@ def _calculate_mathematical_expression(self):
254258 # Calculate high priority mathematical operations
255259 new_expression = []
256260 for i in self ._expression :
257- if i in ("*" , "/" , "%" , "//" ,):
261+ if i in ("*" , "/" , "%" , "//" , "**" ):
258262 operation = i
259263 elif operation :
260264 if operation == "*" :
@@ -265,6 +269,8 @@ def _calculate_mathematical_expression(self):
265269 new_expression [- 1 ] %= i
266270 elif operation == "//" :
267271 new_expression [- 1 ] //= i
272+ elif operation == "**" :
273+ raise UnsupportedMathematicalOperationException ("We do not support '{}' operation" .format (i ))
268274 operation = None
269275 else :
270276 if first_negative :
@@ -310,11 +316,27 @@ def value(self):
310316 for i , v in enumerate (self ._expression ):
311317 if isinstance (v , Element ):
312318 self ._expression [i ] = v .value ()
313- if last_operation and v in self .MATH_ACTIONS :
319+ if isinstance (v , str ):
320+ if v not in ("<=" , ">=" , "==" , '!=' , "**" , "//" ):
321+ if len (v ) > 1 :
322+ if len (v ) % 2 == 0 :
323+ self ._expression [i ] = "+"
324+ else :
325+ self ._expression [i ] = "-"
326+
327+ if last_operation and v in ("+" , "-" ,):
328+ if last_operation == "+" and v == "-" :
329+ self ._expression [i ] = "-"
330+ del self ._expression [i - 1 ]
331+ elif last_operation == "-" and v == "+" :
332+ self ._expression [i ] = "-"
333+ del self ._expression [i ]
334+ elif last_operation and v in self .MATH_ACTIONS :
314335 raise DoubleOperationException ("'{so}' operation follows '{fo}'" .format (
315336 so = last_operation ,
316337 fo = v
317338 ))
339+
318340 if v in self .MATH_ACTIONS :
319341 last_operation = v
320342 else :
@@ -326,6 +348,12 @@ def value(self):
326348
327349 # Evaluate multi-value expression
328350 if self ._multivalue :
329- return self ._func (* self ._expression )
330-
331- return self ._calculate_mathematical_expression ()
351+ try :
352+ return self ._func (* self ._expression )
353+ except TypeError :
354+ raise ExpressionFormatException ("Expected 2 arguments, got 3: '{}'" .format (self ._func ))
355+
356+ try :
357+ return self ._calculate_mathematical_expression ()
358+ except TypeError :
359+ raise UnsupportedMathematicalOperationException ("We do not support '{}' operation" .format (i ))
0 commit comments