Skip to content

Commit c5a9ce7

Browse files
committed
[fix and add logic to pycalc] unary operation and ERRORs
1 parent 1e35d2b commit c5a9ce7

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

libs/element.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

pycalc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from libs.element import Element, BaseExpressionException
66
if __name__ == '__main__':
77
parser = argparse.ArgumentParser(description="")
88
parser.add_argument("-m MODULE ", "--use-modules", metavar="MODULE...", nargs="?", help="additional modules to use")
9-
parser.add_argument("EXPRESSION", help="expression string to evaluate")
9+
parser.add_argument("EXPRESSION", help="expression string to evaluate", type=str)
1010

1111
args = parser.parse_args()
1212

@@ -15,3 +15,10 @@ if __name__ == '__main__':
1515
print(expression.value())
1616
except BaseExpressionException as exc:
1717
print("ERROR: {}".format(str(exc)))
18+
# except ValueError as exc:
19+
# print("ERROR: {}".format(str(exc)))
20+
# except TypeError as exc:
21+
# print("ERROR: {}".format(str(exc)))
22+
# expression = Element(expression=args.EXPRESSION)
23+
# print(expression.value())
24+
# print(expression.booleen_value())

pycalc_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from termcolor import colored
66

77

8-
PYCALC_UTIL_NAME = "pycalc"
8+
PYCALC_UTIL_NAME = "./pycalc"
99
RETURN_CODE = 0
1010

1111

0 commit comments

Comments
 (0)