def store(self, aid, description, day, month, year): ''' store Function to add an assignment Arguments: aid {string} -- assignment id description {string} -- description day {string} -- deadline day month {strign} -- deadline month year {string} -- deadline year Raises: AssignmentException: Invalid ID AssignmentException: ID already exists ''' if not aid.isdigit(): raise AssignmentException('Invalid ID') aid = int(aid) assignment = self._repo.find(aid) if assignment is not None: raise AssignmentException('ID already exists') day = int(day) month = int(month) year = int(year) deadline = datetime.date(year, month, day) undo = FunctionCall(self._repo.delete, aid) redo = FunctionCall(self._repo.store, Assignment(aid, description, deadline)) op = Operation(undo, redo) self._undo.recordOperation(op) self._repo.store(Assignment(aid, description, deadline))
def testGr(self): stud = Student("42", "Mihai Adamescu", "913") stud2 = Student ("12", "Adfs Aop", "915") ass = Assignment("22", "imi plac caii", "20.4") ass2 = Assignment("29", "nu imi plac caii", "20.9") gr = Grade("23", ass, stud, "7") self.assertTrue(gr.getId() == "23") gr.setStud(stud2) gr.setAss(ass2) gr.setGrade("9") self.assertTrue(gr.getAss() == ass2) self.assertTrue(gr.getStud() == stud2) self.assertTrue(gr.getGrade() == "9")
def testValidateAss(self): validator = AssignmentValidator() ass = Assignment("22","imi plac caii","20.4") try: validator.validate(ass) self.assertTrue(True) except: pass
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 _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 reading(self): file = open("/Users/AndiD//Documents/Eclipse/lab57/assignment.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") assign = Assignment(int(list[0]), list[1], list[2]) for asgn in Repository.get_all(self): if asgn.get_id() == int(list[0]): raise ControllerException("Can not add assignment with same id") Repository.add_item(self, assign) file.close()
def update_assignment(self,assign_id,description,deadline,grade): ''' Updates an assignment from the repository after validating its new properties Input: :param assign_id :param description :param deadline :param grade ''' assign = Assignment(assign_id,description,deadline,grade) self.__assign_validator.validate_assignment(assign) self.__assign_repository.update_assignment(assign)
def add_assignment(self,assign_id,description,deadline,grade): ''' Adds a new assignment to the repository after validating its properties Input: :param assign_id :param description :param deadline :param grade ''' assign = Assignment(assign_id,description,deadline,grade) self.__assign_validator.validate_assignment(assign) self.__assign_repository.add_assignment(assign)
def __loadFromFile(self): try: f = open(self.__fName, "r") line = f.readline().strip() while line != "": params = line.split(",") ass = Assignment(params[0], params[1], params[2]) Repository.store(self, ass) line = f.readline().strip() except IOError: pass finally: f.close()
def _loadFile(self): f = open(self._file, 'r') for x in f: if x == '': break a = x a = a.strip() a = a.split(',') self.store(y := Assignment( int(a[0]), a[1], datetime.date(int(a[4]), int(a[3]), int( a[2])))) # print(y) f.close()
def reading(self): file = open( "/Users/ecaterinacarazan/PycharmProjects/files/assignment.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") assign = Assignment(int(list[0]), list[1], list[2]) for asgn in Repository.get_all(self): if asgn.get_id() == int(list[0]): raise ControllerException( "Can not add assignment with same id") Repository.add_item(self, assign) file.close()
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): 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( Assignment( int(data[i][0]), data[i][2], datetime.date(int(data[i][4]), int(data[i][3]), int(data[i][2])))) f.close()
def update_assignment(self, id_assign, description, deadline): """ Finds the assignment with the same id and changes the rest of the fields with the new values :param id_assign: positive integer :param description: string :param deadline: string :return: """ assign = Assignment(id_assign, description, deadline) self._validator.validate_assignment(assign) self._repository.update_item(assign) new_assign = self.get_assignment(id_assign) self._operations.append(UpdateOperation(assign, new_assign)) self._index += 1 self.controller_undo_redo.add([self])
def update_assignment(self, id_assign, student_id, description, deadline, grade): """ Finds the assignment with the same id and changes the rest of the fields with the new values :param id_assign: positive integer :param student_id: positive integer :param description: string :param deadline: string :param grade: positive integer < 10 :return: """ assign = Assignment(id_assign, student_id, description, deadline, grade) self._validator.validate_assignment(assign) self._repository.update_assignment(assign)
def add_assignment(self, id_assign, description, deadline): """ Adds an assignment to the list of assignments :param id_assign: positive integer :param description: string :param deadline: string :return: """ assign = Assignment(id_assign, description, deadline) self._validator.validate_assignment(assign) for asgn in self._repository.get_all(): if asgn.get_id() == id_assign: raise ControllerException("Can not add assignment with same id") self._repository.add_item(assign) self._operations.append(AddOperation(assign)) self._index += 1 self.controller_undo_redo.add([self])
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(y := Assignment( int(result[i][0]), result[i][1], datetime.date(int(result[i][4]), int(result[i][3]), int(result[i][2])))) f.close()
def add_assignment(self, id_assign, student_id, description, deadline, grade): """ Adds an assignment to the list of assignments :param id_assign: positive integer :param student_id: positive integer :param description: string :param deadline: string :param grade: positive integer < 10 :return: """ assign = Assignment(id_assign, student_id, description, deadline, grade) self._validator.validate_assignment(assign) # for assign in self._repository.get_list_of_assignments(): # if assign.get_assignment_id() == id_assign: # raise ControllerException("Can not add assignment with same id") self._repository.add_assignment(assign)
def GenerateAssignments(): """ GenerateAssignments Function that generates a list of assignments randomly Returns: list -- list of assignments """ idlist = [] alist = [] for i in range(20): sid = random.randint(201, 400) while sid in idlist: sid = random.randint(201, 400) idlist.append(sid) desclist = ['delete', 'insert', 'add', 'list', 'update', 'remove', 'implement', 'use classes', 'use modules', 'sort', 'filter', 'undo', 'redo', 'test', 'create GUI', 'implement PyUnit tests', ' do something'] for i in range(20): start_dt = datetime.date.today().replace(day=31, month=1, year=2019).toordinal() end_dt = datetime.date.today().replace(day=31, month=12, year=2020).toordinal() random_day = datetime.date.fromordinal(random.randint(start_dt, end_dt)) alist.append(Assignment(idlist[i], random.choice(desclist), random_day)) return alist
def add(self, assId, description, deadline): assignment = Assignment(assId, description, deadline) self.__validator.validate(assignment) self.__repository.store(assignment) return assignment
def updateAss(self, assId, newDesc, newDeadline): assignment = Assignment(assId, newDesc, newDeadline) self.__validator.validate(assignment) assignment = self.__repository.update(assignment) return assignment
def update_assignment(self, assign_id, description, deadline, grade): assign = Assignment(assign_id, description, deadline, grade) self.__assign_validator.validate_assignment(assign) self.__assign_repository.update_assignment(assign)
def test_add_student(self): assignment = Assignment(1, "fain", "23 12 1996", 10) self.__repository.add_assignment(assignment) self.assertEqual(len(self.__repository.list_all_assignments()), 1, "There should be 1 assignment in the test repository!")
def setUp(self): super().setUp() self.__assignment = Assignment(1, "fain", "23 12 1996", 10)
def testAss(self): ass = Assignment("22","imi plac caii","20.4") self.assertTrue(ass.getId() == "22") self.assertTrue(ass.getDescription() == "imi plac caii") self.assertTrue(ass.getDeadline() == "20.4") ass.setAssignmentId("23") ass.setDeadline("4.4") ass.setDescription("cu placere") self.assertTrue(ass == Assignment("23","cu placere","4.4"))
def remove_assignment(self, assign_id, description, deadline, grade): assign = Assignment(assign_id, description, deadline, grade) self.__assign_validator.validate_assignment(assign) self.__assign_repo.remove_assignment(assign)