def gradeStud(self, stud, assignment, grade): ''' gradeStud Function to grade a student Arguments: stud {string} -- student id assignment {string} -- assignment id grade {string} -- grade Raises: StudentException: Invalid ID AssignmentException: Invalid ID GradeException: Invalid Grade GradeException: Invalid Grade StudentException: Student does not exist AssignmentException: Assignment does not exist GradeException: Student already has a grade for given assignment GradeException: Student does not have that assignment ''' if not stud.isdigit(): raise StudentException('Invalid ID') if not assignment.isdigit(): raise AssignmentException('Invalid ID') if not grade.isdigit(): raise GradeException('Invalid grade') grade = int(grade) if grade < 1 or grade > 10: raise GradeException('Invalid grade') stud = int(stud) assignment = int(assignment) stud = self._studs.find(stud) assignment = self._assigns.find(assignment) if stud is None: raise StudentException('Student does not exist') if assignment is None: raise AssignmentException('Assignment does not exist') for i in self._repo.getAll: if i.IdStud == stud.Id and i.IdAssignment == assignment.Id and i.Grade != -1: raise GradeException( 'Student already has a grade for that assignment') g = 0 for i in self._repo.getAll: if i.IdStud == stud.Id and i.IdAssignment == assignment.Id: g = i if isinstance(g, int): raise GradeException('Student does not have that assignment') undo = FunctionCall(self._repo.update, g) redo = FunctionCall(self._repo.update, Grade(stud, assignment, g.Id, grade)) op = Operation(undo, redo) self._undo.recordOperation(op) self._repo.update(Grade(stud, assignment, g.Id, grade))
def reading(self): file = open("/Users/ecaterinacarazan/PycharmProjects/files/grade.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") student = self.repo_stud.get_by_id(int(list[1])) assign = self.repo_assign.get_by_id(int(list[2])) grade = Grade(int(list[0]), student, assign, int(list[3])) for g in Repository.get_all(self): if grade.get_id() == g.get_id(): raise ControllerException("Can not add grade with same id") Repository.add_item(self, grade) file.close()
def reading(self): file = open("/Users/AndiD//Documents/Eclipse/lab57/grade.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") student = self.repo_stud.get_by_id(int(list[1])) assign = self.repo_assign.get_by_id(int(list[2])) grade = Grade(int(list[0]), student, assign, int(list[3])) for g in Repository.get_all(self): if grade.get_id() == g.get_id(): raise ControllerException("Can not add grade with same id") Repository.add_item(self, grade) file.close()
def test_grade_repository(self): grade_repo = GradeRepository() self.assertIsInstance(grade_repo, GradeRepository, 'Wrong instance') self.assertIsInstance(grade_repo, Repository, 'Wrong instance') self.assertEqual(len(grade_repo._list), 0) grade_repo.store(Grade('id1', 'id2', 1)) self.assertEqual(grade_repo._list[-1].student_id, 'id1') self.assertEqual(grade_repo._list[-1].discipline_id, 'id2') self.assertEqual(grade_repo._list[-1].grade_value, 1) grade_repo.remove(0) self.assertEqual(len(grade_repo._list), 0) grade_repo.store(Grade('id1', 'id2', 1)) self.assertEqual(grade_repo._list[0].student_id, 'id1') grade_repo.update(-1, 3) self.assertEqual(grade_repo._list[-1].grade_value, 3)
def create(self, discipline_id, student_id, val): """ Creates the grade and adds it to the repository :param discipline_id: The id of the discipline :param student_id: The id of the student :param val: The grade :return: The grade obj which was added """ grade = Grade(discipline_id, student_id, val) if self.__disciplineRepository.find(discipline_id) is None: raise GradeException( "[ERROR] Can't find the discipline with the ID given") if self.__studentRepository.find(student_id) is None: raise GradeException( "[ERROR] Can't find the student with the given ID") self.__validator.validate(grade) self.__repository.store(grade) undo = FunctionCall(self.remove_single_grade, discipline_id, student_id, val) redo = FunctionCall(self.create, discipline_id, student_id, val) operation = Operation(undo, redo) self.__undoController.add_operation(operation) return grade
def add(self, discipline_id, student_id, grades): """ Creates the object and calls the repo Add :raises: IOErr """ for grade in grades: self._repo.store(Grade(student_id, discipline_id, grade))
def give_grade_by_group(self, assign_id, group): for student in self.__stud_repository.list_all_students: if str(student.group) == str(group): stud_id = student.stud_id grade = Grade(assign_id, stud_id, 0) self.__grade_validator.validate_grade(grade) self.__grade_repository.add_grade(grade)
def store(self, discipline_id, student_id, grade_value): """ Stores a Grade instance in the repository :param discipline_id: The ID of the discipline at which the grade is given :param student_id: The ID of the graded student :param grade_value: The value of the grade :return: - :exceptions: RepoException; GradeException """ self.__grade_validator.validate_discipline(discipline_id) self.__grade_validator.validate_student_id(student_id) if self.__student_repository.find_by_id(student_id) is None: raise RepositoryException("Invalid student ID!") if self.__discipline_repository.find_by_id(discipline_id) is None: raise RepositoryException("Invalid discipline ID!") student_name = self.__student_repository.find_by_id( student_id).get_name() discipline_name = self.__discipline_repository.find_by_id( discipline_id).get_name() self.__grade_validator.validate_grade_value(grade_value) grade = Grade(discipline_id, discipline_name, student_id, student_name, grade_value) self.__grades_list.append(grade)
def createGradeGroup(self, gradeId, assId, group): result = [] for student in self._studRepo.getAll(): if student.getGroup() == group: gr = Grade(gradeId, self._assRepo.find(assId), student,0) self._gradeRepo.store(gr) result.append(gr) return result
def _loadDatabase(self): current = self._connection.cursor() current.execute("SELECT * FROM grades") rows = current.fetchall() for row in rows: self._objects.append( Grade(Student(row[1], '', ''), Assignment(row[2], '', ''), row[0], row[3]))
def create(self, studentID, disciplineID, gradeValue): """ Adds a new grade to the grade repo """ grade = Grade(studentID, disciplineID, gradeValue) self._validator.validate(grade) self._repo.add(grade) return grade
def enrollStudent(self, disciplineId, studentId, grade): ''' Enrolls a students at a discipline disciplineId - the discipline where the studentId needs to be enrolled ''' g = Grade(disciplineId, studentId, grade) self._gradeValidator.validate(g, self._studentRepo, self._disciplineRepo) self._gradeRepo.enroll(g)
def addAssignmentToGroup(self, group, assignment): ''' addAssignmentToGroup Function to add assignment to a group of students Arguments: group {string} -- group of students assignment {string} -- assignment id Raises: StudentException: Group must be a number StudentException: There are no students in that group AssignmentException: Invalid assignment ID AssignmentException: Assignment does not exist ''' studs = [] if not group.isdigit(): raise StudentException('Group must be a number') group = int(group) for i in self._studs.getAll: if i.Group == group: studs.append(i) if not len(studs): raise StudentException('There are no students in that group') if not assignment.isdigit(): raise AssignmentException('Invalid assignment ID') assignment = int(assignment) assignment = self._assigns.find(assignment) if assignment is None: raise AssignmentException('Assignment does not exist') csc = CascadeOperation() for i in studs: ok = 0 for j in self._repo.getAll: if j.IdStud == i.Id and j.IdAssignment == assignment.Id: ok = 1 if not ok: gid = self.genId() undo = FunctionCall(self._repo.delete, gid) redo = FunctionCall(self._repo.store, Grade(i, assignment, gid)) op = Operation(undo, redo) csc.append(op) self._repo.store(Grade(i, assignment, gid)) self._undo.recordOperation(csc)
def setUp(self): self.__gradeRepo.add(Grade(1, 2, 10)) self.__gradeRepo.add(Grade(1, 3, 9)) self.__gradeRepo.add(Grade(2, 2, 10)) self.__gradeRepo.add(Grade(2, 2, 8)) self.__gradeRepo.add(Grade(3, 4, 4)) self.__gradeRepo.add(Grade(4, 4, 2)) self.__gradeRepo.add(Grade(4, 4, 1))
def _loadFile(self): f = open(self._file, 'r') for x in f: a = x if a == '': break a = a.strip() a = a.split(',') self.store(Grade(Student(int(a[0]), '', ''), Assignment(int(a[1]), '', ''), int(a[2]), int(a[3]))) f.close()
def testValidateGr(self): validator = GradeValidator() stud = Student("42", "Mihai Adamescu", "913") ass = Assignment("22", "imi plac caii", "20.4") gr = Grade("1", ass, stud,"5") try: validator.validate(gr) self.assertTrue(True) except: pass
def addGrade(self, disciplineId, studentId, grade): ''' Saves a grade to the repository in the format <disciplineId><studentId><gradeValue> disciplineId - the disciplineId to save studentId - the studentId to save grade - the grade value to save ''' g = Grade(disciplineId, studentId, grade) self._gradeValidator.validate(g, self._studentRepo, self._disciplineRepo) self._gradeRepo.store(g)
def give_assignment(self, assign_id, stud_id): ''' Adds a new grade to the repository after validating its properties Input: :param assign_id :param stud_id ''' grade = Grade(assign_id, stud_id, 0) self.__grade_validator.validate_grade(grade) self.__grade_repository.add_grade(grade)
def _load_file(self): with open(self._path, 'r') as fp: data = json.load(fp) for obj in data['grades']: stud_id = obj['student_id'] disc_id = obj['discipline_id'] value = obj['grade_value'] obj = Grade(discipline_id=disc_id, student_id=stud_id, grade_value=value) self._list.append(obj) fp.close()
def _load_file(self): file = open(self._path, 'r') line = file.readline() while line: line = line.strip().split() stud_id = line[0] disc_id = line[1] value = line[2] obj = Grade(discipline_id=disc_id, student_id=stud_id, grade_value=value) self._list.append(obj) line = file.readline() file.close()
def give_grade_by_group(self, assign_id, group): ''' Adds grades for a group of students after validating their properties Input: :param assign_id :param group ''' for student in self.__stud_repository.list_all_students: if str(student.group) == str(group): stud_id = student.stud_id grade = Grade(assign_id, stud_id, 0) self.__grade_validator.validate_grade(grade) self.__grade_repository.add_grade(grade)
def test_Grade(self): obj = Grade('stud_id', 'disc_id', 5) self.assertEqual(obj.student_id, 'stud_id') self.assertEqual(obj.discipline_id, 'disc_id') self.assertEqual(obj.grade_value, 5) with self.assertRaises(GradeError): obj.grade_value = 0 with self.assertRaises(GradeError): obj.grade_value = '0' obj.grade_value = '5' with self.assertRaises(GradeError): obj.grade_value = StudentError('da') with self.assertRaises(GradeError): obj.grade_value = 11 self.assertEqual(obj.__str__(), str(obj.grade_value))
def GenerateGrades(studs, assigns): """ Function that generates randomly a list of grades :return: list of grades """ glist = [] idlist = [] for i in range(35): sid = random.choice(studs) aid = random.choice(assigns) gr = random.randint(-1, 10) while gr == 0: gr = random.randint(-1, 10) # gr = -1 gid = random.randint(1000, 1999) while gid in idlist: gid = random.randint(1000, 1999) idlist.append(gid) glist.append(Grade(sid, aid, gid, gr)) while any(x.IdAssignment == glist[i].IdAssignment and x.IdStud == glist[i].IdStud for x in glist if glist.index(x) != i): aid = random.choice(assigns) glist[i] = Grade(sid, aid, gid, gr) return glist
def _loadFile(self): with open(self._file) as f: try: data = json.load(f) except Exception: f.close() return for i in data.keys(): data[i] = data[i].split(',') self.store( Grade(Student(int(data[i][0]), '', ''), Assignment(int(data[i][1]), '', ''), int(data[i][2]), int(data[i][3]))) f.close()
def _loadFile(self): try: file = open(self._fileName, "r") entry = file.readline() while len(entry) > 2: entryList = entry.split(',') GradeRepo.add( self, Grade(int(entryList[0]), entryList[1], entryList[2][:-1])) entry = file.readline() except FileNotFoundError as fnfe: raise ValueError("Cannot open file " + str(fnfe)) finally: file.close()
def __loadFromFile(self): try: f = open(self.__fileName, "r") line = f.readline().strip() while line != "": attrs = line.split(",") grade = Grade(int(attrs[0]), int(attrs[1]), int(attrs[2])) GradeRepository.add(self, grade) line = f.readline().strip() except IOError: raise FileRepositoryException() finally: f.close()
def update_grade(self, grade_id, grade): """ updates a grade :param grade_id: > 0 :param grade: between 0 and 10 :return: """ g = self.__repository.get_by_id(grade_id) gr = Grade(g.get_id(), g.get_student(), g.get_assignment(), grade) self.__validator.validate_grade(gr) self.__repository.update_item(gr) new_grade = self.get_by_id(grade_id) self._operations.append(UpdateOperation(g, new_grade)) self._index += 1 self.controller_undo_redo.add([self])
def addAssignmentToStud(self, stud, assignment): ''' addAssignmentToStud Function to give an assignment to a student Arguments: stud {string} -- ID of student assignment {strin} -- id of assignment Raises: StudentException: Invalid ID AssignmentException: Invalid ID StudentException: Student does not exist AssignmentException: Assignment does not exist GradeException: Student already has that assignment ''' if not stud.isdigit(): raise StudentException('Invalid ID') if not assignment.isdigit(): raise AssignmentException('Invalid ID') stud = int(stud) assignment = int(assignment) stud = self._studs.find(stud) assignment = self._assigns.find(assignment) if stud is None: raise StudentException('Student does not exist') if assignment is None: raise AssignmentException('Assignment does not exist') for i in self._repo.getAll: if i.IdStud == stud.Id and i.IdAssignment == assignment.Id: raise GradeException('Student already has that assignment') gid = self.genId() undo = FunctionCall(self._repo.delete, gid) redo = FunctionCall(self._repo.store, Grade(stud, assignment, gid)) op = Operation(undo, redo) self._undo.recordOperation(op) self._repo.store(Grade(stud, assignment, gid))
def testName(self): """ Testing all functions that modify the repository """ self._repo.enroll(self._grade) self._repo.store(self._grade) self.assertEqual(len(self._repo), 1) g = Grade("1", "3", [10]) self._repo.enroll(g) self._repo.store(g) self.assertEqual(len(self._repo), 2) self._repo.removeStudent("2") self.assertEqual(len(self._repo), 1) self._repo.removeDiscipline("1") self.assertEqual(len(self._repo), 0)
def add(self, disciplineID, studentID, gradeValue): """ Assign a grade for a student at a given discipline input: studentId - id of the student receiving the grade discipline - the discipline they are graded in gradeValue - numerical value of the grade. Must be an integer between 1 and 10 output: The created Grade object raises: ValidateException for invalid gradeValue StudentNotFound if there is no student for the given id """ # student = self.__studentRepo.find(studentId) # if student == None: # raise StudentNotFoundException("Student with id = " + str(studentId) + " not found.") """ Create new Grade object Validate it Store it Return it """ try: studentID = int(studentID) except ValueError: raise GradeControllerException("Student ID must be an integer") if self.__studentRepo.findByID(studentID) == None: raise GradeControllerException("Inexistent student ID") try: disciplineID = int(disciplineID) except ValueError: raise GradeControllerException("Discipline ID must be an integer") if self.__disciplineRepo.findByID(disciplineID) == None: raise GradeControllerException("Inexistent discipline ID") try: gradeValue = int(gradeValue) except ValueError: raise GradeControllerException("Grade Value must be an integer") newGrade = Grade(studentID, disciplineID, gradeValue) self.__validator.validate(newGrade) self.__gradeRepo.add(newGrade)
def _loadFile(self): result = [] try: f = open(self._file, 'rb') result = pickle.load(f) except EOFError: result = [] except IOError as err: raise err if len(result) == 0: return [] for i in range(len(result)): result[i] = result[i].strip() result[i] = result[i].split(',') self.store( Grade(Student(int(result[i][0]), '', ''), Assignment(int(result[i][1]), '', ''), int(result[i][2]), int(result[i][3]))) f.close()