Skip to content

Commit c9fc8b6

Browse files
authored
Merge pull request #220 from nidhaloff/hotfix/digit-translation
Hotfix/digit translation
2 parents 5891228 + 6bf8aa6 commit c9fc8b6

File tree

15 files changed

+35
-38
lines changed

15 files changed

+35
-38
lines changed

deep_translator/deepl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
ServerException,
1818
TranslationNotFound,
1919
)
20-
from deep_translator.validate import is_empty, is_input_valid
20+
from deep_translator.validate import is_empty, is_input_valid, request_failed
2121

2222

2323
class DeeplTranslator(BaseTranslator):
@@ -85,7 +85,7 @@ def translate(self, text: str, **kwargs) -> str:
8585
# If the answer is not success, raise server exception.
8686
if response.status_code == 403:
8787
raise AuthorizationException(self.api_key)
88-
elif response.status_code != 200:
88+
if request_failed(status_code=response.status_code):
8989
raise ServerException(response.status_code)
9090
# Get the response and check is not empty.
9191
res = response.json()

deep_translator/google.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
TooManyRequests,
1717
TranslationNotFound,
1818
)
19-
from deep_translator.validate import is_empty, is_input_valid
19+
from deep_translator.validate import is_empty, is_input_valid, request_failed
2020

2121

2222
class GoogleTranslator(BaseTranslator):
@@ -70,7 +70,7 @@ def translate(self, text: str, **kwargs) -> str:
7070
if response.status_code == 429:
7171
raise TooManyRequests()
7272

73-
if response.status_code != 200:
73+
if request_failed(status_code=response.status_code):
7474
raise RequestError()
7575

7676
soup = BeautifulSoup(response.text, "html.parser")

deep_translator/libre.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ServerException,
2121
TranslationNotFound,
2222
)
23-
from deep_translator.validate import is_empty, is_input_valid
23+
from deep_translator.validate import is_empty, is_input_valid, request_failed
2424

2525

2626
class LibreTranslator(BaseTranslator):
@@ -95,7 +95,7 @@ def translate(self, text: str, **kwargs) -> str:
9595

9696
if response.status_code == 403:
9797
raise AuthorizationException(self.api_key)
98-
elif response.status_code != 200:
98+
elif request_failed(status_code=response.status_code):
9999
raise ServerException(response.status_code)
100100
# Get the response and check is not empty.
101101
res = response.json()

deep_translator/linguee.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
TooManyRequests,
2020
TranslationNotFound,
2121
)
22-
from deep_translator.validate import is_empty, is_input_valid
22+
from deep_translator.validate import is_empty, is_input_valid, request_failed
2323

2424

2525
class LingueeTranslator(BaseTranslator):
@@ -72,8 +72,9 @@ def translate(
7272
if response.status_code == 429:
7373
raise TooManyRequests()
7474

75-
if response.status_code != 200:
75+
if request_failed(status_code=response.status_code):
7676
raise RequestError()
77+
7778
soup = BeautifulSoup(response.text, "html.parser")
7879
elements = soup.find_all(self._element_tag, self._element_query)
7980
response.close()

deep_translator/mymemory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
TooManyRequests,
1616
TranslationNotFound,
1717
)
18-
from deep_translator.validate import is_empty, is_input_valid
18+
from deep_translator.validate import is_empty, is_input_valid, request_failed
1919

2020

2121
class MyMemoryTranslator(BaseTranslator):
@@ -71,7 +71,7 @@ def translate(
7171

7272
if response.status_code == 429:
7373
raise TooManyRequests()
74-
if response.status_code != 200:
74+
if request_failed(status_code=response.status_code):
7575
raise RequestError()
7676

7777
data = response.json()

deep_translator/papago.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from deep_translator.base import BaseTranslator
1313
from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
1414
from deep_translator.exceptions import TranslationNotFound
15-
from deep_translator.validate import is_input_valid
15+
from deep_translator.validate import is_input_valid, request_failed
1616

1717

1818
class PapagoTranslator(BaseTranslator):
@@ -67,7 +67,7 @@ def translate(self, text: str, **kwargs) -> str:
6767
response = requests.post(
6868
self._base_url, headers=headers, data=payload
6969
)
70-
if response.status_code != 200:
70+
if request_failed(status_code=response.status_code):
7171
raise Exception(
7272
f"Translation error! -> status code: {response.status_code}"
7373
)

deep_translator/pons.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
TooManyRequests,
2020
TranslationNotFound,
2121
)
22-
from deep_translator.validate import is_empty, is_input_valid
22+
from deep_translator.validate import is_empty, is_input_valid, request_failed
2323

