Skip to content

Commit a68b149

Browse files
committed
Improve typing
1 parent d216f70 commit a68b149

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

PyMultiDictionary/_dictionary.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def _process(self, word: str) -> str:
117117
:param word: Word
118118
:return: Word without invalid chars
119119
"""
120-
assert isinstance(word, str), 'word must be an string'
121-
s = ''.join(i for i in word if not i.isdigit()) # remove numbers
120+
assert isinstance(word, str), 'word must be a string'
121+
s: str = ''.join(i for i in word if not i.isdigit()) # remove numbers
122122
if self._tokenize: # tokenize
123123
s = ut.tokenize(s)
124124
s = s.lower() # lowercase
@@ -133,19 +133,18 @@ def _bsoup(self, link: str, encoding: str = 'utf-8') -> Optional['BeautifulSoup'
133133
:param encoding: Web encoding
134134
:return: Parsed web. None if error
135135
"""
136-
bs_keys = list(_CACHED_SOUPS.keys())
136+
bs_keys: List[str] = list(_CACHED_SOUPS.keys())
137137
if link in bs_keys:
138138
return _CACHED_SOUPS[link]
139139
if link in self._test_cached_file.keys():
140-
f = open(self._test_cached_file[link], encoding='utf8')
141-
data = ''.join(f.readlines())
142-
f.close()
140+
with open(self._test_cached_file[link], encoding='utf8') as f:
141+
data = ''.join(f.readlines())
143142
else:
144143
try:
145144
data = self.__request(link, encoding)
146145
except (urllib.error.HTTPError, ValueError):
147146
return None
148-
bs = BeautifulSoup(data, 'html.parser')
147+
bs: 'BeautifulSoup' = BeautifulSoup(data, 'html.parser')
149148
_CACHED_SOUPS[link] = bs
150149
if len(bs_keys) >= self._max_cached_websites:
151150
# noinspection PyTypeChecker
@@ -182,7 +181,7 @@ def _save_bsoup(self, link: str, filename: str, encoding: str = 'utf-8') -> None
182181
:param filename: Output file
183182
:param encoding: Website encoding
184183
"""
185-
bs = self._bsoup(link, encoding)
184+
bs: 'BeautifulSoup' = self._bsoup(link, encoding)
186185
with open(filename, 'w', encoding='utf8') as out:
187186
out.write(str(bs.prettify()))
188187

@@ -208,7 +207,7 @@ def __synonym_com(self, word: str, _type: str) -> SynonymType:
208207
if bs is None:
209208
return []
210209
results = bs.find_all('div', {'class': 'section'})
211-
en_words = []
210+
en_words: SynonymType = []
212211
for section in results: # Iterate each section
213212
title = section.find_all('h3', {'class': 'section-title'})
214213
if len(title) == 0:
@@ -229,12 +228,12 @@ def __synonym_com(self, word: str, _type: str) -> SynonymType:
229228
sectionlist = sectionlist[0]
230229
if 'href' not in str(sectionlist): # Not links, but words
231230
for w in sectionlist.findAll('li'):
232-
wr = w.text.strip()
231+
wr: str = w.text.strip()
233232
if '(' not in wr and wr not in en_words: # Avoid onld english
234233
en_words.append(wr)
235234
else:
236235
for w in sectionlist.findAll('a'):
237-
wr = w.text.strip()
236+
wr: str = w.text.strip()
238237
if '(' not in wr and wr not in en_words: # Avoid onld english
239238
en_words.append(wr)
240239
return en_words
@@ -248,14 +247,14 @@ def synonym(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Sy
248247
:param dictionary: Dictionary to retrieve the synonyms
249248
:return: Synonyms list
250249
"""
251-
words = []
250+
words: SynonymType = []
252251
word = self._process(word)
253252
lang = lang.lower()
254253

255254
assert dictionary in (DICT_EDUCALINGO, DICT_SYNONYMCOM, DICT_THESAURUS), 'Unsupported dictionary'
256255
if lang not in self._langs.keys() or not self._langs[lang][0]:
257256
raise InvalidLangCode(f'{lang} code is not supported for synonyms')
258-
if word == '':
257+
elif word == '':
259258
return words
260259

261260
if dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS:
@@ -313,16 +312,16 @@ def antonym(self, lang: str, word: str, dictionary: str = DICT_SYNONYMCOM) -> An
313312
:param dictionary: Dictionary to retrieve the antonyms
314313
:return: Synonyms list
315314
"""
316-
words = []
315+
words: AntonymType = []
317316
word = self._process(word)
318317

319318
assert dictionary in DICT_SYNONYMCOM, 'Unsupported dictionary'
320319
if lang not in self._langs.keys() or not self._langs[lang][3]:
321320
raise InvalidLangCode(f'{lang} code is not supported for antonyms')
322-
if word == '':
321+
elif word == '':
323322
return words
324323

325-
if dictionary == DICT_SYNONYMCOM and lang == 'en':
324+
elif dictionary == DICT_SYNONYMCOM and lang == 'en':
326325
en_words = self.__synonym_com(word, 'Antonyms')
327326
for w in en_words:
328327
if w not in words:
@@ -378,7 +377,7 @@ def meaning(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Me
378377
elif word == '':
379378
return types, words, wiki
380379

381-
if dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS:
380+
elif dictionary == DICT_EDUCALINGO and lang in _EDUCALINGO_LANGS:
382381
bs = self.__search_educalingo(lang, word=word.replace(' ', '-'))
383382
if bs is not None:
384383
results = [i for i in bs.find_all('div', {'id': 'cuadro_categoria_gramatical'})]
@@ -434,8 +433,7 @@ def meaning(self, lang: str, word: str, dictionary: str = DICT_EDUCALINGO) -> Me
434433

435434
return definitions
436435

437-
else:
438-
raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
436+
raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
439437

440438
def get_meanings(self, dictionary: str = DICT_EDUCALINGO) -> List[MeaningType]:
441439
"""
@@ -457,9 +455,9 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
457455
:param dictionary: Dictionary to retrieve the translations if ``to`` is empty
458456
:return: List of (Lang tag, translated word)
459457
"""
460-
assert isinstance(lang, str), 'lang code must be an string'
461-
assert isinstance(to, str), 'to lang code must be an string'
462-
words = []
458+
assert isinstance(lang, str), 'lang code must be a string'
459+
assert isinstance(to, str), 'to lang code must be a string'
460+
words: TranslationType = []
463461
word = self._process(word)
464462

465463
assert dictionary in DICT_EDUCALINGO, 'Unsupported dictionary'
@@ -473,7 +471,7 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
473471
if lang not in self._langs.keys() or not self._langs[lang][2]:
474472
raise InvalidLangCode(f'{lang} code is not supported for translation')
475473

476-
if lang in _EDUCALINGO_LANGS:
474+
elif lang in _EDUCALINGO_LANGS:
477475
bs = self.__search_educalingo(lang, word=word.replace(' ', '-'))
478476
if bs is None:
479477
return words
@@ -488,7 +486,6 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
488486
lang_name = lang_name[0].find_all('strong', {})
489487
if len(lang_name) != 1:
490488
continue
491-
# lang_name = lang_name[0].text.strip().capitalize()
492489

493490
# Find non-links
494491
lang_nonlink = j.find_all('span', {'class': 'negro'})
@@ -507,9 +504,6 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
507504
# Sort translations
508505
words = sorted(words, key=lambda x: x[0])
509506

510-
# else:
511-
# raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
512-
513507
return words
514508

515509
def get_translations(self, to: str = '', dictionary: str = DICT_EDUCALINGO) -> List[TranslationType]:

0 commit comments

Comments
 (0)