From 9574c10600513ffcccaa0d39c81e22e656c7ddb1 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 2 Jan 2025 22:07:23 +0100 Subject: [PATCH] added main.py --- abacus_core.py | 84 ++++++++++++++++++++++++++++++-------------------- main.py | 7 +++++ 2 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 main.py diff --git a/abacus_core.py b/abacus_core.py index 2ca9f95..39b3b5c 100644 --- a/abacus_core.py +++ b/abacus_core.py @@ -15,68 +15,84 @@ class AbacusCore: self._funcs['r2'] = lambda x: round(x, 2) self._funcs['r0'] = lambda x: int(x) - def _calculate(self, input): - return eval(input) - def parse_input(self, input): + def _calculate(self, input_str): + try: + return True, eval(input_str) + except ZeroDivisionError: + res = 'Division by Zero' + except NameError as e: + res = 'Variable '+ e.name + ' not exists' + return False, res + + def get_vars(self): + return self._vars + + def add_var(self, name, value): + self._vars[name] = value + + def parse_input(self, input_str): comment = None input_wo_com = None - input = str(input).strip() - input = input.replace(',', '.') - if input[0] == '#': - comment = input[1:] - com_index = input.find('#') + success = False + input_str = str(input_str).strip() + input_str = input_str.replace(',', '.') + if input_str[0] == '#': + comment = input_str[1:] + com_index = input_str.find('#') if com_index > -1: - input_wo_com = input[:com_index] - comment = input[com_index+1:] + input_wo_com = input_str[:com_index] + comment = input_str[com_index + 1:] if input_wo_com is None: - input_wo_com = input + input_wo_com = input_str print('comment', comment) result = self._split_input(input_wo_com) print('result:',result) if result[1] is not None: - res = self._calculate(result[1]) - print(res) - operators = result[0] - operators.reverse() - for op in operators: - if op[-1] == '=': - self._vars[op[:-1]] = res - elif op[-1] == ':': - res = self._funcs.get(op[:-1])(res) - print('vars:', self._vars) + success, res = self._calculate(result[1]) + if success: + operators = result[0] + operators.reverse() + for op in operators: + if op[-1] == '=': + self._vars[op[:-1]] = res + elif op[-1] == ':': + res = self._funcs.get(op[:-1])(res) + print('vars:', self._vars) + else: + print("Error") - def _split_input(self, input): + def _split_input(self, input_str): last_position = 0 varchunks = list() - for i, c in enumerate(input): + for i, c in enumerate(input_str): if c in [':', '=']: - chunk = input[last_position:i].strip() - chunk = chunk+input[i] + chunk = input_str[last_position:i].strip() + chunk = chunk + input_str[i] varchunks.append(chunk) last_position = i+1 - rest = input[last_position:].strip() + rest = input_str[last_position:].strip() print('rest', rest) rest = self._find_vars(rest)[1] - return (varchunks, rest) + return varchunks, rest - def _find_vars(self, input): + def _find_vars(self, input_str): chunks = list() last_position = 0 - for i, c in enumerate(input): + for i, c in enumerate(input_str): if c in ['+', '-', '/', '*', '(', ')', '[', ']', ';']: - ch = input[last_position:i].strip() + ch = input_str[last_position:i].strip() if len(ch)>0: chunks.append(ch) - chunks.append(input[i].strip()) + chunks.append(input_str[i].strip()) last_position = i+1 - if last_position1, ''.join(cp_chunks)) \ No newline at end of file + return len(cp_chunks)>1, ''.join(cp_chunks) diff --git a/main.py b/main.py new file mode 100644 index 0000000..0d97bda --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from abacus_core import AbacusCore + +ab = AbacusCore() + + +if __name__ == '__main__': + ab.parse_input('12')