Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Put src in src directory
Browse files Browse the repository at this point in the history
  • Loading branch information
GabDug committed Jan 8, 2018
1 parent 46891f3 commit 7e54489
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ language: python
python:
- "3.6"
script:
- python test.py
- python src/test.py
2 changes: 1 addition & 1 deletion REPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Introduction
------------

- We tried to respect Python writting (conventions, style, philosophy...). Ex: _ for private


Architecture of the calculator
Expand Down
6 changes: 0 additions & 6 deletions GUI/basic_tk.py → src/GUI/basic_tk.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions GUI/tk_calculator.py → src/GUI/tk_calculator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# 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
from tkinter import messagebox
from tkinter.scrolledtext import ScrolledText

from eval_global import ext_eval_global

import logger_conf

logger = logger_conf.Log.logger
Expand Down
124 changes: 58 additions & 66 deletions eval_global.py → src/eval_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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:
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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":
Expand All @@ -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"))
Expand Down Expand Up @@ -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])}")
File renamed without changes.
3 changes: 2 additions & 1 deletion main.py → src/main.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions parse.py → src/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
3 changes: 1 addition & 2 deletions test.py → src/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

from eval_global import ext_eval_global
from parse import parse


class TestEval(unittest.TestCase):
Expand Down Expand Up @@ -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"),

]
Expand Down

0 comments on commit 7e54489

Please sign in to comment.