Recently, I've been working more and more with the CUI. CUI is flexible and very nice ...
(This is a matter of my English ability)
I can read error messages such as python traceback to some extent,
I couldn't read the Linux man
page and --help
because they were almost in English.
Therefore···
I came up with the idea.
--Use googletrans
--Accept options with command line arguments
--Supports input using |
(pipe)
#honnyaku.py
# ~Receive text with standard I / O,
# ~Print it
import sys
from googletrans import Translator
from logging import getLogger, StreamHandler, DEBUG
# ~Log settings
logger = getLogger(__name__)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False
trans = Translator()
#Main processing
def convert(text="None", lang="ja"):
'''Function to translate'''
#Converts non-numeric characters to strings and excludes types that cannot be converted to str, such as float types
try:
text = str(text)
except:
logger.error("You have entered something other than a string or an integer.")
conv_text = trans.translate(text, dest=lang)
return conv_text.text
This time it was developed in a test-driven development style.
# ~Test code
import honnyaku
try:
assert "Hello" == honnyaku.convert("hello")
except AssertionError:
print("Failure")
else:
print("success")
Actual state
I was able to translate it successfully! I'd like to think about how to implement options a little more, so I'd like to do it next time.
Recommended Posts