Skip to content

Commit 9d7fea0

Browse files
committed
refactor: convert to f-string
1 parent 780aab1 commit 9d7fea0

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

googletrans/client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ async def _translate(
126126

127127
if r.status_code == 200:
128128
data = utils.format_json(r.text)
129+
if not isinstance(data, list):
130+
data = [data] # Convert dict to list to match return type
129131
return data, r
130132

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

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

149151
params = utils.build_params(
150152
client=self.client_type,

googletrans/models.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,9 @@ def __str__(self): # pragma: nocover
4141

4242
def __unicode__(self): # pragma: nocover
4343
return (
44-
"Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation}, "
45-
"extra_data={extra_data})".format(
46-
src=self.src,
47-
dest=self.dest,
48-
text=self.text,
49-
pronunciation=self.pronunciation,
50-
extra_data='"' + repr(self.extra_data)[:10] + '..."',
51-
)
44+
f"Translated(src={self.src}, dest={self.dest}, text={self.text}, "
45+
f"pronunciation={self.pronunciation}, "
46+
f'extra_data="{repr(self.extra_data)[:10]}...")'
5247
)
5348

5449

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

7065
def __unicode__(self): # pragma: nocover
71-
return "Detected(lang={lang}, confidence={confidence})".format(
72-
lang=self.lang, confidence=self.confidence
73-
)
66+
return f"Detected(lang={self.lang}, confidence={self.confidence})"

translate

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32
import argparse
4-
import sys
3+
54
from googletrans import Translator
65

6+
77
def main():
88
parser = argparse.ArgumentParser(
9-
description='Python Google Translator as a command-line tool')
10-
parser.add_argument('text', help='The text you want to translate.')
11-
parser.add_argument('-d', '--dest', default='en',
12-
help='The destination language you want to translate. (Default: en)')
13-
parser.add_argument('-s', '--src', default='auto',
14-
help='The source language you want to translate. (Default: auto)')
15-
parser.add_argument('-c', '--detect', action='store_true', default=False,
16-
help='')
9+
description="Python Google Translator as a command-line tool"
10+
)
11+
parser.add_argument("text", help="The text you want to translate.")
12+
parser.add_argument(
13+
"-d",
14+
"--dest",
15+
default="en",
16+
help="The destination language you want to translate. (Default: en)",
17+
)
18+
parser.add_argument(
19+
"-s",
20+
"--src",
21+
default="auto",
22+
help="The source language you want to translate. (Default: auto)",
23+
)
24+
parser.add_argument("-c", "--detect", action="store_true", default=False, help="")
1725
args = parser.parse_args()
1826
translator = Translator()
1927

2028
if args.detect:
2129
result = translator.detect(args.text)
22-
result = """
23-
[{lang}, {confidence}] {text}
24-
""".strip().format(text=args.text,
25-
lang=result.lang, confidence=result.confidence)
30+
result = f"""
31+
[{result.lang}, {result.confidence}] {args.text}
32+
""".strip()
2633
print(result)
2734
return
2835

2936
result = translator.translate(args.text, dest=args.dest, src=args.src)
30-
result = u"""
31-
[{src}] {original}
37+
result = f"""
38+
[{result.src}] {result.origin}
3239
->
33-
[{dest}] {text}
34-
[pron.] {pronunciation}
35-
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
36-
text=result.text, pronunciation=result.pronunciation)
40+
[{result.dest}] {result.text}
41+
[pron.] {result.pronunciation}
42+
""".strip()
3743
print(result)
3844

39-
if __name__ == '__main__':
45+
46+
if __name__ == "__main__":
4047
main()

0 commit comments

Comments
 (0)