Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b7a23fd8f |
@@ -1,22 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -2,54 +2,52 @@ import subject
|
|||||||
import user_IO
|
import user_IO
|
||||||
import serializer
|
import serializer
|
||||||
import encryption
|
import encryption
|
||||||
import exceptions
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
subjects = []
|
subjects = []
|
||||||
|
filepath = "/data/user.grades"
|
||||||
try:
|
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:
|
while True:
|
||||||
try:
|
user_input = user_IO.user_input()
|
||||||
user_input = user_IO.user_input()
|
input_or_output = user_input[0]
|
||||||
input_or_output = user_input[0]
|
if input_or_output == "input":
|
||||||
if input_or_output == "input":
|
_edit = user_input[1]
|
||||||
_edit = user_input[1]
|
_subject = user_input[2]
|
||||||
_subject = user_input[2]
|
if _edit == "grade":
|
||||||
if _edit == "grade":
|
_category = user_input[3]
|
||||||
_category = user_input[3]
|
_grade = user_input[4]
|
||||||
_grade = user_input[4]
|
_grade_name = user_input[5]
|
||||||
_grade_name = user_input[5]
|
subject_edit = subject.get_subject(subjects, _subject)
|
||||||
subject_edit = subject.get_subject(subjects, _subject)
|
if subject_edit:
|
||||||
if subject_edit:
|
subject_edit.add_grade(_category, subject.Grade(_grade, _grade_name))
|
||||||
subject_edit.add_grade(_category, subject.Grade(_grade, _grade_name))
|
elif _edit == "category":
|
||||||
elif _edit == "category":
|
_category = user_input[3]
|
||||||
_category = user_input[3]
|
_rating = user_input[4]
|
||||||
_rating = user_input[4]
|
subject_edit = subject.get_subject(subjects, _subject)
|
||||||
subject_edit = subject.get_subject(subjects, _subject)
|
if subject_edit:
|
||||||
if subject_edit:
|
subject_edit.add_grade_category(_category, _rating)
|
||||||
subject_edit.add_grade_category(_category, _rating)
|
elif _edit == "subject":
|
||||||
elif _edit == "subject":
|
_main = user_input[3]
|
||||||
_main = user_input[3]
|
_oral = user_input[4]
|
||||||
_oral = user_input[4]
|
subjects.append(subject.Subject(_subject, _main, _oral))
|
||||||
subjects.append(subject.Subject(_subject, _main, _oral))
|
elif input_or_output == "output":
|
||||||
else:
|
_subject = user_input[1]
|
||||||
raise exceptions.WrongCaseException
|
subject_get = subject.get_subject(subjects, _subject)
|
||||||
elif input_or_output == "output":
|
user_IO.user_output(subject_get.return_average_subject)
|
||||||
_subject = user_input[1]
|
elif input_or_output == "exit":
|
||||||
subject_get = subject.get_subject(subjects, _subject)
|
break
|
||||||
user_IO.user_output(subject_get.name, subject_get.return_grade_categories(),
|
with open(filepath, 'w') as file:
|
||||||
[subject_get.return_average_of_category(i) for i in subject_get.return_grade_categories()],
|
_list = [serializer.serialize(_subject) for _subject in subjects]
|
||||||
subject_get.return_average_of_subject())
|
string = encryption.encrypt(password, str(_list))
|
||||||
elif input_or_output == "exit":
|
file.write(string)
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise exceptions.WrongCaseException
|
|
||||||
|
|
||||||
except (exceptions.NotANumberException, exceptions.CategoryNotExistingException,
|
|
||||||
exceptions.SubjectNotExistingException, exceptions.WrongCaseException) as e:
|
|
||||||
user_IO.exception_raised(e)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('ERROR' + str(e))
|
print('ERROR' + str(e))
|
||||||
raise
|
|
||||||
|
|||||||
+34
-9
@@ -2,23 +2,48 @@ import json
|
|||||||
import subject
|
import subject
|
||||||
|
|
||||||
|
|
||||||
class TempSubject:
|
class TempSubjectSerialize:
|
||||||
|
def __init__(self, _subject):
|
||||||
|
self.name = _subject.name
|
||||||
|
self.main_subject = str(_subject.main_subject)
|
||||||
|
self.oral_exam = str(_subject.oral_exam)
|
||||||
|
self.grades = {}
|
||||||
|
for category in _subject.grades:
|
||||||
|
self.grades[category] = {}
|
||||||
|
for counter, grade in enumerate(_subject.grades[category][0]):
|
||||||
|
self.grades[category][str(counter)] = {
|
||||||
|
"grade": str(grade.grade),
|
||||||
|
"name": str(grade.name),
|
||||||
|
"date": str(grade.date)
|
||||||
|
}
|
||||||
|
self.grades[category]["rating"] = _subject.grades[category][1]
|
||||||
|
|
||||||
|
def get_string(self):
|
||||||
|
string = self.__dict__
|
||||||
|
string = str(string)
|
||||||
|
string = string.replace("'", '"')
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
|
class TempSubjectDeserialize:
|
||||||
def __init__(self, string):
|
def __init__(self, string):
|
||||||
self.__dict__ = json.loads(string)
|
self.__dict__ = json.loads(string)
|
||||||
|
|
||||||
|
|
||||||
def serialize(subject):
|
def serialize(_subject):
|
||||||
string = subject.__dict__
|
ser = TempSubjectSerialize(_subject)
|
||||||
for category in string["grades"]:
|
string = ser.get_string()
|
||||||
for counter, grade in enumerate(string["grades"][category][0]):
|
|
||||||
string["grades"][category][0][counter] = grade.__dict__
|
|
||||||
return str(string)
|
return str(string)
|
||||||
|
|
||||||
|
|
||||||
def deserialize(string):
|
def deserialize(string):
|
||||||
temp = TempSubject(string)
|
temp = TempSubjectDeserialize(string)
|
||||||
_subject = subject.Subject(temp.name, bool(temp.main_subject), bool(temp.oral_exam))
|
_subject = subject.Subject(temp.name, bool(temp.main_subject), bool(temp.oral_exam))
|
||||||
for category in temp.grades:
|
for category in temp.grades:
|
||||||
_subject.add_grade_category(category, temp.grades[category][1])
|
_subject.add_grade_category(category, temp.grades[category]['rating'])
|
||||||
|
for grade in temp.grades[category]:
|
||||||
|
if grade != "rating":
|
||||||
|
_subject.add_grade(category, subject.Grade(float(temp.grades[category][grade]["grade"]), # the grade
|
||||||
|
temp.grades[category][grade]["name"], # the name of grade
|
||||||
|
temp.grades[category][grade]["date"])) # the date of grade
|
||||||
return _subject
|
return _subject
|
||||||
|
|
||||||
|
|||||||
+4
-11
@@ -1,5 +1,4 @@
|
|||||||
from datetime import datetime as str_date
|
from datetime import datetime as str_date
|
||||||
from exceptions import SubjectNotExistingException, CategoryNotExistingException
|
|
||||||
|
|
||||||
|
|
||||||
class Subject:
|
class Subject:
|
||||||
@@ -19,25 +18,19 @@ class Subject:
|
|||||||
return self.grades[category][0]
|
return self.grades[category][0]
|
||||||
|
|
||||||
def return_grade_categories(self): # Return all categories
|
def return_grade_categories(self): # Return all categories
|
||||||
return self.grades
|
return [i for i in self.grades]
|
||||||
|
|
||||||
def return_average_of_category(self, category): # Return average of one categorie
|
def return_average_of_category(self, category): # Return average of one categorie
|
||||||
_list = [int(i.grade) for i in self.grades[category][0]]
|
return sum(self.grades[category][0]) / len(self.grades[category][0])
|
||||||
return sum(_list) / len(self.grades[category][0])
|
|
||||||
|
|
||||||
def return_average_of_subject(self):
|
def return_average_of_subject(self):
|
||||||
rating_sum = 0
|
rating_sum = 0
|
||||||
grade_sum = 0
|
grade_sum = 0
|
||||||
for i in self.grades:
|
for i in self.grades:
|
||||||
rating_sum += self.grades[i][1]
|
rating_sum += self.grades[i][1]
|
||||||
grade_sum += self.return_average_of_category(i)*self.grades[i][1]
|
grade_sum += self.return_average_of_category(i)
|
||||||
return grade_sum / rating_sum
|
return grade_sum / rating_sum
|
||||||
|
|
||||||
def category_existing(self, category):
|
|
||||||
if category in self.grades:
|
|
||||||
return True
|
|
||||||
raise CategoryNotExistingException
|
|
||||||
|
|
||||||
|
|
||||||
class Grade:
|
class Grade:
|
||||||
def __init__(self, grade, name, date=str(str_date.now())):
|
def __init__(self, grade, name, date=str(str_date.now())):
|
||||||
@@ -50,4 +43,4 @@ def get_subject(liste, name):
|
|||||||
for subject in liste:
|
for subject in liste:
|
||||||
if subject.name == name:
|
if subject.name == name:
|
||||||
return subject
|
return subject
|
||||||
raise SubjectNotExistingException
|
return None
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
{"name": "Deutsch", "main_subject": "False", "oral_exam": "True", "grades": {"schriftlich": {"0": {"grade": "13", "name": "KA1", "date": "2019-08-04 08:25:17.797803"}, "1": {"grade": "9", "name": "KA2", "date": "2019-08-04 08:25:17.797803"}, "rating": 2}, "mündlich": {"0": {"grade": "12", "name": "mündl 1. HJ", "date": "2019-08-04 08:25:17.797803"}, "rating": 1}}}
|
||||||
+7
-23
@@ -1,6 +1,3 @@
|
|||||||
import exceptions
|
|
||||||
|
|
||||||
|
|
||||||
def user_input():
|
def user_input():
|
||||||
todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n")
|
todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n")
|
||||||
if todo == "see":
|
if todo == "see":
|
||||||
@@ -17,29 +14,23 @@ def user_input():
|
|||||||
main_subject = True
|
main_subject = True
|
||||||
elif main_subject == 'n':
|
elif main_subject == 'n':
|
||||||
main_subject = False
|
main_subject = False
|
||||||
else:
|
|
||||||
raise exceptions.WrongCaseException
|
|
||||||
oral_exam = input("Will there be an oral exam in this subject [y/n]? \n")
|
oral_exam = input("Will there be an oral exam in this subject [y/n]? \n")
|
||||||
if oral_exam == 'y':
|
if oral_exam == 'y':
|
||||||
oral_exam = True
|
oral_exam = True
|
||||||
elif oral_exam == 'n':
|
elif oral_exam == 'n':
|
||||||
oral_exam = False
|
oral_exam = False
|
||||||
else:
|
|
||||||
raise exceptions.WrongCaseException
|
|
||||||
return "input", "subject", subject, main_subject, oral_exam
|
return "input", "subject", subject, main_subject, oral_exam
|
||||||
elif user_add == "category":
|
elif user_add == "category":
|
||||||
subject = input("To which subject do you want to add the category? \n")
|
subject = input("To which subject do you want to add the category? \n")
|
||||||
category = input("Which category do you want to add? \n")
|
category = input("Which category do you want to add? \n")
|
||||||
rating = float(input("Rating? \n"))
|
rating = float(input("Rating? \n"))
|
||||||
if exceptions.convertible(rating):
|
return "input", "category", subject, category, rating
|
||||||
return "input", "category", subject, category, rating
|
|
||||||
elif user_add == "grade":
|
elif user_add == "grade":
|
||||||
subject = input("To which subject do you want to add the grade? \n")
|
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")
|
category = input("To which category do you want to add it? \n")
|
||||||
grade = input("Grade? \n")
|
grade = input("Grade? \n")
|
||||||
grade_name = input("What's the name of the grade? \n")
|
grade_name = input("What's the name of the grade? \n")
|
||||||
if exceptions.convertible(grade):
|
return "input", "grade", subject, category, grade, grade_name
|
||||||
return "input", "grade", subject, category, grade, grade_name
|
|
||||||
elif todo == "exit":
|
elif todo == "exit":
|
||||||
return "exit"
|
return "exit"
|
||||||
|
|
||||||
@@ -49,19 +40,12 @@ def user_output(subject, categories, average, average_subject):
|
|||||||
for counter, category in enumerate(categories):
|
for counter, category in enumerate(categories):
|
||||||
print("The average in {} is {} with these grades:".format(category, average[counter]))
|
print("The average in {} is {} with these grades:".format(category, average[counter]))
|
||||||
string = ""
|
string = ""
|
||||||
for grade in categories[category][0]:
|
for grade in categories[category]:
|
||||||
string += str(grade.grade) + ", "
|
string += str(grade) + ", "
|
||||||
print(string + "\n")
|
print(string + "\n")
|
||||||
print("The average of {} is {}.".format(subject, average_subject))
|
print("The average of {} is {}.".format(subject, average_subject))
|
||||||
|
|
||||||
|
|
||||||
def exception_raised(exception):
|
def password():
|
||||||
print("Dear user you've raised an exception. The cause of the exception is:")
|
print("What is your password?")
|
||||||
if exception is exceptions.SubjectNotExistingException:
|
return input()
|
||||||
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")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user