Skip to content

Commit 4031231

Browse files
committed
Simplify code / reduce memory usage a bit by using yielding
1 parent af41b04 commit 4031231

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Polskie tłumaczenie dokumentacji Pythona
33
<!-- [[[cog
44
from manage_translation import get_resource_language_stats, progress_from_resources, language_switcher, get_number_of_translators
55
6-
stats = get_resource_language_stats()
7-
switcher = progress_from_resources(stats, language_switcher)
8-
total = progress_from_resources(stats, lambda _: True)
6+
stats = list(get_resource_language_stats())
7+
switcher = progress_from_resources(filter(language_switcher, stats))
8+
total = progress_from_resources(stats)
99
translators = get_number_of_translators()
1010
1111
print(

manage_translation.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from re import match
2121
from subprocess import call
2222
import sys
23-
from typing import Self, Callable
23+
from typing import Self, Generator, Iterable
2424
from urllib.parse import urlparse, parse_qs
2525
from warnings import warn
2626

@@ -124,10 +124,9 @@ def from_api_v3_entry(cls, data: dict) -> Self:
124124
)
125125

126126

127-
def _get_from_api_v3_with_cursor(url: str, params: dict) -> list[dict]:
127+
def _get_from_api_v3_with_cursor(url: str, params: dict) -> Generator[dict, None, None]:
128128
from requests import get
129129

130-
resources = []
131130
cursor = None
132131
if os.path.exists('.tx/api-key'):
133132
with open('.tx/api-key') as f:
@@ -142,51 +141,46 @@ def _get_from_api_v3_with_cursor(url: str, params: dict) -> list[dict]:
142141
)
143142
response.raise_for_status()
144143
response_json = response.json()
145-
response_list = response_json['data']
146-
resources.extend(response_list)
144+
yield from response_json['data']
147145
if not response_json['links'].get('next'): # for stats no key, for list resources null
148146
break
149147
cursor, *_ = parse_qs(urlparse(response_json['links']['next']).query)['page[cursor]']
150-
return resources
151148

152149

153-
def _get_resources() -> list[Resource]:
150+
def _get_resources() -> Generator[Resource, None, None]:
154151
resources = _get_from_api_v3_with_cursor(
155152
'https://rest.api.transifex.com/resources', {'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}'}
156153
)
157-
return [Resource.from_api_v3_entry(entry) for entry in resources]
154+
yield from (Resource.from_api_v3_entry(entry) for entry in resources)
158155

159156

160-
def get_resource_language_stats() -> list[ResourceLanguageStatistics]:
157+
def get_resource_language_stats() -> Generator[ResourceLanguageStatistics, None, None]:
161158
resources = _get_from_api_v3_with_cursor(
162159
'https://rest.api.transifex.com/resource_language_stats',
163160
{'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'}
164161
)
165-
return [ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources]
162+
yield from (ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources)
166163

167164

168-
def progress_from_resources(resources: list[ResourceLanguageStatistics], filter_function: Callable) -> float:
169-
filtered = filter(filter_function, resources)
170-
pairs = ((e.translated_words, e.total_words) for e in filtered)
165+
def progress_from_resources(resources: Iterable[ResourceLanguageStatistics]) -> float:
166+
pairs = ((e.translated_words, e.total_words) for e in resources)
171167
translated_total, total_total = (sum(counts) for counts in zip(*pairs))
172168
return translated_total / total_total * 100
173169

174170

175171
def get_number_of_translators():
176-
translators = _fetch_translators()
172+
translators = set(_fetch_translators())
177173
_remove_aliases(translators)
178174
_check_for_new_aliases(translators)
179175
return len(translators)
180176

181177

182-
def _fetch_translators() -> set[str]:
183-
translators = set()
178+
def _fetch_translators() -> Generator[str, None, None]:
184179
for file in Path().rglob('*.po'):
185180
header = pofile(file).header.splitlines()
186181
for translator_record in header[header.index('Translators:') + 1:]:
187182
translator, _year = translator_record.split(', ')
188-
translators.add(translator)
189-
return translators
183+
yield translator
190184

191185

192186
def _remove_aliases(translators: set[str]) -> None:

0 commit comments

Comments
 (0)