2424

2525
class PonsTranslator(BaseTranslator):
@@ -71,7 +71,7 @@ def translate(
7171
if response.status_code == 429:
7272
raise TooManyRequests()
7373

74-
if response.status_code != 200:
74+
if request_failed(status_code=response.status_code):
7575
raise RequestError()
7676

7777
soup = BeautifulSoup(response.text, "html.parser")

deep_translator/qcri.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ServerException,
1717
TranslationNotFound,
1818
)
19+
from deep_translator.validate import request_failed
1920

2021

2122
class QcriTranslator(BaseTranslator):
@@ -96,7 +97,7 @@ def translate(self, text: str, **kwargs) -> str:
9697
raise ServerException(503)
9798

9899
else:
99-
if response.status_code != 200:
100+
if request_failed(status_code=response.status_code):
100101
ServerException(response.status_code)
101102
else:
102103
res = response.json()

deep_translator/validate.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ def is_empty(text: str) -> bool:
99
return text == ""
1010

1111

12+
def request_failed(status_code: int) -> bool:
13+
"""Check if a request has failed or not.
14+
A request is considered successfull if the status code is in the 2** range.
15+
16+
Args:
17+
status_code (int): status code of the request
18+
19+
Returns:
20+
bool: indicates request failure
21+
"""
22+
if status_code > 299 or status_code < 200:
23+
return True
24+
return False
25+
26+
1227
def is_input_valid(
1328
text: str, min_chars: int = 0, max_chars: Optional[int] = None
1429
) -> bool:
@@ -20,7 +35,7 @@ def is_input_valid(
2035
@return: bool
2136
"""
2237

23-
if not isinstance(text, str) or text.isdigit():
38+
if not isinstance(text, str):
2439
raise NotValidPayload(text)
2540
if max_chars and (not min_chars <= len(text) < max_chars):
2641
raise NotValidLength(text, min_chars, max_chars)

deep_translator/yandex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
TooManyRequests,
1919
TranslationNotFound,
2020
)
21-
from deep_translator.validate import is_input_valid
21+
from deep_translator.validate import is_input_valid, request_failed
2222

2323

2424
class YandexTranslator(BaseTranslator):
@@ -75,7 +75,7 @@ def dirs(self, proxies: Optional[dict] = None):
7575
else:
7676
data = response.json()
7777

78-
if response.status_code != 200:
78+
if request_failed(status_code=response.status_code):
7979
raise ServerException(response.status_code)
8080
return data.get("dirs")
8181

tests/test_google.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ def test_empty_text(google_translator):
4646

4747

4848
def test_payload(google_translator):
49-
with pytest.raises(exceptions.NotValidPayload):
50-
google_translator.translate(text="1234")
51-
google_translator.translate(text="{}")
52-
google_translator.translate(text="%@")
53-
54-
with pytest.raises(exceptions.NotValidPayload):
55-
google_translator.translate(text=123)
56-
5749
with pytest.raises(exceptions.NotValidPayload):
5850
google_translator.translate(text={})
5951

tests/test_libre.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ def test_abbreviations_and_languages_mapping():
3232

3333

3434
def test_payload(libre):
35-
with pytest.raises(exceptions.NotValidPayload):
36-
libre.translate(123)
37-
3835
with pytest.raises(exceptions.NotValidPayload):
3936
libre.translate({})
4037

tests/test_linguee.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ def test_inputs():
3838

3939

4040
def test_payload(linguee):
41-
with pytest.raises(exceptions.NotValidPayload):
42-
linguee.translate(123)
43-
4441
with pytest.raises(exceptions.NotValidPayload):
4542
linguee.translate({})
4643

tests/test_mymemory.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def test_inputs():
3636

3737

3838
def test_payload(mymemory):
39-
with pytest.raises(exceptions.NotValidPayload):
40-
mymemory.translate(text=123)
41-
4239
with pytest.raises(exceptions.NotValidPayload):
4340
mymemory.translate(text={})
4441

tests/test_pons.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def test_inputs():
3636

3737

3838
def test_payload(pons):
39-
with pytest.raises(exceptions.NotValidPayload):
40-
pons.translate(123)
41-
4239
with pytest.raises(exceptions.NotValidPayload):
4340
pons.translate({})
4441

0 commit comments

Comments
 (0)