def test_read_test_file(): list_strings = read_text_file('small.txt') #checks if the 2nd element of the list is valid assert list_strings[2] == 'another word.', "Should be True but is " + str(list_strings[0]) # checks if the 1st element of the list is valid assert list_strings[0]== 'Yossarian decided', "Should be True but is " + str(list_strings[-1])
def read_filename(self, name): self.text_lines = read_text_file(name) print(self.text_lines.the_array) print(self.text_lines.size)
def prompt_user(self): """ Function calls print_menu. Function asks user for selected inputs inputs. Function quits when thr user types "quit" @:param self: the object @Pre Condition: User has to type a command, Exception raised if incorrect command typed by user @Post Condition: Calls the appropriate function, and updates the list @complexity: Best case O(1), Worst case O(n) """ end = False while not end: # while end = False the outermost loop runs try: printMenu = False if printMenu == False: self.print_menu() # Call print menu # Prompt user to type command user_input = input("Enter Command: (eg: read small.txt ) : \n" ).lower().split(" ") if len(user_input) == 2 and user_input[ 0] == "read": # if length of user_input == 2 and first self.text_lines = read_text_file( user_input[1] ) # element is "read" call read_text_files() # method and store output in self.text_lines # if length of user_input <= 2 and first element is "print" then: elif len(user_input) <= 2 and user_input[0] == "print": if len(user_input) == 1: # if length of user_input == 1 rest_string = "" self.print_num(rest_string) # initiate print all lines else: self.print_num( (user_input[1]) ) # else print item at line num = second user input element # if length of user_input <= 2 and first element is "delete" then: elif len(user_input) <= 2 and user_input[0] == "delete": if len(user_input) == 1: # if length of user_input == 1 rest_string = "" self.delete_num( rest_string) # initiate delete all lines else: self.delete_num( user_input[1] ) # Else delete item at line num = second element of user input # if length of user_input == 2 and first element is "insert" then: elif len(user_input) == 2 and user_input[0] == "insert": try: # running is_number() on second element of user input to check if its a number if self.is_number(user_input[1]) == True: number = int( user_input[1] ) # storing line number as type integer if number == 0: # if line number = 0 raise IndexError( 'Invalid line number' ) # index error raised if line number = 0 else: # Creating new ListADT instance to store the content user wants to add list_strings = ListADT() end_insert = False # setting end_insert condition to false print("Enter text: ") # while end inster = False run while loop to prompt user to type lines to enter while not end_insert: prompt_user = input(" - ").strip() # if full stop typed by user, set end_insert to true and stop prompting user to type lines if prompt_user == ".": end_insert = True else: list_strings.append(prompt_user) self.insert_num_strings( user_input[1], list_strings ) # List_strings inserted to self else: raise IndexError( 'Enter valid Line number' ) # IndexError raised if index is not a number. except Exception as e: # All Exceptions in Try block are caught and printed. print("??") print(e) elif len(user_input) == 2 and user_input[0] == "search": print(self.search_string(user_input[1])) elif len(user_input) == 1 and (user_input[0] == "quit"): # Sets 'end' to true when user types Quit, which stops the outermost while loop from running end = True else: print("You must enter one of the listed commands") print("Try again ") self.prompt_user( ) # If invalid input is entered, allows user to try again. except Exception as e: # All Exceptions in outer most Try block are caught and printed. end = True print(e) print("??") self.prompt_user()
def test_Task5_search(): x = Editor() x.text_lines = read_text_file('small.txt') x.search_string('not')