Skip to content

Commit

Permalink
refactor: convert to f-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ssut committed Nov 20, 2024
1 parent 780aab1 commit 9d7fea0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
6 changes: 4 additions & 2 deletions googletrans/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ async def _translate(

if r.status_code == 200:
data = utils.format_json(r.text)
if not isinstance(data, list):
data = [data] # Convert dict to list to match return type
return data, r

if self.raise_exception:
Expand All @@ -138,13 +140,13 @@ async def _translate(
DUMMY_DATA[0][0][0] = text
return DUMMY_DATA, r

def build_request(
async def build_request(
self, text: str, dest: str, src: str, override: typing.Dict[str, typing.Any]
) -> httpx.Request:
"""Async helper for making the translation request"""
token = "xxxx" # dummy default value here as it is not used by api client
if self.client_type == "webapp":
token = self.token_acquirer.do(text)
token = await self.token_acquirer.do(text)

params = utils.build_params(
client=self.client_type,
Expand Down
15 changes: 4 additions & 11 deletions googletrans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,9 @@ def __str__(self): # pragma: nocover

def __unicode__(self): # pragma: nocover
return (
"Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation}, "
"extra_data={extra_data})".format(
src=self.src,
dest=self.dest,
text=self.text,
pronunciation=self.pronunciation,
extra_data='"' + repr(self.extra_data)[:10] + '..."',
)
f"Translated(src={self.src}, dest={self.dest}, text={self.text}, "
f"pronunciation={self.pronunciation}, "
f'extra_data="{repr(self.extra_data)[:10]}...")'
)


Expand All @@ -68,6 +63,4 @@ def __str__(self): # pragma: nocover
return self.__unicode__()

def __unicode__(self): # pragma: nocover
return "Detected(lang={lang}, confidence={confidence})".format(
lang=self.lang, confidence=self.confidence
)
return f"Detected(lang={self.lang}, confidence={self.confidence})"
49 changes: 28 additions & 21 deletions translate
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import sys

from googletrans import Translator


def main():
parser = argparse.ArgumentParser(
description='Python Google Translator as a command-line tool')
parser.add_argument('text', help='The text you want to translate.')
parser.add_argument('-d', '--dest', default='en',
help='The destination language you want to translate. (Default: en)')
parser.add_argument('-s', '--src', default='auto',
help='The source language you want to translate. (Default: auto)')
parser.add_argument('-c', '--detect', action='store_true', default=False,
help='')
description="Python Google Translator as a command-line tool"
)
parser.add_argument("text", help="The text you want to translate.")
parser.add_argument(
"-d",
"--dest",
default="en",
help="The destination language you want to translate. (Default: en)",
)
parser.add_argument(
"-s",
"--src",
default="auto",
help="The source language you want to translate. (Default: auto)",
)
parser.add_argument("-c", "--detect", action="store_true", default=False, help="")
args = parser.parse_args()
translator = Translator()

if args.detect:
result = translator.detect(args.text)
result = """
[{lang}, {confidence}] {text}
""".strip().format(text=args.text,
lang=result.lang, confidence=result.confidence)
result = f"""
[{result.lang}, {result.confidence}] {args.text}
""".strip()
print(result)
return

result = translator.translate(args.text, dest=args.dest, src=args.src)
result = u"""
[{src}] {original}
result = f"""
[{result.src}] {result.origin}
->
[{dest}] {text}
[pron.] {pronunciation}
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
text=result.text, pronunciation=result.pronunciation)
[{result.dest}] {result.text}
[pron.] {result.pronunciation}
""".strip()
print(result)

if __name__ == '__main__':

if __name__ == "__main__":
main()

0 comments on commit 9d7fea0

Please sign in to comment.