From 7e54489b248092b5d277d3fdb382cbd0e59e7fc7 Mon Sep 17 00:00:00 2001 From: Gabriel Dugny Date: Mon, 8 Jan 2018 19:17:44 +0100 Subject: [PATCH] Put src in src directory --- .travis.yml | 2 +- REPORT.md | 2 +- {GUI => src/GUI}/basic_tk.py | 6 -- {GUI => src/GUI}/tk_calculator.py | 3 +- eval_global.py => src/eval_global.py | 124 +++++++++++++-------------- logger_conf.py => src/logger_conf.py | 0 main.py => src/main.py | 3 +- parse.py => src/parse.py | 1 + test.py => src/test.py | 3 +- 9 files changed, 65 insertions(+), 79 deletions(-) rename {GUI => src/GUI}/basic_tk.py (85%) rename {GUI => src/GUI}/tk_calculator.py (99%) rename eval_global.py => src/eval_global.py (66%) rename logger_conf.py => src/logger_conf.py (100%) rename main.py => src/main.py (99%) rename parse.py => src/parse.py (99%) rename test.py => src/test.py (96%) diff --git a/.travis.yml b/.travis.yml index e1d0f2f..36e362b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,4 @@ language: python python: - "3.6" script: - - python test.py \ No newline at end of file + - python src/test.py \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index 3e75579..71caa6c 100644 --- a/REPORT.md +++ b/REPORT.md @@ -8,7 +8,7 @@ Introduction ------------ - +- We tried to respect Python writting (conventions, style, philosophy...). Ex: _ for private Architecture of the calculator diff --git a/GUI/basic_tk.py b/src/GUI/basic_tk.py similarity index 85% rename from GUI/basic_tk.py rename to src/GUI/basic_tk.py index 3615b6b..be331cc 100644 --- a/GUI/basic_tk.py +++ b/src/GUI/basic_tk.py @@ -1,13 +1,7 @@ -import os -import sys import tkinter as tk import tkinter.ttk as ttk -from threading import Lock -from tkinter import TOP, BOTH, END, INSERT, SEL, SEL_FIRST, SEL_LAST -from tkinter import messagebox from tkinter.scrolledtext import ScrolledText -from eval_global import ext_eval_global import logger_conf logger = logger_conf.Log.logger diff --git a/GUI/tk_calculator.py b/src/GUI/tk_calculator.py similarity index 99% rename from GUI/tk_calculator.py rename to src/GUI/tk_calculator.py index ced3673..02d9186 100644 --- a/GUI/tk_calculator.py +++ b/src/GUI/tk_calculator.py @@ -1,8 +1,6 @@ # PYTKCON by Samy (samyzaf) [https://github.com/samyzaf/pytkcon] # The code is free to use, used to be a Python console but has been simplified -import os -import sys import tkinter as tk from threading import Lock from tkinter import TOP, BOTH, END, INSERT, SEL, SEL_FIRST, SEL_LAST @@ -10,6 +8,7 @@ from tkinter.scrolledtext import ScrolledText from eval_global import ext_eval_global + import logger_conf logger = logger_conf.Log.logger diff --git a/eval_global.py b/src/eval_global.py similarity index 66% rename from eval_global.py rename to src/eval_global.py index 57714de..b9025e1 100644 --- a/eval_global.py +++ b/src/eval_global.py @@ -7,7 +7,7 @@ logger = logging.getLogger(__name__) -def single_element(element: tuple): +def _single_element(element: tuple): """Evaluate a single token. Used when there is only one token.""" if element[1] == "variable": if element[0] == "exit": @@ -21,7 +21,7 @@ def single_element(element: tuple): return element[0] -def first_eval(expression: list): +def _first_eval(expression: list): """Evaluates an expression (boolean, integer or string), where the input is a parsed list of tokens.""" size = len(expression) if size == 0: @@ -31,12 +31,12 @@ def first_eval(expression: list): # exit() raise Exception('Error: Expression is empty.') elif size == 1: - return single_element(expression[0]) + return _single_element(expression[0]) else: - return eval_global(expression) + return _eval_global(expression) -def eval_global(expression: list): +def _eval_global(expression: list): """Evaluates an expression, where the input is a parsed list of tokens.""" expression = remove_parenthesis(expression) logger.debug("Input at eval global: " + str(expression)) @@ -45,19 +45,10 @@ def eval_global(expression: list): if length == 0: raise Exception(" Error: expression is empty.") - # expression is: single-operand + # If expression is single-operand if length == 1: - # should be an operand - # Do not simplify - if expression[0] == True: - logger.debug(" Return: true") - return "true" - if not expression[0]: - logger.debug(" Return: false") - return "false" - logger.debug(" Return:" + str(expression[0][0])) - return expression[0][0] + return _single_element(expression[0]) # Expression Shape: left-expression main-operator right-expression operator_index = find_operator(expression) @@ -78,7 +69,7 @@ def eval_global(expression: list): if left_expression is None or left_expression == []: logger.debug(" Left Expression Is None") - if eval_global(right_expression) == "true": + if _eval_global(right_expression) == "true": logger.debug(" Return: false") return "false" else: @@ -87,84 +78,76 @@ def eval_global(expression: list): else: logger.debug(" Left Exp : " + str(left_expression)) - if eval_global(right_expression) == "true": + if _eval_global(right_expression) == "true": left_expression.append(("false", "boolean")) else: left_expression.append(("true", "boolean")) logger.debug(" Return eval: " + str(left_expression)) - return eval_global(left_expression) + return _eval_global(left_expression) # Binary infix operators else: if main_operator[0] == '+': - return eval_global(left_expression) + eval_global(right_expression) + return _eval_global(left_expression) + _eval_global(right_expression) elif main_operator[0] == '-': - return eval_global(left_expression) - eval_global(right_expression) + return _eval_global(left_expression) - _eval_global(right_expression) elif main_operator[0] == '*': - return eval_global(left_expression) * eval_global(right_expression) + return _eval_global(left_expression) * _eval_global(right_expression) elif main_operator[0] == "/": - return eval_global(left_expression) // eval_global(right_expression) + return _eval_global(left_expression) // _eval_global(right_expression) elif main_operator[0] == '==': - if eval_global(left_expression) == eval_global(right_expression): + if _eval_global(left_expression) == _eval_global(right_expression): return "true" else: return "false" - # return eval_global(left_expression) == eval_global(right_expression) + # return _eval_global(left_expression) == _eval_global(right_expression) elif main_operator[0] == '!=': - if eval_global(left_expression) != eval_global(right_expression): + if _eval_global(left_expression) != _eval_global(right_expression): return "true" else: return "false" elif main_operator[0] == '<': - if eval_global(left_expression) < eval_global(right_expression): + if _eval_global(left_expression) < _eval_global(right_expression): return "true" else: return "false" elif main_operator[0] == '>': - if eval_global(left_expression) > eval_global(right_expression): + if _eval_global(left_expression) > _eval_global(right_expression): return "true" else: return "false" elif main_operator[0] == '<=': - if eval_global(left_expression) <= eval_global(right_expression): + if _eval_global(left_expression) <= _eval_global(right_expression): return "true" else: return "false" elif main_operator[0] == '>=': - if eval_global(left_expression) >= eval_global(right_expression): + if _eval_global(left_expression) >= _eval_global(right_expression): return "true" else: return "false" elif main_operator[0] == 'and': - if eval_global(left_expression) == eval_global(right_expression) == 'true': + if _eval_global(left_expression) == _eval_global(right_expression) == 'true': return "true" else: return "false" elif main_operator[0] == 'or': - if eval_global(left_expression) == "true" or eval_global(right_expression) == 'true': + if _eval_global(left_expression) == "true" or _eval_global(right_expression) == 'true': return "true" else: return "false" - # def eval_variable(expression: list): - # """Evaluates the variables.""" - # TODO pas fini - # size = len(expression) - # - # i = 0 - # while i < size: - # if expression[i][0] == "variable": - # eval_exp = first_eval() def ext_eval_global(expression_str: str): """Evaluates an expression (boolean, integer or string), where the input is a string.""" - return first_eval(parse(expression_str)) + return _first_eval(parse(expression_str)) def find_operator(expression): - """Find the operator where to split an integer expression in two operands.""" + """Find the operator where to split an integer expression in two operands. We check from the end of the expression + to the beggining (left associativity, from the lowest priority operator to the highest, ignoring operators in + factors (between parenthesis).""" parenthesis = 0 - # Check for + or - for j in range(6, -1, -1): for i in range(len(expression) - 1, -1, -1): if expression[i][1] == "parenthesis": @@ -178,26 +161,35 @@ def find_operator(expression): if __name__ == "__main__": - import logger_conf - - logger = logger_conf.Log.logger - - print(ext_eval_global("'Hi' < 'Hello'")) - # print(ext_eval_global("false and false")) - # print(ext_eval_global("false and false")) - # print(ext_eval_global("true and false")) - # print(ext_eval_global("true and true")) - # print(ext_eval_global("false and true")) - # print(ext_eval_global("false or false")) - # print(ext_eval_global("true or false")) - # print(ext_eval_global("true or true")) - # print(ext_eval_global("false or true")) - # print(ext_eval_global("not true")) - # print(ext_eval_global("not false")) - # print(ext_eval_global("not not true")) - # print(ext_eval_global("not not false")) - # print(ext_eval_global("not not not true")) - # print(ext_eval_global("not not not false")) + import logging + from sys import stdout + + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) + ch = logging.StreamHandler(stdout) + ch.setLevel(logging.DEBUG) + formatter = logging.Formatter("%(asctime)s [%(levelname)s] : %(message)s") + formatter.datefmt = "%H:%M:%S" + ch.setFormatter(formatter) + logger.addHandler(ch) + logger.info("Starting logger from module.") + + # print(ext_eval_global("'Hi' < 'Hello'")) + print(ext_eval_global("false and false")) + print(ext_eval_global("false and false")) + print(ext_eval_global("true and false")) + print(ext_eval_global("true and true")) + print(ext_eval_global("false and true")) + print(ext_eval_global("false or false")) + print(ext_eval_global("true or false")) + print(ext_eval_global("true or true")) + print(ext_eval_global("false or true")) + print(ext_eval_global("not true")) + print(ext_eval_global("not false")) + print(ext_eval_global("not not true")) + print(ext_eval_global("not not false")) + print(ext_eval_global("not not not true")) + print(ext_eval_global("not not not false")) # # print(ext_eval_global("-35 == (4+6)-5*9")) # print(ext_eval_global("1==2")) @@ -234,9 +226,9 @@ def find_operator(expression): # ("((1+(2*3))*(4*5))", "140"), # ("(4+6)-5*9", "-35")] # for e in range(len(test)): - # if str(eval_global(parse(test[e][0]))) == str(test[e][1]): + # if str(_eval_global(parse(test[e][0]))) == str(test[e][1]): # logger.info(f"Succes {e}: {test[e][0]} = {test[e][1]}") # else: # logger.error(f"Failure {e}: {test[e][0]} != {test[e][1]}") - # logger.info(f" => Result: {eval_global(parse(test[e][0]))}") + # logger.info(f" => Result: {_eval_global(parse(test[e][0]))}") # logger.info(f" => eval : {eval(test[e][0])}") diff --git a/logger_conf.py b/src/logger_conf.py similarity index 100% rename from logger_conf.py rename to src/logger_conf.py diff --git a/main.py b/src/main.py similarity index 99% rename from main.py rename to src/main.py index e8c15b8..52a48fd 100644 --- a/main.py +++ b/src/main.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- -from eval_global import ext_eval_global import traceback + import logger_conf +from eval_global import ext_eval_global logger = logger_conf.Log.logger diff --git a/parse.py b/src/parse.py similarity index 99% rename from parse.py rename to src/parse.py index 577b1a6..ff7d168 100644 --- a/parse.py +++ b/src/parse.py @@ -149,6 +149,7 @@ def remove_parenthesis(expression): # logger.info(parse("'Salut'")) # logger.info(parse("'Salut'+'toi'")) # logger.info(parse("'Salut+'toi'")) + logger.info((parse("3+(4*(4-8))"))) logger.info(parse("3+4*2")) logger.info(parse("248+345")) logger.info(parse("(3 + 2) * 4")) diff --git a/test.py b/src/test.py similarity index 96% rename from test.py rename to src/test.py index fa05af2..07f0997 100644 --- a/test.py +++ b/src/test.py @@ -1,7 +1,6 @@ import unittest from eval_global import ext_eval_global -from parse import parse class TestEval(unittest.TestCase): @@ -47,7 +46,7 @@ def test_bool(self): ("true", "true"), ("true or false and false", "true"), ("(true or false) and false", "false"), - ("not false and not false","true"), + ("not false and not false", "true"), ("1<0", "false"), ]