def _get_answer_from_user(self): """Get the answer from the user. :returns: The string with the answer from the user. """ return safe_ask.safe_ask(qprompt.ask_str, "Answer")
def run_command(self, menu_context): """Run the command that will delete a word from the dictionary. :menu_context: The context of the menu to add the dictionary to. """ if menu_context.get("dictionary", None) is None: print("Menu does not have a dictionary") qprompt.pause() return dictionary = menu_context["dictionary"] word_index_to_remove = safe_ask.safe_ask( qprompt.ask_int, "Insert the index of the word to remove") word_to_remove = dictionary.get_word_by_index(word_index_to_remove) if qprompt.ask_yesno( f"Do you want to remove the word \"{word_to_remove}?\"", dft=False): dictionary.delete_word(word_index_to_remove) print("Word removed") else: print("Word not removed") qprompt.pause()
def run_command(self, menu_context): """Run the command that will add the dictionary to the menu. :menu_context: The context of the menu to add the dictionary to. :Note: This functions adds the dictionary to the menu context. """ if menu_context.get("dictionary", None): print("Menu already has a dictionary.") qprompt.pause() return native_language = safe_ask.safe_ask( qprompt.ask_str, "Insert the native language of the dictionary") learned_language = safe_ask.safe_ask( qprompt.ask_str, "Insert the learned language of the dictionary") new_dictionary = dictionary.Dictionary(native_language, learned_language) menu_context["dictionary"] = new_dictionary
def add_new_word(dictionary): """Add a new word to the dictionary. :dictionary: The dictionary to add the word to. :returns: True if the user wanted to add new word, False otherwise. :note: The return value indicate only the user's intention. In case the word was not added (for example, if it already was in the dictionary), the function will still return True. :note: The function get all the word's parameters from the user. """ print("Insert the words as asked for. To cancel, put -1 as any field.") native_word = safe_ask.safe_ask(qprompt.ask_str, "Insert the native word") learned_word = safe_ask.safe_ask(qprompt.ask_str, "Insert the learned word") known_level = safe_ask.safe_ask( qprompt.ask_int, "Insert the known level", 0, vld=list(range(-1, word_couple.WordCouple.MAX_LEVEL + 1))) # In case the one of the parameters was set to -1 it also means that # the user didn't want to add the word, simply exit the function. if known_level == -1 or native_word == "-1" or learned_word == "-1": print("Not adding any new word.") return False new_word = word_couple.WordCouple(native_word, learned_word, known_level) try: dictionary.add_word(new_word) except KeyError as dictionary_error: print(dictionary_error) return True
def run_command(self, menu_context): """Print all the translation of a given word. :menu_context: The context of the menu. """ if menu_context.get("dictionary", None) is None: print("Can't translate without dictionary.") qprompt.pause() return word_to_translate = safe_ask.safe_ask(qprompt.ask_str, "Insert the word to translate") dictionary = menu_context["dictionary"] print(dictionary.get_all_translations_to_native([word_to_translate])) qprompt.pause()
def run_command(self, menu_context): """Extend the dictionary in the context with the new loaded one. :menu_context: The context of the menu. """ if menu_context.get("dictionary", None) is None: print("Can't extend dictionary when there isn't one.") qprompt.pause() return file_name = safe_ask.safe_ask(qprompt.ask_str, "Insert the new name of the dictionary") dictionary = menu_context["dictionary"] load_action = load_dictionary.LoadDictionaryAction.load_dictionary_file new_dictionary = load_action(file_name) dictionary.extend(new_dictionary)
def run_command(self, menu_context): """Run the command that will save the dictionary to the file. :menu_context: The context of the menu. """ if menu_context.get("dictionary", None) is None: print("Menu doesn't have a dictionary.") qprompt.pause() return dictionary_name = menu_context.get("dictionary_name", None) file_name = safe_ask.safe_ask( qprompt.ask_str, "Insert the file name to save the dictionary into", dictionary_name) with open(file_name, "wb") as dictionary_file: pickle.dump(menu_context["dictionary"], dictionary_file)
def run_command(self, menu_context): """Run the command that will load the dictionary from the file. :menu_context: The context of the menu to add the dictionary to. :Note: This functions adds the dictionary to the context of the menu. """ if menu_context.get("dictionary", None): print("Menu already has a dictionary.") qprompt.pause() return file_name = safe_ask.safe_ask( qprompt.ask_str, "Insert the file name of the dictionary") menu_context["dictionary"] = self.load_dictionary_file(file_name) menu_context["dictionary_name"] = file_name return
def run_command(self, menu_context): """Print all the translation of a given word. :menu_context: The context of the menu. :*args: The special arguments for the command. It will include the word to translate. """ if menu_context.get("dictionary", None) is None: print("Can't translate without dictionary.") qprompt.pause() return word_to_translate = safe_ask.safe_ask( qprompt.ask_str, "Insert the word to translate") dictionary = menu_context["dictionary"] print(dictionary.get_all_translations_to_learned( [word_to_translate])) qprompt.pause()
def run_command(self, menu_context): """Run the command that will add the new word to the dictionary. :menu_context: The context of the menu. """ dictionary = menu_context.get("dictionary", None) if dictionary is None: print("Menu does not have a dictionary") qprompt.pause() return number_of_questions = safe_ask.safe_ask( qprompt.ask_int, "Insert the number of questions per known level", test.Test.DEFAULT_NUMBER_OF_QUESTIONS, vld=lambda x: x > 0) current_test = test.Test(dictionary, number_of_questions) current_test.run() qprompt.pause()
def run_command(self, menu_context): """Run the command that will add load the file to the dict. :menu_context: The context of the menu to add the dictionary to. :Note: This functions adds the new words to the dictionary. """ # Get the dictionary (and validate it exists). dictionary = menu_context.get("dictionary", None) if dictionary is None: print("Menu does not have a dictionary") qprompt.pause() return # Get the name of the yaml file. file_with_words = safe_ask.safe_ask( qprompt.ask_str, "Insert the name of the file with the words") # Add the new word. self.add_new_words(dictionary, file_with_words) # Make sure the user sees the output before ending the action. qprompt.pause()