def print_languages(self):
     """
     Prints the supported languages to the screen
     """
     print(
         Colorful.color_text(
             "blue", "\nWelcome to the translator! Translator supports:\n"))
     for number, lang in self.languages.items():
         print(Colorful.color_text("blue", f"{number}. {lang}"))
    def get_output_result(self,
                          lang_number,
                          translations,
                          example_sentences,
                          output_number,
                          colorful=True):
        """
        Prepares the formatted output for the given translations
        :param lang_number: Translation language number
        :param translations: Translations
        :param example_sentences: Example sentences
        :param output_number: Number of translations and example sentences to be shown
        :param colorful: Indicates if the output will be colorful
        """
        language = self.languages[lang_number]
        result = f"{language} translation cannot be found\n"
        if colorful:
            result = self.get_error_output(result)

        if translations:
            translations_count = min(output_number, len(translations))
            shown_translations = translations[0:translations_count]

            sentence_count = min(output_number * 2, len(example_sentences))
            shown_sentences = example_sentences[0:sentence_count]

            result = f"{language} Translations:\n"
            if colorful:
                result = Colorful.color_text("magenta", result)
            result += "\n".join(shown_translations) + "\n"
            if colorful:
                result += Colorful.color_text("magenta",
                                              f"\n{language} Examples:\n")
            else:
                result += f"\n{language} Examples:\n"
            for i in range(0, len(shown_sentences), 2):
                result += f"{shown_sentences[i]}\n"
                result += f"{shown_sentences[i + 1]}\n\n"

        return result
    def translate_to_all(self, from_lang_number, word):
        """
        Translates the word to all languages in the list and
        prints the output to the screen and to the file
        :param from_lang_number: Original language
        :param word: Word to be translated
        """
        all_translations = ""
        for lang_number in self.languages:
            if from_lang_number != lang_number:
                response = self.get_translation_response(
                    from_lang_number, lang_number, word)
                if response.error_code != 0:
                    translation_output = response.error_message
                    print(self.get_error_output(response.error_message))
                    # for these error codes, no need to try other languages
                    if response.error_code in [1, 2]:
                        break
                else:
                    translation_output = self.get_output_result(
                        lang_number,
                        response.translations,
                        response.example_sentences,
                        output_number=1)
                    print(translation_output)  # prints the colorful text
                    # this output will be added to the file
                    translation_output = self.get_output_result(
                        lang_number,
                        response.translations,
                        response.example_sentences,
                        output_number=1,
                        colorful=False)

                # error message or translation output is appended to the result string
                all_translations += f"{translation_output}\n"
        if len(all_translations) > 0:
            file_name = f"{word}.txt"
            self.save_to_file(file_name, all_translations)
            print(
                Colorful.color_text("green",
                                    f"\nAll results saved to '{file_name}'\n"))
Пример #4
0
        if x in ("f", "false"):
            return False
        raise ValueError(x)
    elif isinstance(x, bool):
        return x
    if x == 0:
        return False
    if x == 1:
        return True
    raise TypeError(x)


success_symbol = " ✔ "
error_symbol = " ✖ "

cf = Colorful()
cf_palette = {
    "ghostWhite": "#F8F8F0",
    "lightGhostWhite": "#F8F8F2",
    "lightGray": "#CCCCCC",
    "gray": "#888888",
    "brownGray": "#49483E",
    "darkGray": "#282828",
    "yellow": "#E6DB74",
    "blue": "#66D9EF",
    "magenta": "#F92672",
    "purple": "#AE81FF",
    "brown": "#75715E",
    "orange": "#FD971F",
    "lightOrange": "#FFD569",
    "green": "#A6E22E",
 def get_error_output(error):
     return Colorful.color_text("red", error)
Пример #6
0
 def test_two_digits_is_colorful(self):
     c = Colorful()
     result = c.is_colorful(25)
     self.assertIs(result, True)
Пример #7
0
 def test_is_not_colorful(self):
     c = Colorful()
     result = c.is_colorful(236)
     self.assertIs(result, False)
Пример #8
0
 def test_is_colorful(self):
     c = Colorful()
     result = c.is_colorful(263)
     self.assertIs(result, True)
Пример #9
0
 def test_two_digits_is_not_colorful(self):
     c = Colorful()
     result = c.is_colorful(11)
     self.assertIs(result, False)