def save_json_list(self): if is_empty(self.person_list): error_msg("Can't save file! List is empty!") press_any_key() return print(f"Saveing list to file.[{self.jsonfile.filename}]") self.jsonfile.save_to_json(Person.person_to_dict_list( self.person_list)) press_any_key()
def chose_person_remove_menu(self, plist): print("\nChose the index of the person you wish to remove\n") Menu.print_list(plist, True) print(str(len(plist)) + ". Go back") while True: index = input_int(self.type_in_choice, "Invalid Input! Try again!") if index == len(plist): return if index < 0 or index > (len(plist)): error_msg("Invalid Input! Try again!") continue break print( f"Removeing {plist[index].firstname} {plist[index].lastname} . . ." ) self.person_list.remove(plist[index]) press_any_key()
def remove_person_menu(self): while True: print("Remove person menu") self.print_menu_text(self.remove_menu_text) while True: choice = input_int(self.type_in_choice, self.invalid_choice) if choice < 1 or choice > 3: error_msg(self.invalid_choice) continue break if choice == 3: break elif choice == 1: self.find_and_remove_person() break elif choice == 2: self.chose_person_remove_menu(self.person_list) break
def find_person(self): while True: print("Search menu. Search for:") self.print_menu_text(self.find_menu_text) choice = input_int(self.type_in_choice, self.invalid_choice) if choice > 8 and choice < 1: error_msg("Invalid option! Try again!") continue if choice == 8: print("Going Back") break option = Person.get_keylist() option.append("text") option = option[choice - 1].title() search_string = input( f"Type in {option} your are searching for:").lower() found_list = [] for person in self.person_list: if self.check_found(person, search_string, choice): found_list.append(person) return found_list
def remove_sub_menu(self, plist): self.print_menu_text(self.remove_sub_menu_text) while True: choice = input_int(self.type_in_choice, self.invalid_choice) if choice == 3: return if choice < 1 or choice > 3: error_msg(self.invalid_choice) continue break if choice == 1: self.chose_person_remove_menu(plist) if choice == 2: print("These are the matches:") Menu.print_list(plist) if prompt_yes_or_no( "Do you wish to remove all persons that matched?"): print("Removeing") for person in plist: self.person_list.remove(person) else: print("Going back.")
def start(self): while True: print("\nLaboration 2 - GMI2BT - Nils Broberg") self.print_menu_text(self.main_menu_text) choice = input_int(self.type_in_choice, self.invalid_choice) if choice == 8: break elif choice == 1: self.read_from_file() elif choice == 2: self.showing_json_data() elif choice == 3: self.add_person() elif choice == 4: self.remove_person_menu() elif choice == 5: self.find_person_menu() elif choice == 6: self.save_json_list() elif choice == 7: self.load_json_list() else: error_msg(self.invalid_choice)
def open(self, mode="r"): try: self.fp = open(self.get_fullpath(), mode, encoding=self.encodeing) return True except FileNotFoundError: error_msg(f"File [{self.get_fullpath}] was not found!") except FileExistsError: error_msg(f"File [{self.get_fullpath}] did not exist!") except IOError: error_msg(f"File [{self.get_fullpath}] could be opened!") self.fp = None return False