def testGetStudentGrades(): """ Test function for getForDisc """ rep = GradeFileRepository("testgrade.txt") rep.removeAll() st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 10) rep.store(gr) gr = Grade(st, "SO", 9.5) rep.store(gr) studentGrades = rep.getAllForDisc("FP") assert len(studentGrades) == 1 assert studentGrades[0].getStudentID() == "1" assert studentGrades[0].getGrade() == 10 assert studentGrades[0].getDiscipline() == "FP" st = Student("2", "Ionel", Address("Adr2", 22, "Cluj")) gr = Grade(st, "SO", 9.5) rep.store(gr) studentGrades = rep.getAllForDisc("SO") assert len(studentGrades) == 2 assert studentGrades[0].getStudentID() == "1" assert studentGrades[1].getStudentID() == "2"
def testGetGrades(): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 10) rep = GradeRepository() rep.store(gr) gr = Grade(st, "SO", 9.5) rep.store(gr) grades = rep.getAll(st) assert grades[0].getStudent() == st assert grades[0].getGrade() == 10 assert grades[0].getDiscipline() == "FP"
def testGrade(self): repog = gradeRepository() repog.add(Grade(1, 1, 9)) self.assertEqual(str(repog.getPos(0)), str(Grade(1, 1, 9))) repog.add(Grade(1, 1, 8)) self.assertEqual(str(repog.getPos(1)), str(Grade(1, 1, 8))) repog.add(Grade(2, 2, 9)) self.assertEqual(str(repog.getPos(2)), str(Grade(2, 2, 9))) repog.removeGradeStudent(Student(2, "Razvan")) self.assertEqual(str(repog.getPos(1)), str(Grade(1, 1, 8))) repog.add(Grade(2, 2, 9)) self.assertEqual(str(repog.getPos(2)), str(Grade(2, 2, 9))) repog.removeGradeDiscipline(Discipline(1, "Fizica")) self.assertEqual(str(repog.getPos(0)), str(Grade(2, 2, 9)))
def find(self, st, disc): """ Lookup a grade for a given student and discipline st - student disc - discipline return Grade or None if there is no grade in the repository """ #open the file for read and count the number of lines try: f = open(self.__fname, "r") except IOError: #the file is not created yet return None try: line = f.readline().strip() while line != "": attrs = line.split(",") #if this line refers to the requested student and discipline isSameStudent = attrs[0] == st.getId() and attrs[1] == disc if isSameStudent: rez = Grade(st, disc, float(attrs[2])) f.close() return rez line = f.readline().strip() f.close() return None except IOError: raise RepositorException("Unable to read grades from the file") return None
def getAll(self, st): """ Return the grades for a given student st student return a list of grades """ try: f = open(self.__fname, "r") except IOError: #the file is not created yet return None try: rez = [] line = f.readline().strip() while line != "": attrs = line.split(",") #if this line refers to the requested student if attrs[0] == st.getId(): gr = Grade(st, attrs[1], float(attrs[2])) rez.append(gr) line = f.readline().strip() f.close() return rez except IOError: raise RepositorException("Unable to read grades from the file")
def testStoreGrade(): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 9.5) rep = GradeRepository() assert rep.size() == 0 rep.store(gr) assert rep.size() == 1 gr2 = rep.find(st, "FP") assert gr2 == gr gr = Grade(st, "FP", 9) try: rep.store(gr) assert False except GradeAlreadyAssigned: assert True
def statistics_students_failing(self): partial_grades = self.__gradeRepository.get_all() grades = [] for grade in partial_grades: student = self.__studentRepository.search( Student(grade.get_student().get_stud_id(), "")) discipline = self.__disciplineRepository.search( Discipline(grade.get_discipline().get_dis_id(), "")) grades.append(Grade(student, discipline, grade.get_value())) students = {} for grade in grades: if not grade.get_student().get_stud_id() in students: students[grade.get_student().get_stud_id()] = [] students[grade.get_student().get_stud_id()].append( grade.get_value()) failing = [] for item in students.items(): stud_id = item[0] stud_name = self.__studentRepository.search(Student( stud_id, "")).get_stud_name() average = sum(item[1]) / len(item[1]) if average < 5.0: fail_stud_dto = StudentDTO(stud_name, average) failing.append(fail_stud_dto) failing.sort(key=lambda x: x.get_average(), reverse=False) return failing[:]
def update_grade(self, student_id, discipline_id, old_value, new_value): ''' Updates a the grade identified by student_id, discipline_id and the old_value, and replaces its value with a new_value :param student_id: :param discipline_id: :param old_value: :param new_value: :return: None ''' old_grade = self.find_specific(student_id, discipline_id, old_value) new_grade = Grade(student_id, discipline_id, new_value) if old_grade is None: old_grade = Grade(student_id, discipline_id, old_value) self.__grade_repository.update(old_grade.entity_id, new_grade)
def statistics_students_best(self): partial_grades = self.__gradeRepository.get_all() grades = [] for grade in partial_grades: student = self.__studentRepository.search( Student(grade.get_student().get_stud_id(), "")) discipline = self.__disciplineRepository.search( Discipline(grade.get_discipline().get_dis_id(), "")) grades.append(Grade(student, discipline, grade.get_value())) students = {} for grade in grades: if not grade.get_student().get_stud_id() in students: students[grade.get_student().get_stud_id()] = [] students[grade.get_student().get_stud_id()].append( grade.get_value()) best = [] for item in students.items(): stud_id = item[0] stud_name = self.__studentRepository.search(Student( stud_id, "")).get_stud_name() average = sum(item[1]) / len(item[1]) best_stud = StudentDTO(stud_name, average) best.append(best_stud) best.sort(key=lambda x: x.get_average(), reverse=True) return best[:3]
def statistics_disciplines(self): partial_grades = self.__gradeRepository.get_all() grades = [] for grade in partial_grades: student = self.__studentRepository.search( Student(grade.get_student().get_stud_id(), "")) discipline = self.__disciplineRepository.search( Discipline(grade.get_discipline().get_dis_id(), "")) grades.append(Grade(student, discipline, grade.get_value())) disciplines = {} for grade in grades: if not grade.get_discipline().get_dis_id() in disciplines: disciplines[grade.get_discipline().get_dis_id()] = [] disciplines[grade.get_discipline().get_dis_id()].append( grade.get_value()) results = [] for item in disciplines.items(): dis_id = item[0] dis_name = self.__disciplineRepository.search( Discipline(dis_id, "")).get_dis_name() average = sum(item[1]) / len(item[1]) dis_dto = DisciplineDTO(dis_name, average) results.append(dis_dto) results.sort(key=lambda x: x.get_average(), reverse=True) return results[:]
def test_checkExistentIdInRepository(self): args = ['1', "Mate"] self.__discipline_controller.addDiscipline(args) args = ['1', "Corina"] self.__student_controller.addStudent(args) args = ["1", "1", 2] self.__grade_controller.addGrade(args) gr1 = Grade("1", "2", '8')
def __loadFromFile(self): f = open(self.file, "r") for line in f: id, object, grade = line.split(",") stud = self.srepo.findByID(id) grade = Grade(stud, object, grade) self.list.append(grade) f.close()
def testValidateGrade(): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 11) val = GradeValidator() try: val.validate(gr) assert False except ValidationException: assert True
def assign_lab_to_student(self, student_id, lab_no, lab_problem_no): grade_list = self.__grade_repo.get_all() for grade in grade_list: if grade.student_id == student_id and grade.laboratory_no == lab_no: raise ControllerException( "Student with ID {0} has already been assigned a problem for lab no {1}." .format(student_id, lab_no)) new_grade = Grade(student_id, lab_no, lab_problem_no, 0) self.__grade_repo.store(new_grade)
def get_grades(self): grades = self.__gradeRepository.get_all() rez = [] for grade in grades: student = self.__studentRepository.search( Student(grade.get_student().get_stud_id(), "")) discipline = self.__disciplineRepository.search( Discipline(grade.get_discipline().get_dis_id(), "")) rez.append(Grade(student, discipline, grade.get_value())) return rez
def readGrade(): ''' reads o gradeType object from the keyboard ''' print("student:") s = UI.readID() print("discipline:") d = UI.readID() grade = int(input("grade: ")) return Grade(d, s, grade)
def add_grade(self, stud_id, dis_id, grad_value): student = self.__studentRepository.search(Student(stud_id, "")) discipline = self.__disciplineRepository.search(Discipline(dis_id, "")) grade = Grade(student, discipline, grad_value) self.__gradeValidator.validate_grade(grade) self.__gradeRepository.add(grade) action = UndoRedoAction(self.__gradeRepository, self.__gradeRepository.remove, self.__gradeRepository.add, grade) self.__undoStack.push(action) self.__redoStack.clear()
def __init_from_file(self): file_handler = open(self.__file_name, 'r') input_data = file_handler.read() input_data = input_data.strip("\n") input_data = input_data.split('\n') for line in input_data: this_line = line.split(' ') grade = Grade(int(this_line[0]), int(this_line[1]), int(this_line[2]), int(this_line[3])) self.store(grade) file_handler.close()
def addGrade(self, disciplineID, studentID, gradeValue): grade = Grade(disciplineID, studentID, gradeValue) self.checkExistentIdInRepository(grade, self.__student_repository, self.__discipline_repository) self.__grade_repository.save(grade) redo = FunctionCall(self.addGrade, disciplineID, studentID, gradeValue) undo = FunctionCall(self.removeGrade, disciplineID, studentID, gradeValue) operation = Operation(redo, undo) self.__undoController.recordOperation(operation)
def add_grade(self, student_id, discipline_id, grade_value): ''' Adds a grde to the repository :param student_id: :param discipline_id: :param grade_value: :return: None :exception: ControllerOperationExcption if the student with student_id is not enrolled for discipline with \ discipline_id ''' self.__validate(student_id, discipline_id) link_id = (student_id, discipline_id) if self.__enroll_controller.find_by_id(link_id) is None: raise ControllerOperationException('student was not enrolled for given discipline') grade = Grade(student_id, discipline_id, grade_value) self.__grade_repository.add(grade)
def assign(self, stId, disc, grade): """ Assign a grade for a student at a given discipline stID String, id of the student disc String, discipline grade double, grade post: the grade is stored return Grade raise ValidateException for invalid grade StudentNotFound if there is no student for the given id """ #lookup student st = self.__stRep.find(stId) if st == None: raise StudentNotFoundException() gr = Grade(st, disc, grade) #validate grade self.__grVal.validate(gr) #store grade self.__grRep.store(gr) return gr
def grade(self, student_id, lab_no, value): grades_obtained = self.find_by_student_id(student_id) can_be_graded = False old_grade = None for grade in grades_obtained: if grade.laboratory_no == lab_no: can_be_graded = True if grade.value != 0: raise ControllerException( "Student with ID {0} is already graded at laboratory number {1}." .format(student_id, lab_no)) old_grade = grade if not can_be_graded: raise ControllerException( "Student with ID {0} was not assigned a problem for laboratory number {1}." .format(student_id, lab_no)) if can_be_graded: if old_grade is not None: self.__grade_repo.remove(old_grade) grade = Grade(student_id, lab_no, old_grade.laboratory_problem, value) self.__grade_repo.store(grade)
def assign_lab_to_group(self, student_id_list, lab_no): current_problem = 1 did_add = False for student_id in student_id_list: grades_obtained = self.find_by_student_id(student_id) is_assigned = False if grades_obtained is not None: for grade in grades_obtained: if grade.laboratory_no == lab_no: is_assigned = True if not is_assigned: new_grade = Grade(student_id, lab_no, current_problem, 0) self.__grade_repo.store(new_grade) print( "Student with ID {0} was assigned problem number {1} at lab number {2}." .format(student_id, current_problem, lab_no)) current_problem += 1 did_add = True if not did_add: print( "All students from that group were already assigned with a problem for lab number {0}." .format(lab_no))
def update(self, student_id, discipline_id, grade_value, new_grade): ''' :args: student_id - the student id of the old grade discipline_id - the discipline id of the old grade grade_value - the grade value of the old grade new_grade - the entity of the grade that will replace the old one :effect: replace the old grade with the new_grade if it exists add new_grade to the repository otherwise ''' grades = self.__grade_repository.get_all() old_grade = None for i in range(len(grades)): if grades[i].student_id == student_id and grades[i].discipline_id == discipline_id and \ grades[i].value == grade_value: old_grade = grades[i] if old_grade is None: raise ControllerOperationException("you can't update something that does not exist") new_grade = Grade(student_id, discipline_id, new_grade) self.remove(old_grade.student_id, old_grade.discipline_id, old_grade.grade_value) self.add_grade(new_grade)
def test_entity_ID(self): g1 = Grade('1', '2', "8") self.assertEqual(g1.entity_ID, g1.entity_ID, "Id should be 1")
def test_studentID(self): g1 = Grade('1', '2', "8") self.assertEqual(g1.disciplineID, '1', "Id should be 1")
def test_grade_value(self): g1 = Grade('1', '2', "8") self.assertEqual(g1.grade_value, '8', "Id should be 8")
def mainMenu(self): keepAlive = True while keepAlive: try: UI.printMenu() command = input("Enter command: ").strip() if command == '0': print("exit...") keepAlive = False elif command == '1': s = UI.readStudent() self._controller.addStudent(s) elif command == '2': for n in self._controller.getAllStudents(): print("ID: " + str(n.getID()) + " Name: " + n.getName()) elif command == '3': i = UI.readID() #s = UI.readStudent() s = self._controller.findStudentById(i) self._controller.removeStudent(s) elif command == '4': print("enter the old Student") os = UI.readStudent() print("enter the new Student") ns = UI.readStudent() if (os._ID != ns._ID): print("Error encountered - the id should be the same!") else: self._controller.updateStudent(os, ns) elif command == '5': d = UI.readDiscipline() self._controller.addDiscipline(d) elif command == '6': for n in self._controller.getAllDisciplines(): print("ID: " + str(n.getID()) + " Name: " + n.getName()) elif command == '7': i = UI.readID() #d=UI.readDiscipline() d = self._controller.findDisciplineById(i) self._controller.removeDiscipline(d) elif command == '8': print("enter the old discipline") od = UI.readDiscipline() print("enter the new discipline") nd = UI.readDiscipline() if (od._ID != nd._ID): print("Error encountered - the id should be the same!") else: self._controller.updateDiscipline(od, nd) elif command == '9': g = UI.readGrade() self._controller.addGrade(g) elif command == '10': k = 0 s = UI.readID() for n in self._controller.getAllGrades(): if n.getSID() == s: n = str(n) n = n.split() for d in self._controller.getAllDisciplines(): if str(d.getID()) == str(n[0]): print(n[2], "at", d.getName()) k = 1 if k == 0: print("the student has no grades!") elif command == '11': k = 0 d = UI.readID() for n in self._controller.getAllGrades(): if n.getDID() == d: n = str(n) n = n.split() for x in self._controller.getAllStudents(): if str(x.getID()) == str(n[1]): print(n[2], x.getName()) k = 1 if k == 0: print("there is no grade at this discipline!") elif command == '12': print( "\t 1.search for students and disciplines by ID \t 2.search for students by name \t 3.search for disciplines by title" ) cmd = input("select the search method:").strip() if cmd == '1': uid = UI.readID() print(self._controller.findStudentById(uid).getName()) print( self._controller.findDisciplineById(uid).getName()) elif cmd == '2': try: nem = UI.readName() sl = self._controller.findStudentByName(nem) for i in sl: print("ID: " + str(i.getID()) + " Name: " + i.getName()) except ValueError as ve: print(ve) elif cmd == '3': try: nem = UI.readName() dl = self._controller.findDisciplineByName(nem) for i in dl: print("ID: " + str(i.getID()) + " Name: " + i.getName()) except ValueError as ve: print(ve) elif command == '13': st = Statistics(self._controller) st.AllStudentsEnrolledAtDiscipline() elif command == '14': st = Statistics(self._controller) st.FailingStuds() elif command == '15': st = Statistics(self._controller) st.BestSchoolSitutaion() elif command == '16': st = Statistics(self._controller) st.ActDisc() elif command == '17': self._undoController.undo() elif command == '18': self._undoController.redo() elif command == '19': self._controller.removeGrade(Grade(1, 1, 1), False) else: print("INVALID COMMAND!") except Exception as exc: print("Error encountered - " + str(exc))
repos.add(Student(5, "George")) repos.add(Student(6, "Andrei")) repos.add(Student(7, "Alex")) repos.add(Student(8, "Vlad Andrei")) repos.add(Student(9, "Adrian")) repos.add(Student(10, "Razvan")) repos.add(Student(11, "Razvan Andrei")) repod.add(Discipline(1, "Fizica")) repod.add(Discipline(2, "Matematica")) repod.add(Discipline(3, "Romana")) repod.add(Discipline(4, "Informatica")) repod.add(Discipline(5, "Chimie")) repod.add(Discipline(6, "Engleza")) repod.add(Discipline(7, "Franceza")) repod.add(Discipline(8, "Matematica aplicata")) repog.add(Grade(2, 2, 9)) repog.add(Grade(2, 2, 8)) repog.add(Grade(1, 2, 9)) repog.add(Grade(1, 1, 10)) repog.add(Grade(3, 4, 9)) repog.add(Grade(4, 4, 10)) repog.add(Grade(3, 4, 3)) repog.add(Grade(4, 4, 2)) repog.add(Grade(3, 4, 2)) repog.add(Grade(4, 4, 3)) repog.add(Grade(1, 1, 2)) repog.add(Grade(1, 1, 2)) repog.add(Grade(1, 1, 3)) repog.add(Grade(2, 1, 2)) repog.add(Grade(2, 1, 2)) repog.add(Grade(2, 1, 3))
def test_validate(self): g1 = Grade('1', '1', "a") self.assertRaises(GradeValidatorException, GradeValidator.validate, g1) g2 = Grade('1', '1', "11") self.assertRaises(GradeValidatorException, GradeValidator.validate, g2)