Created
October 25, 2019 07:33
-
-
Save ahmdrz/ce433f17efcf8e85c03297da1061b93e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
class School: | |
def __init__(self, name): | |
self.name = name | |
self.students = [] | |
def to_dict(self): | |
return { | |
'name': self.name, | |
'students': map(Student.to_dict, self.students), | |
} | |
def from_dict(self, input_dict): | |
self.name = input_dict['name'] | |
self.students = map(Student.from_dict, input_dict['students']) | |
def save(self, file_path='database.json'): | |
with open(file_path, 'w') as file_handler: | |
file_handler.write(json.dumps(self.to_dict(), indent=4)) | |
def load(self, file_path='database.json'): | |
with open(file_path, 'r') as file_handler: | |
dictionary = json.load(file_handler) | |
self.from_dict(dictionary) | |
def find_student(self, student): | |
for i, std in enumerate(self.students): | |
if std.national_id == student.national_id: | |
return i | |
return -1 | |
def add_student(self, student): | |
index = self.find_student(student) | |
if index != -1: | |
return | |
self.students.append(student) | |
def remove_student(self, student): | |
index = self.find_student(student) | |
if index == -1: | |
return | |
self.students.pop(index) | |
class Student: | |
max_age = 18 | |
def __init__(self, first_name, last_name, national_id, birth_year=''): | |
self.first_name = first_name | |
self.last_name = last_name | |
self.national_id = national_id | |
self.birth_year = birth_year | |
self.lessons = [] | |
@staticmethod | |
def from_dict(dict_input): | |
std = Student( | |
first_name = dict_input['first_name'], | |
last_name = dict_input['last_name'], | |
national_id = dict_input['national_id'] | |
) | |
std.birth_year = dict_input['birth_year'] | |
std.lessons = dict_input['lessons'] | |
return std | |
def to_dict(self): | |
return { | |
'first_name': self.first_name, | |
'last_name': self.last_name, | |
'national_id': self.national_id, | |
'birth_year': self.birth_year, | |
'lessons': self.lessons, | |
} | |
def add_lesson(self, name, score): | |
index = self.find_lesson(name) | |
if index != -1: | |
return | |
self.lessons.append({ | |
'name': name, | |
'score': score | |
}) | |
def find_lesson(self, name): | |
for i in range(len(self.lessons)): | |
lesson = self.lessons[i] | |
if lesson['name'] == name: | |
return i | |
return -1 | |
def remove_lesson(self, name): | |
index = self.find_lesson(name) | |
if index == -1: | |
return | |
self.lessons.pop(index) | |
def edit_lesson(self, name, score): | |
index = self.find_lesson(name) | |
if index == -1: | |
return | |
self.lessons[index]['score'] = score | |
if __name__ == "__main__": | |
school = School(name='anghezi') | |
school.load() | |
yaro_index = school.find_student(Student(first_name='', last_name='', national_id=84511)) | |
if yaro_index == -1: | |
print('yaro not found') | |
exit(1) | |
mamadreza_zibaei = school.students[yaro_index] | |
mamadreza_zibaei.add_lesson('varzesh', 10) | |
school.students[yaro_index] = mamadreza_zibaei | |
amirreza = Student( | |
first_name = 'amirreza', | |
last_name = 'moeini', | |
national_id = 18784 | |
) | |
school.add_student(amirreza) | |
school.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment