10 Commits

Author SHA1 Message Date
lars 3b7a23fd8f serialization and deserialization done 2019-08-04 08:44:23 +02:00
tstst334 0cb5e2f73f break 2019-08-02 17:33:24 +02:00
lars a3fce9f7e4 file saving 2019-08-02 16:06:53 +02:00
lars 8b9823f48f Merge remote-tracking branch 'origin/saving_data' into saving_data 2019-08-02 15:55:08 +02:00
lars 4fd2e1e278 open file 2019-08-02 15:54:59 +02:00
tstst334 97541f8cd3 break 2019-08-02 15:53:09 +02:00
tstst334 16cb42203d break 2019-08-02 15:52:16 +02:00
tstst334 93c9c9d33c password 2019-08-02 15:48:06 +02:00
tstst334 92d253680e password 2019-08-02 15:47:17 +02:00
lars 9727e3381a \n 2019-08-02 15:43:26 +02:00
4 changed files with 75 additions and 7 deletions
+18 -1
View File
@@ -1,8 +1,20 @@
import subject import subject
import user_IO import user_IO
import serializer
import encryption
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:
user_input = user_IO.user_input() user_input = user_IO.user_input()
input_or_output = user_input[0] input_or_output = user_input[0]
@@ -30,7 +42,12 @@ try:
_subject = user_input[1] _subject = user_input[1]
subject_get = subject.get_subject(subjects, _subject) subject_get = subject.get_subject(subjects, _subject)
user_IO.user_output(subject_get.return_average_subject) user_IO.user_output(subject_get.return_average_subject)
raise SystemError elif input_or_output == "exit":
break
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: except Exception as e:
print('ERROR' + str(e)) print('ERROR' + str(e))
+48 -5
View File
@@ -1,6 +1,49 @@
def serialize(subject): import json
string = subject.__dict__ import subject
for category in string["grades"]:
for counter, grade in enumerate(string["grades"][category][0]):
string["grades"][category][0][counter] = grade.__dict__ 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):
self.__dict__ = json.loads(string)
def serialize(_subject):
ser = TempSubjectSerialize(_subject)
string = ser.get_string()
return str(string) return str(string)
def deserialize(string):
temp = TempSubjectDeserialize(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]['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
+1
View File
@@ -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}}}
+8 -1
View File
@@ -1,5 +1,5 @@
def user_input(): 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": if todo == "see":
print("Which subject do you want to know the information?") print("Which subject do you want to know the information?")
subject = input() subject = input()
@@ -31,6 +31,8 @@ def user_input():
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")
return "input", "grade", subject, category, grade, grade_name return "input", "grade", subject, category, grade, grade_name
elif todo == "exit":
return "exit"
def user_output(subject, categories, average, average_subject): def user_output(subject, categories, average, average_subject):
@@ -42,3 +44,8 @@ def user_output(subject, categories, average, average_subject):
string += str(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 password():
print("What is your password?")
return input()