def remove(self, objectID): ''' Removes an object from repo list Input: -objID- positive integer Output: True - if removing succeded raises RepoException ''' if type(objectID) != int or objectID <= 0: raise RepoException("ObjectID must be a positive integer!") pos = self.find(objectID) if pos != -1: del self._objects[pos] return True raise RepoException("An object with this ID does not exit!")
def removeGrade(self, grade): ''' Removes a grade from repo list Input: -grade- Grade object Output: True - if removing succeded raises RepoException ''' for gr in self._objects: if type(gr) != Grade or type(grade) != Grade: raise RepoException("This method can only be used with Grade objects!") for i in range(len(self._objects)): if self._objects[i] == grade: del self._objects[i] return True raise RepoException("In this repository does not exit such a grade!")
def __findGr(self, obj): if type(obj) != Grade: raise RepoException("Object must be a grade!") for i in range(len(self._objects)): if self._objects[i].assignID == obj.assignID and self._objects[i].studentID == obj.studentID: return i return -1
def add(self, obj): ''' Adds a new object into repo list Input: -obj- object Output: True - if adding succeded raises RepoException ''' if self.find(obj.id) == -1 or (type(obj) == Grade and self.__findGr(obj) == -1): self._objects.append(obj) return True raise RepoException("An object with this ID already exists!")
def find(self, objectID): ''' Finds an object from repo list Input: -objID- positive integer Output: - i - int which is position of element with given id - -1 - if there is no object with given id ''' if type(objectID) != int or objectID <= 0: raise RepoException("ObjectID must be a positive integer!") for i in range(len(self._objects)): if self._objects[i].id == objectID: return i return -1
def update(self, obj): ''' Updates an object from repo list Input: -obj- object Output: True - if update succeded False - otherwise ''' if type(obj) == Grade: for i in range(len(self._objects)): if self._objects[i].assignID == obj.assignID and self._objects[i].studentID == obj.studentID: self._objects[i] = deepcopy(obj) return True else: pos = self.find(obj.id) if pos != -1 and type(obj) != Grade: self._objects[pos] = deepcopy(obj) return True raise RepoException("There is no object with such id in the database!")
def testRepoEx(self): x = RepoException("Test") self.assertEqual(x.messages, "Test") self.assertEqual(str(x), "Test") self.r.add(self.g) self.assertEqual(type(str(self.r)), str)