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
def testSort(self): list = MyList() for i in range(4, -1, -1): list.append(i) list.sort() sortList = MyList() for i in range(0, 5): sortList.append(i) self.assertEqual(list, sortList)
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 lateRentals(self): res = MyList() rentals = self._rentalRepo.getAll() today = datetime.datetime.now().date() for i in rentals: if i.getDueDate() < today and i.getReturnedDate() == "": bookId = i.getBookId() numberOfDays = today - i.getDueDate() title = self.getBookTitleByBookId(bookId) res.append(LateRental(title, numberOfDays.days)) res.sort(key=lambda x: x.getNumberOfDays(), reverse=True) return res
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
def mostRentedBooks(self): """ :return: the list of most rented books bookCounter[i] = i-book id, the number of rentals of i, i-title """ bookCounter = MyList() books = self._bookRepo.getAll() rentals = self._rentalRepo.getAll() for i in books: bookCounter.append(BookRental(i.getId(), i.getTitle(), 0)) for i in rentals: for j in bookCounter: if i.getBookId() == j.getBookId(): j.incrementRentalCount() bookCounter.sort(key=lambda x: x.getRentalCount(), reverse=True) return bookCounter
def setUp(self): unittest.TestCase.setUp(self) self.rentalList = MyList() self.bookList = MyList() self.clientList = MyList() self.rentalList.append( Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.rentalList.append( Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.bookList.append(Book(0, "book0", "desc0", "author1")) self.bookList.append(Book(1, "book1", "desc1", "author0")) self.bookList.append(Book(2, "book2", "desc2", "author2")) self.clientList.append(Client(0, "name1")) self.clientList.append(Client(1, "name0")) self.clientList.append(Client(2, "name2"))
class SortingAlgorithmTest(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.rentalList = MyList() self.bookList = MyList() self.clientList = MyList() self.rentalList.append( Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.rentalList.append( Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.bookList.append(Book(0, "book0", "desc0", "author1")) self.bookList.append(Book(1, "book1", "desc1", "author0")) self.bookList.append(Book(2, "book2", "desc2", "author2")) self.clientList.append(Client(0, "name1")) self.clientList.append(Client(1, "name0")) self.clientList.append(Client(2, "name2")) def tearDown(self): unittest.TestCase.tearDown(self) def testSortObject(self): self.clientList.sort(key=lambda x: x.getName()) self.assertEqual(self.clientList[0], Client(1, "name0")) self.assertEqual(self.clientList[1], Client(0, "name1")) self.assertEqual(self.clientList[2], Client(2, "name2")) self.bookList.sort(key=lambda x: x.getTitle(), reverse=True) self.assertEqual(self.bookList[0], Book(2, "book2", "desc2", "author2")) self.assertEqual(self.bookList[1], Book(1, "book1", "desc1", "author0")) self.assertEqual(self.bookList[2], Book(0, "book0", "desc0", "author1")) def testFilter(self): res = self.clientList.filter(key=lambda x: x.getName(), value="name1") self.assertEqual(res[0], Client(0, "name1")) res1 = self.bookList.filter(key=lambda x: x.getId(), value=0) self.assertEqual(res1[0], Book(0, "book0", "desc0", "author1")) res2 = self.clientList.filter() self.assertEqual(res2, self.clientList) self.clientList.append(Client(3, "name0")) res3 = self.clientList.filter(key=lambda x: x.getName(), value="name0") self.assertEqual(res3[0], Client(1, "name0")) self.assertEqual(res3[1], Client(3, "name0"))
def __init__(self): self._data = MyList()