def __testAdd(self): ''' Test the 'add' function ''' numberList = [] historyStack = [] self.add(numberList, historyStack, ComplexNumber(5, 6)) self.add(numberList, historyStack, ComplexNumber(10, 2)) self.add(numberList, historyStack, ComplexNumber(5.4, 6.7)) self.add(numberList, historyStack, ComplexNumber(101, 21)) self.add(numberList, historyStack, ComplexNumber(-5.41, -6.71)) assert len(numberList) == 5 assert numberList[0] == ComplexNumber(5, 6) assert numberList[1] == ComplexNumber(10, 2) assert numberList[2] == ComplexNumber(5.4, 6.7) assert numberList[3] == ComplexNumber(101, 21) assert numberList[4] == ComplexNumber(-5.41, -6.71)
def __addUI(self): ''' Interface for the "add" function @param: - None @return: - None ''' com = self.__readComplexNumber() if com != ComplexNumber(None, None): addObj = Services() addObj.add(self.numberList, self.historyStack, com)
def generateList(n): ''' Randomly generates a list of 'n' complex numbers @param: - n = integer @return: - numberList = randomly generated list of complex numbers ''' import random from Complex import ComplexNumber numberList = [] for i in range(n): numberList.append( ComplexNumber(random.randint(0, 100), random.randint(0, 100))) return numberList
def __readComplexNumber(self): ''' Reads a ComplexNumber from the console @param: - None @return: - ComplexNumber, if the input was valid - None, otherwise ''' try: real, imag = input( "Please insert real and imaginary part: ").split() real = self.__validateInput(real) imag = self.__validateInput(imag) except: print("Invalid complex number") return None return ComplexNumber(real, imag)
def getInput(self): ''' ''' x = input("Please insert the commands, separated by a comma: \n") i = 0 l = len(x) ServiceObj = Services() while i < l: if x[i] == 'a': command = self.__getAdd(x[i:]) if command: real = int(command[1]) imag = int(command[2]) ServiceObj.add(self.numberList, self.historyStack, ComplexNumber(real, imag)) i += 8 elif x[i] == 'l': command = self.__getList(x[i:]) if command: self.__printComplexNumberList() i += 4 elif x[i] == 'f': command = self.__getFilter(x[i:]) if command: startPos = int(command[1]) endPos = int(command[2]) if startPos < endPos: ServiceObj.filter(self.numberList, self.historyStack, startPos, endPos) i += 11 elif x[i] == 'u': command = self.__getUndo(x[i:]) if command: ServiceObj.undo(self.numberList, self.historyStack) i += 4 elif x[i] == 'x': return -1 i += 1
def __testUndo(self): ''' Test the 'undo function ''' numberList = [] historyStack = [] self.add(numberList, historyStack, ComplexNumber(5, 6)) self.add(numberList, historyStack, ComplexNumber(10, 2)) self.undo(numberList, historyStack) assert len(numberList) == 1 assert numberList[0] == ComplexNumber(5, 6) self.add(numberList, historyStack, ComplexNumber(5.4, 6.7)) self.add(numberList, historyStack, ComplexNumber(101, 21)) self.add(numberList, historyStack, ComplexNumber(-5.41, -6.71)) self.undo(numberList, historyStack) assert len(numberList) == 3 assert numberList[0] == ComplexNumber(5, 6) assert numberList[1] == ComplexNumber(5.4, 6.7) assert numberList[2] == ComplexNumber(101, 21) self.add(numberList, historyStack, ComplexNumber(-5.41, -6.71)) self.filter(numberList, historyStack, 0, 2) self.undo(numberList, historyStack) assert len(numberList) == 4 assert numberList[0] == ComplexNumber(5, 6) assert numberList[1] == ComplexNumber(5.4, 6.7) assert numberList[2] == ComplexNumber(101, 21) assert numberList[3] == ComplexNumber(-5.41, -6.71) self.filter(numberList, historyStack, 0, 1) self.undo(numberList, historyStack) assert len(numberList) == 4 assert numberList[0] == ComplexNumber(5, 6) assert numberList[1] == ComplexNumber(5.4, 6.7) assert numberList[2] == ComplexNumber(101, 21) assert numberList[3] == ComplexNumber(-5.41, -6.71)
def __testFilter(self): ''' Test the 'filter' function ''' numberList = [] historyStack = [] self.add(numberList, historyStack, ComplexNumber(5, 6)) self.add(numberList, historyStack, ComplexNumber(10, 2)) self.add(numberList, historyStack, ComplexNumber(5.4, 6.7)) self.add(numberList, historyStack, ComplexNumber(101, 21)) self.add(numberList, historyStack, ComplexNumber(-5.41, -6.71)) self.filter(numberList, historyStack, 0, 1) assert len(numberList) == 2 assert numberList[0] == ComplexNumber(5, 6) assert numberList[1] == ComplexNumber(10, 2) numberList = [] historyStack = [] self.add(numberList, historyStack, ComplexNumber(5, 6)) self.add(numberList, historyStack, ComplexNumber(10, 2)) self.add(numberList, historyStack, ComplexNumber(5.4, 6.7)) self.add(numberList, historyStack, ComplexNumber(101, 21)) self.add(numberList, historyStack, ComplexNumber(-5.41, -6.71)) self.filter(numberList, historyStack, 1, 3) assert len(numberList) == 3 assert numberList[0] == ComplexNumber(10, 2) assert numberList[1] == ComplexNumber(5.4, 6.7) assert numberList[2] == ComplexNumber(101, 21)