def mostActiveClients(self): """ :return: The statistic of the most active clients, in descending order of the number of days that they rented books """ res = MyList() rentals = self._rentalRepo.getAll() for i in rentals: numberOfDays = datetime.timedelta(0) rentalsByClient = self.getAllRentalsByClientId(i.getClientId()) for j in rentalsByClient: numberOfDays += j.getDueDate() - j.getRentedDate() res.append( ClientActivity(self.clientNameById(i.getClientId()), numberOfDays.days)) """ Eliminate the clients with the same name """ res.sort(key=lambda x: x.getClient()) for i in range(0, len(res) - 1): if res[i].getClient() == res[i + 1].getClient(): res[i].setClient("") i = 0 while i < len(res): while res[i].getClient() == "": res.remove(res[i]) else: i += 1 res.sort(key=lambda x: x.getNumberOfDays(), reverse=True) return res
class Repository: def __init__(self): self._data = MyList() def add(self, object): """ Add an object to the repository """ self._data.append(object) def remove(self, object): """ Removes an object from the repository """ #del self._data[id] #self._data.pop(id) self._data.remove(object) def update(self, id, object): """ Updates the book at the position id with a new book :param id: the given position :param object: the new object :return: - """ self._data[id].update(object) def __len__(self): """ :return: The size of the list """ return len(self._data) def getAll(self): """ :return: ALl the elements in the list """ return self._data[:] # deepcopy def get(self, index): if index < 0 or index >= len(self._data): raise RepositoryException("Invalid element position") return self._data[index]
def testList(self): list = MyList() self.assertEqual(len(list), 0) for i in range(0, 10): list.append(i) self.assertEqual(len(list), 10) for i in list: self.assertEqual(list[i], i) list.pop(7) self.assertEqual(len(list), 9) del list[4] self.assertEqual(len(list), 8) list.remove(1) self.assertEqual(len(list), 7) list[5] = "Andreea" self.assertEqual(list[5], "Andreea") list[2] = list[5] self.assertEqual(list[2], "Andreea")
def mostRentedAuthors(self): """ :return: the list of the most rented authors """ authorCounter = MyList() books = self._bookRepo.getAll() rentals = self._rentalRepo.getAll() for i in books: authorCounter.append(AuthorRental(i.getId(), i.getAuthor(), 0)) for i in rentals: for j in authorCounter: if i.getBookId() == j.getBookId(): j.incrementRentalCount() for i in range(0, len(authorCounter) - 1): for j in range(i + 1, len(authorCounter)): if authorCounter[i].getBookAuthor( ) == authorCounter[j].getBookAuthor(): authorCounter[i].addRentalCount( authorCounter[j].getRentalCount()) authorCounter[j] = AuthorRental(0, "", 0) i = 0 while i < len(authorCounter): if authorCounter[i].getBookAuthor() == "": authorCounter.remove(authorCounter[i]) else: i += 1 authorCounter.sort(key=lambda x: x.getRentalCount(), reverse=True) result = [] for i in authorCounter: result.append(i) return result