def pb_upd(): name_to_update = pb_view.get_data_from_user("upd.name?") if pb_crud.search_name_in_phone_book(name_to_update): tel = pb_view.get_data_from_user("upd.tel?") pb_crud.update_phone_book(name_to_update, tel) else: pb_view.message_to_user("Failed to update %s" % name_to_update)
def pb_del(): name_to_delete = pb_view.get_data_from_user("del.name?") try: pb_crud.del_entry_from_phone_book(name_to_delete) pb_view.message_to_user("%s successfully deleted" % name_to_delete) except ValueError as e: pb_view.message_to_user(e)
def search_name_in_phone_book(name_to_search): if name_to_search in phone_book: pb_view.message_to_user("%s: %s" % (name_to_search, phone_book[name_to_search])) return True else: pb_view.message_to_user("no %s in PhoneBook" % name_to_search) return False
def pb_add(): name = pb_view.get_data_from_user("add.name?") tel = pb_view.get_data_from_user("add.tel?") try: pb_crud.add_entry_to_phone_book(name, tel) pb_view.message_to_user("%s: %s added successfully." % (name, tel)) except ValueError as e: pb_view.message_to_user(e)
def start_phone_book(): try: # phonebook = pb_data.get_data_from_dbfile(PHONE_BOOK_DB_FILE) phonebook = pb_data.get_data_from_csvfile(PHONE_BOOK_DB_FILE) except (EOFError, IOError): pb_view.message_to_user( "Error loading data from file. Starting new phone_book.") phonebook = {} return phonebook
def list_phone_book(): if not phone_book: pb_view.message_to_user("PhoneBook is empty") else: for name, number in phone_book.iteritems(): pb_view.message_to_user("%s: %s" % (name, number))
def pb_default(): pb_view.message_to_user("Bad command. Please input correct one!")
def pb_exit(): pb_view.message_to_user("Exit.") exit()
def pb_help(): pb_view.message_to_user(HLP_MSG)
pb_crud.search_name_in_phone_book(name_to_search) def pb_help(): pb_view.message_to_user(HLP_MSG) def pb_exit(): pb_view.message_to_user("Exit.") exit() def pb_default(): pb_view.message_to_user("Bad command. Please input correct one!") pb_view.message_to_user("Welcome to PhoneBook.") pb_view.message_to_user(HLP_MSG) commands = { 'add': pb_add, 'del': pb_del, 'upd': pb_upd, 'lst': pb_lst, 'exit': pb_exit, 'srch': pb_srch, 'help': pb_help } while True: commands.get(pb_view.get_data_from_user("?"), pb_default)()