def player_turn(self, point): Validator.validate_point(Validator, point) if not self.__player.check_hit(point): point.value = self.__computer.get_point(point) self.__player.hit(point, self.__computer.attacked(point)) else: raise PlaneException("Error!!! Unavailable move!!!")
def setUp(self): self.validator = Validator() self.book = Book(12, '', '', '') self.book2 = Book(22, 'da', 'desc', 'aut') self.client = Client(13, '') self.client2 = Client(14, 'da')
def __read_point(self): read = input("Insert coordinates: ").split() if len(read) == 3: Validator.validate_types(read) if read[2] not in ["N", "S", "W", "E"]: raise PlaneException("Error!!! Invalid orientation!!!\n") return Point(read[0], read[1], read[2]) elif len(read) == 2: Validator.validate_types(read) return Point(read[0], int(read[1]), "Default") else: raise PlaneException("Error!!! Please insert a valid point!!!\n")
def __init__(self, filename): """ Initializing the controller :param filename: """ self.__repo = Repository( filename) # initialize the repository with the filename self.__validator = Validator( ) # initialize the validator for checking if the input is ok self.__scramble = self.__repo.get_scramble( ) # getting the randomly picked sentence from repo self.__undo = None
class TestValidare(unittest.TestCase): def setUp(self): self.validator = Validator() self.book = Book(12, '', '', '') self.book2 = Book(22, 'da', 'desc', 'aut') self.client = Client(13, '') self.client2 = Client(14, 'da') def test_validare(self): self.assertRaises(ValidateException, self.validator.validateBook, self.book) self.assertRaises(ValidateException, self.validator.validateClient, self.client) self.assertTrue(self.validator.validateClient(self.client2)) self.assertTrue(self.validator.validateBook(self.book2))
def __init__(self, bookRepo, clientRepo, rentalRepo, stats): self.__bookRepo = bookRepo self.__clientRepo = clientRepo self.__rentalRepo = rentalRepo self.__validator = Validator() # self.__stats = Statistics(bookRepo) self.__stats = stats self.__date = datetime.now()
def testValidator(self): validator = Validator() msg = validator.validateNewBook(1, "Title", "Desc", "Author") self.assertTrue(len(msg) == 0) msg = validator.validateNewBook(-1, "", "", "") self.assertTrue( msg == "Invalid ID, must be an integer \nThe title cannot be empty \nThe description cannot be empty \n" \ "The author name cannot be empty \n") msg = validator.validateNewClient(1, "Name") self.assertTrue(len(msg) == 0) msg = validator.validateNewClient(-1, "") self.assertTrue(msg == "Invalid ID, must be an integer \nName cannot be empty")
def __init__(self, controller1, controller2, controller3, controller4): self.__Bcontroller = controller1 self.__Ccontroller = controller2 self.__Rcontroller = controller3 self.__Ucontroller = controller4 self.validator = Validator()
class Controller: def __init__(self, filename): """ Initializing the controller :param filename: """ self.__repo = Repository( filename) # initialize the repository with the filename self.__validator = Validator( ) # initialize the validator for checking if the input is ok self.__scramble = self.__repo.get_scramble( ) # getting the randomly picked sentence from repo self.__undo = None def dead(self): """ Check if it is either a win, lose or we can't decide yet :return: """ if self.__scramble.score == 0: # if the score is 0 it's a lose return "You Lose!" elif self.__scramble.sentance == self.__scramble.scramble: # if the scramble sentence is solved it's a win return "You Win!" else: return True # otherwise we cant decide yet def swap(self, word_1, letter_1, word_2, letter_2): """ Swapping the letters :param word_1: :param letter_1: :param word_2: :param letter_2: :return: """ self.__validator.validate_swap( word_1, letter_1, word_2, letter_2, self.__scramble.id) # check if the input is correct self.__scramble.swap( word_1, letter_1, word_2, letter_2 ) # swap the elements using the swap function from the Sentence class self.__scramble.dec_score(1) # decrease the score by one self.__undo = [word_1, letter_1, word_2, letter_2] # add the move to undo def undo(self): """ Undo the last operation :return: """ if self.__undo is None: # if we can not undo anymore we raise an error raise ControllerException("Error!!! Can't undo anymore!!!\n") else: # otherwise we simply do the swap from the undo list once more self.__scramble.swap(self.__undo[0], self.__undo[1], self.__undo[2], self.__undo[3]) # self.__scramble.inc() self.__undo = None # undo becomes None because we don't want the user to do multiple undo operations def list_scramble(self): """ Returns the str of the self.__scramble object for printing :return: """ return str(self.__scramble) def _get_scramble(self): """ This functions only purpose is testing for the controller :return: """ return self.__scramble
def __init__(self, clientRepo, stats): self.__clientRepo = clientRepo self.__validator = Validator() self.__stats = stats
class BookController: def __init__(self, bookRepo, stats): self.__bookRepo = bookRepo self.__validator = Validator() self.__stats = stats def addBook(self, id, title, description, author): """ :param id: int :param title: string :param description: string :param author: string :return: a string of errors if any """ errorString = "" errorString += self.__validator.validateNewBook( id, title, description, author) if len(errorString) != 0: return errorString book = Book(id, title, description, author) try: self.__bookRepo.add(book) except RepositoryException as re: return str(re) return "" def removeBook(self, id): """ :param id: int :return: a string of errors if any """ id = int(id) if self.__bookRepo.existsById(id): self.__bookRepo.removeById(id) return "" else: return "The provided ID does not exist" def getBooks(self): """ :return: a list of all books """ return self.__bookRepo.getAll() def updateBook(self, id, newTitle, newDesc, newAuthor): """ :param id: int :param newTitle: string :param newDesc: string :param newAuthor: string :return: None """ try: self.__bookRepo.removeById(id) updatedBook = Book(id, newTitle, newDesc, newAuthor) self.__bookRepo.add(updatedBook) except RepositoryException as re: pass def populateBookRepository(self): """ Populate the repo with some random entities :return: None """ for i in range(100): id = random.randint(1, 100) title = "Title" + str(random.randint(1, 100)) desc = "Description" + str(random.randint(1, 100)) author = "Author" + str(random.randint(1, 100)) if not self.__bookRepo.existsById(id): book = Book(id, title, desc, author) self.addBook(id, title, desc, author) def searchBookById(self, id): """ :param id: int :return: The book with the id id or a empty list if it doesn't exist """ filteredBooksList = [] for book in self.getBooks(): if book.getId() == id: filteredBooksList.append(book) return filteredBooksList def searchBookByTitle(self, title): """ :param title: string :return: All books which contain title in their title, case insensitive """ filteredBooksList = [] for book in self.getBooks(): if title.lower() in book.getTitle().lower(): filteredBooksList.append(book) return filteredBooksList def searchBookByDescription(self, desc): """ :param desc: string :return: All books which contain desc in their description, case insensitive """ filteredBooksList = [] for book in self.getBooks(): if desc.lower() in book.getDescription().lower(): filteredBooksList.append(book) return filteredBooksList def searchBookByAuthor(self, author): """ :param author: string :return: All books which contain author in their author, case insensitive """ filteredBooksList = [] for book in self.getBooks(): if author.lower() in book.getAuthor().lower(): filteredBooksList.append(book) return filteredBooksList
class ClientController: def __init__(self, repo, undoController, rentalRepo): self.__clientRepo = repo self.validator = Validator() self._undoController = undoController self.rentalRepo = rentalRepo def addClient(self, client): """ :param client: :return: """ self.validator.validateClient(client) self.__clientRepo.add(client) # IF there are no errors we continue to append data for undo/repo redo = FunctionCall(self.addClient, client) undo = FunctionCall(self.removeClient, client.getID()) operation = Operation(redo, undo) self._undoController.recordOperation(operation) def removeClient(self, ID): """ :param ID: :return: """ # IF there are no errors we continue to append data for undo/repo redo = FunctionCall(self.removeClient, ID) undo = FunctionCall(self.addClient, self.__clientRepo.get(ID)) operation = Operation(redo, undo) self.__clientRepo.remove(ID) for rental in self.rentalRepo.getAll(): if rental.getClientID() == ID: self.rentalRepo.remove(rental.getID()) self._undoController.recordOperation(operation) def updateClient(self, aclient): """ :param aclient: :return: """ redo = FunctionCall(self.updateClient, aclient) undo = FunctionCall(self.updateClient, self.__clientRepo.get(aclient.getID())) operation = Operation(redo, undo) self._undoController.recordOperation(operation) self.validator.validateClient(aclient) self.__clientRepo.update(aclient) def getAllClients(self): return self.__clientRepo.getAll() def searchClient(self, substring): """ :return: """ if substring == '': raise ControllerException("no keywords given") result = [] for x in self.__clientRepo.getAll(): if substring in str(x.getID()).lower() or substring in str( x.getName()).lower(): result.append(x) if not result: raise ControllerException("No entries match your search") else: return result
def __init__(self, player_planes_board, player_hits_board): self.__planes = player_planes_board self.__hits = player_hits_board self.__validator = Validator() self.__planes_alive = 0
class PlayerController(Utils): def __init__(self, player_planes_board, player_hits_board): self.__planes = player_planes_board self.__hits = player_hits_board self.__validator = Validator() self.__planes_alive = 0 @property def get_planes(self): return self.__planes def new_plane(self, head): self.__validator.validate_plane(head) plane = Plane(head) self.__validator.overlap_plane(self.__planes, plane.coordinates()) self.__planes.add(plane.coordinates()) self.__planes_alive += 1 def get_point(self, point): return self.__planes[point.x][point.y] def attacked(self, point): self.__validator.validate_plane(point) if self.__planes[point.x][point.y] == "#": self.__planes[point.x][point.y] = "!" return "X" elif self.__planes[point.x][point.y] in ["S", "N", "E", "W"]: self.__planes = self.__fill(point, "!", self.__planes) self.__planes_alive -= 1 return "K" return "O" def hit(self, point, result): if result == "K": self.__hits = self.__fill(point, "X", self.__hits) else: self.__hits[point.x][point.y] = result def check_hit(self, point): if self.__hits[point.x][point.y] != " ": return True return False @property def planes_alive(self): return self.__planes_alive def __str__(self): return "Alive Planes: " + str( self.__planes_alive) + "\nPlanes:\n" + str( self.__planes) + "\nMoves:\n" + str(self.__hits) def __fill(self, param, value, board): if param.value == "N": return super().fill_N(param.x, param.y, value, board) if param.value == "S": return super().fill_S(param.x, param.y, value, board) if param.value == "W": return super().fill_W(param.x, param.y, value, board) if param.value == "E": return super().fill_E(param.x, param.y, value, board)
clientRepo = Repository() rentalRepo = Repository() elif mode == "text": bookRepo = CSVRepository(path + "bookRepo.csv", Book) clientRepo = CSVRepository(path + "clientRepo.csv", Client) rentalRepo = CSVRepository(path + "rentalRepo.csv", Rental) elif mode == "binary": bookRepo = PickleRepository(path + "binaryBookRepo.pickle") clientRepo = PickleRepository(path + "binaryClientRepo.pickle") rentalRepo = PickleRepository(path + "binaryRentalRepo.pickle") elif mode == "custom": bookRepo = CustomRepository(Book) clientRepo = CustomRepository(Client) rentalRepo = CustomRepository(Rental) else: raise ValueError("Invalid option") validator = Validator() stats = Statistics(bookRepo) bookController = BookController(bookRepo, stats) bookController.populateBookRepository() clientController = ClientController(clientRepo, stats) clientController.populateClientRepository() rentalController = RentalController(bookRepo, clientRepo, rentalRepo, stats) #rentalController.populateRentals() ui = Ui(bookController, clientController, rentalController, validator, stats) ui.runUi()
class BookController: def __init__(self, repo, undoController, rentalRepo): self.__bookRepo = repo self.validator = Validator() self.__rentalrepo = rentalRepo self._undoController = undoController def __str__(self): """ :return: formatted string of the repo """ result = '' for x in self.__bookRepo.getAll(): result += str(x) + '\n' return result def addBook(self, book): """ function that adds book to repo input: book of type Book """ self.validator.validateBook(book) self.__bookRepo.add(book) # IF there are no errors we continue to append data for undo/repo redo = FunctionCall(self.addBook, book) undo = FunctionCall(self.removeBook, book.getID()) operation = Operation(redo, undo) self._undoController.recordOperation(operation) def removeBook(self, ID): """ handles removal of element from repository :param ID: ID for book to be removed :return: """ redo = FunctionCall(self.removeBook, ID) undo = FunctionCall(self.addBook, self.__bookRepo.get(ID)) operation = Operation(redo, undo) self.__bookRepo.remove(ID) for rental in self.__rentalrepo.getAll(): if rental.getBookID() == ID: self.__rentalrepo.remove(rental.getID()) self._undoController.recordOperation(operation) def updateBook(self, abook): """ :param abook: :return: """ redo = FunctionCall(self.updateBook, abook) undo = FunctionCall(self.updateBook, self.__bookRepo.get(abook.getID())) operation = Operation(redo, undo) self._undoController.recordOperation(operation) self.validator.validateBook(abook) self.__bookRepo.update(abook) def getBook(self, idx): """ return the book having the ID idx :param idx: id of book retrieved :return: book having that ID """ return self.__bookRepo.get(idx) def getAllBooks(self): """ return the list of all books :return: """ return self.__bookRepo.getAll() def searchBook(self, substring): """ function that searches substring in book repository :param substring: keywords :return: list of books which contain searched keywords """ if substring == '': raise ControllerException("no keywords were given") result = [] for x in self.__bookRepo.getAll(): if substring in str(x.getID()).lower() or substring in str( x.getAuthor()).lower() or substring in str( x.getTitle()).lower() or substring in str( x.getDescription()).lower() or substring in str( x.get_available).lower(): result.append(x) if not result: raise ControllerException("No entries match your search") else: return result
def __init__(self, repo, undoController, rentalRepo): self.__bookRepo = repo self.validator = Validator() self.__rentalrepo = rentalRepo self._undoController = undoController
def __init__(self, repo, undoController, rentalRepo): self.__clientRepo = repo self.validator = Validator() self._undoController = undoController self.rentalRepo = rentalRepo
def __init__(self, bookRepo, stats): self.__bookRepo = bookRepo self.__validator = Validator() self.__stats = stats
class ClientController: def __init__(self, clientRepo, stats): self.__clientRepo = clientRepo self.__validator = Validator() self.__stats = stats def addClient(self, id, name): """ Adds a client to the list of clients :param id: int :param name: string :return: a string of errors if any """ errorString = self.__validator.validateNewClient(id, name) if len(errorString) != 0: return errorString client = Client(id, name) try: self.__clientRepo.add(client) except RepositoryException as re: return str(re) return "" def removeClient(self, id): """ Remove a client from the list :param id: int :return: a string of errors if any """ id = int(id) if self.__clientRepo.existsById(id): self.__clientRepo.removeById(id) return "" else: return "The provided ID does not exist" def updateClient(self, id, newName): """ Update a client's name :param id: :param newName: :return: """ try: self.__clientRepo.removeById(id) updatedClient = Client(id, newName) self.__clientRepo.add(updatedClient) except RepositoryException as re: pass def getClients(self): """ :return: a list of all clients """ return self.__clientRepo.getAll() def populateClientRepository(self): """ Populate the repo with some random values :return: None """ for i in range(100): id = random.randint(1, 100) name = "Name" + str(random.randint(1, 100)) if not self.__clientRepo.existsById(id): self.addClient(id, name) def searchClientById(self, id): """ Return the cliet with the Id id :param id: int :return: list """ filteredClientsList = [] for client in self.getClients(): if client.getId() == id: filteredClientsList.append(client) return filteredClientsList def searchClientByName(self, name): """ Returns a list of clients whose name contains name, case insensitive :param name: string :return: list """ filteredClientsList = [] for client in self.getClients(): if name in client.getName().lower(): filteredClientsList.append(client) return filteredClientsList