From 9727e3381afba59b7c7965e312041cdb1fc65846 Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Fri, 2 Aug 2019 15:43:26 +0200 Subject: [PATCH 1/9] \n --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 4c44759..d4b1b2e 100644 --- a/main.py +++ b/main.py @@ -34,3 +34,4 @@ try: except Exception as e: print('ERROR' + str(e)) + -- 2.39.5 From 92d253680e3dbe18b565f124170eb42ed7ec2a10 Mon Sep 17 00:00:00 2001 From: tstst334 Date: Fri, 2 Aug 2019 15:47:17 +0200 Subject: [PATCH 2/9] password --- user_IO.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_IO.py b/user_IO.py index 42a61d5..2f31563 100644 --- a/user_IO.py +++ b/user_IO.py @@ -42,3 +42,7 @@ def user_output(subject, categories, average, average_subject): string += str(grade) + ", " print(string + "\n") print("The average of {} is {}.".format(subject, average_subject)) + +def password(): + print("What is your password?") + return input() \ No newline at end of file -- 2.39.5 From 93c9c9d33cfe12903eb584644bc5088a2dfa4aed Mon Sep 17 00:00:00 2001 From: tstst334 Date: Fri, 2 Aug 2019 15:48:06 +0200 Subject: [PATCH 3/9] password --- user_IO.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user_IO.py b/user_IO.py index 2f31563..7ba55ba 100644 --- a/user_IO.py +++ b/user_IO.py @@ -43,6 +43,7 @@ def user_output(subject, categories, average, average_subject): print(string + "\n") print("The average of {} is {}.".format(subject, average_subject)) + def password(): print("What is your password?") - return input() \ No newline at end of file + return input() -- 2.39.5 From 16cb42203d75c98091f9c5ed2d3d005064c375b5 Mon Sep 17 00:00:00 2001 From: tstst334 Date: Fri, 2 Aug 2019 15:52:16 +0200 Subject: [PATCH 4/9] break --- main.py | 2 ++ user_IO.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index d4b1b2e..3514075 100644 --- a/main.py +++ b/main.py @@ -30,6 +30,8 @@ try: _subject = user_input[1] subject_get = subject.get_subject(subjects, _subject) user_IO.user_output(subject_get.return_average_subject) + elif input_or_output == "exit": + break raise SystemError except Exception as e: diff --git a/user_IO.py b/user_IO.py index 7ba55ba..bc1ea2e 100644 --- a/user_IO.py +++ b/user_IO.py @@ -1,5 +1,5 @@ def user_input(): - todo = input("Ok, do you want to see them or to edit them [see/edit]?\n") + todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n") if todo == "see": print("Which subject do you want to know the information?") subject = input() @@ -31,6 +31,8 @@ def user_input(): grade = input("Grade? \n") grade_name = input("What's the name of the grade? \n") return "input", "grade", subject, category, grade, grade_name + elif todo == "exit": + return "exit" def user_output(subject, categories, average, average_subject): -- 2.39.5 From 97541f8cd344bf134425bbf7be7407f463d80797 Mon Sep 17 00:00:00 2001 From: tstst334 Date: Fri, 2 Aug 2019 15:53:09 +0200 Subject: [PATCH 5/9] break --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index 3514075..d569468 100644 --- a/main.py +++ b/main.py @@ -36,4 +36,3 @@ try: except Exception as e: print('ERROR' + str(e)) - -- 2.39.5 From 4fd2e1e27864f7e5bab0d78e8ddd5fc3cb29797f Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Fri, 2 Aug 2019 15:54:59 +0200 Subject: [PATCH 6/9] open file --- main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/main.py b/main.py index d4b1b2e..de36a74 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,20 @@ import subject import user_IO +import serializer +import encryption +import os subjects = [] +filepath = "/data/user.grades" try: + password = user_IO.password() + if os.path.isfile(filepath): + with open(filepath, 'r') as file: + string = encryption.decrypt(password, file.read()) + serializer.deserialize(string) + + + while True: user_input = user_IO.user_input() input_or_output = user_input[0] -- 2.39.5 From a3fce9f7e4b0e22361385c03856cf10e3cdde5cc Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Fri, 2 Aug 2019 16:06:53 +0200 Subject: [PATCH 7/9] file saving --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 1cd7fbe..4c2dee1 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,10 @@ try: user_IO.user_output(subject_get.return_average_subject) elif input_or_output == "exit": break - raise SystemError + with open(filepath, 'w') as file: + _list = [serializer.serialize(_subject) for _subject in subjects] + string = encryption.encrypt(password, str(_list)) + file.write(string) except Exception as e: print('ERROR' + str(e)) -- 2.39.5 From 0cb5e2f73ff748911bddc6b926fc677d41b785fe Mon Sep 17 00:00:00 2001 From: tstst334 Date: Fri, 2 Aug 2019 17:33:24 +0200 Subject: [PATCH 8/9] break --- serializer.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/serializer.py b/serializer.py index b0b496b..d6d349d 100644 --- a/serializer.py +++ b/serializer.py @@ -1,6 +1,24 @@ +import json +import subject + + +class TempSubject: + def __init__(self, string): + self.__dict__ = json.loads(string) + + def serialize(subject): string = subject.__dict__ for category in string["grades"]: for counter, grade in enumerate(string["grades"][category][0]): string["grades"][category][0][counter] = grade.__dict__ return str(string) + + +def deserialize(string): + temp = TempSubject(string) + _subject = subject.Subject(temp.name, bool(temp.main_subject), bool(temp.oral_exam)) + for category in temp.grades: + _subject.add_grade_category(category, temp.grades[category][1]) + return _subject + -- 2.39.5 From caf650eaefe3f6d063641b80048909576b7beed0 Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Fri, 2 Aug 2019 18:54:27 +0200 Subject: [PATCH 9/9] exceptions added for user input --- exceptions.py | 22 ++++++++++++++++ main.py | 69 +++++++++++++++++++++++++++++---------------------- subject.py | 8 +++++- user_IO.py | 25 +++++++++++++++++-- 4 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 exceptions.py diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000..75e57e5 --- /dev/null +++ b/exceptions.py @@ -0,0 +1,22 @@ +class NotANumberException(Exception): + pass + + +class SubjectNotExistingException(Exception): + pass + + +class CategoryNotExistingException(Exception): + pass + + +class WrongCaseException(Exception): + pass + + +def convertible(string): + try: + float(string) + return True + except ValueError: + raise NotANumberException \ No newline at end of file diff --git a/main.py b/main.py index 4c2dee1..9ff0195 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import subject import user_IO import serializer import encryption +import exceptions import os subjects = [] @@ -13,37 +14,45 @@ try: string = encryption.decrypt(password, file.read()) serializer.deserialize(string) - - while True: - user_input = user_IO.user_input() - input_or_output = user_input[0] - if input_or_output == "input": - _edit = user_input[1] - _subject = user_input[2] - if _edit == "grade": - _category = user_input[3] - _grade = user_input[4] - _grade_name = user_input[5] - subject_edit = subject.get_subject(subjects, _subject) - if subject_edit: - subject_edit.add_grade(_category, subject.Grade(_grade, _grade_name)) - elif _edit == "category": - _category = user_input[3] - _rating = user_input[4] - subject_edit = subject.get_subject(subjects, _subject) - if subject_edit: - subject_edit.add_grade_category(_category, _rating) - elif _edit == "subject": - _main = user_input[3] - _oral = user_input[4] - subjects.append(subject.Subject(_subject, _main, _oral)) - elif input_or_output == "output": - _subject = user_input[1] - subject_get = subject.get_subject(subjects, _subject) - user_IO.user_output(subject_get.return_average_subject) - elif input_or_output == "exit": - break + try: + user_input = user_IO.user_input() + input_or_output = user_input[0] + if input_or_output == "input": + _edit = user_input[1] + _subject = user_input[2] + if _edit == "grade": + _category = user_input[3] + _grade = user_input[4] + _grade_name = user_input[5] + subject_edit = subject.get_subject(subjects, _subject) + if subject_edit: + subject_edit.add_grade(_category, subject.Grade(_grade, _grade_name)) + elif _edit == "category": + _category = user_input[3] + _rating = user_input[4] + subject_edit = subject.get_subject(subjects, _subject) + if subject_edit: + subject_edit.add_grade_category(_category, _rating) + elif _edit == "subject": + _main = user_input[3] + _oral = user_input[4] + subjects.append(subject.Subject(_subject, _main, _oral)) + else: + raise exceptions.WrongCaseException + elif input_or_output == "output": + _subject = user_input[1] + subject_get = subject.get_subject(subjects, _subject) + user_IO.user_output(subject_get.return_average_subject) + elif input_or_output == "exit": + break + else: + raise exceptions.WrongCaseException + + except (exceptions.NotANumberException, exceptions.CategoryNotExistingException, + exceptions.SubjectNotExistingException, exceptions.WrongCaseException) as e: + user_IO.exception_raised(e) + with open(filepath, 'w') as file: _list = [serializer.serialize(_subject) for _subject in subjects] string = encryption.encrypt(password, str(_list)) diff --git a/subject.py b/subject.py index f2454a5..98eafea 100644 --- a/subject.py +++ b/subject.py @@ -1,4 +1,5 @@ from datetime import datetime as str_date +from exceptions import SubjectNotExistingException, CategoryNotExistingException class Subject: @@ -31,6 +32,11 @@ class Subject: grade_sum += self.return_average_of_category(i) return grade_sum / rating_sum + def category_existing(self, category): + if category in self.grades: + return True + raise CategoryNotExistingException + class Grade: def __init__(self, grade, name, date=str(str_date.now())): @@ -43,4 +49,4 @@ def get_subject(liste, name): for subject in liste: if subject.name == name: return subject - return None + raise SubjectNotExistingException diff --git a/user_IO.py b/user_IO.py index bc1ea2e..5650a1b 100644 --- a/user_IO.py +++ b/user_IO.py @@ -1,3 +1,6 @@ +import exceptions + + def user_input(): todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n") if todo == "see": @@ -14,23 +17,29 @@ def user_input(): main_subject = True elif main_subject == 'n': main_subject = False + else: + raise exceptions.WrongCaseException oral_exam = input("Will there be an oral exam in this subject [y/n]? \n") if oral_exam == 'y': oral_exam = True elif oral_exam == 'n': oral_exam = False + else: + raise exceptions.WrongCaseException return "input", "subject", subject, main_subject, oral_exam elif user_add == "category": subject = input("To which subject do you want to add the category? \n") category = input("Which category do you want to add? \n") rating = float(input("Rating? \n")) - return "input", "category", subject, category, rating + if exceptions.convertible(rating): + return "input", "category", subject, category, rating elif user_add == "grade": subject = input("To which subject do you want to add the grade? \n") category = input("To which category do you want to add it? \n") grade = input("Grade? \n") grade_name = input("What's the name of the grade? \n") - return "input", "grade", subject, category, grade, grade_name + if exceptions.convertible(grade): + return "input", "grade", subject, category, grade, grade_name elif todo == "exit": return "exit" @@ -49,3 +58,15 @@ def user_output(subject, categories, average, average_subject): def password(): print("What is your password?") return input() + + +def exception_raised(exception): + print("Dear user you've raised an exception. The cause of the exception is:") + if exception is exceptions.SubjectNotExistingException: + print("The subject you tried to access is not created") + elif exceptions is exceptions.CategoryNotExistingException: + print("The category you tried to add something is non-existent in this subject") + elif exception is exceptions.NotANumberException: + print("The number you gave the program is not a number") + elif exception is exceptions.WrongCaseException: + print("This was not one of the available choices") \ No newline at end of file -- 2.39.5