From 8adafb71c914eac6f217d7bd839e4c024e53caeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Thu, 20 Feb 2020 16:21:25 +0100 Subject: [PATCH] Misc: Utilize yield from It is more effective than iterating and yielding every item. Issue #2201 --- weblate/addons/cleanup.py | 9 +++++---- weblate/checks/flags.py | 3 +-- weblate/formats/base.py | 5 +---- weblate/screenshots/views.py | 3 +-- weblate/utils/stats.py | 5 +++-- weblate/utils/views.py | 3 +-- weblate/vcs/git.py | 3 +-- weblate/vcs/mercurial.py | 3 +-- 8 files changed, 14 insertions(+), 20 deletions(-) diff --git a/weblate/addons/cleanup.py b/weblate/addons/cleanup.py index 472f17afcc8e..070349604466 100644 --- a/weblate/addons/cleanup.py +++ b/weblate/addons/cleanup.py @@ -45,10 +45,11 @@ def build_index(storage): @staticmethod def iterate_translations(component): - for translation in component.translation_set.iterator(): - if translation.is_source: - continue - yield translation + yield from ( + translation + for translation in component.translation_set.iterator() + if not translation.is_source + ) @classmethod def can_install(cls, component, user): diff --git a/weblate/checks/flags.py b/weblate/checks/flags.py index a77e269178ca..e6e8064268fe 100644 --- a/weblate/checks/flags.py +++ b/weblate/checks/flags.py @@ -123,8 +123,7 @@ def parse_xml(cls, flags): yield "font-weight:{}".format(font[2].strip()) text = flags.get("weblate-flags") if text: - for flag in cls.parse(text): - yield flag + yield from cls.parse(text) def has_value(self, key): return key in self._values diff --git a/weblate/formats/base.py b/weblate/formats/base.py index 4a4513df68de..db1efca28a46 100644 --- a/weblate/formats/base.py +++ b/weblate/formats/base.py @@ -332,10 +332,7 @@ def all_units(self): @property def content_units(self): - for unit in self.all_units: - if not unit.has_content(): - continue - yield unit + yield from (unit for unit in self.all_units if unit.has_content()) @staticmethod def mimetype(): diff --git a/weblate/screenshots/views.py b/weblate/screenshots/views.py index 256a704b6507..739212b14586 100644 --- a/weblate/screenshots/views.py +++ b/weblate/screenshots/views.py @@ -216,8 +216,7 @@ def ocr_extract(api, image, strings): ocr_result = api.GetUTF8Text() parts = [ocr_result] + ocr_result.split('|') + ocr_result.split() for part in parts: - for match in difflib.get_close_matches(part, strings, cutoff=0.9): - yield match + yield from difflib.get_close_matches(part, strings, cutoff=0.9) api.Clear() diff --git a/weblate/utils/stats.py b/weblate/utils/stats.py index b31b8331685f..0272329d729b 100644 --- a/weblate/utils/stats.py +++ b/weblate/utils/stats.py @@ -501,8 +501,9 @@ def invalidate(self, language=None): clist.stats.invalidate() def get_language_stats(self): - for translation in self.translation_set: - yield TranslationStats(translation) + yield from ( + TranslationStats(translation) for translation in self.translation_set + ) def get_single_language_stats(self, language): try: diff --git a/weblate/utils/views.py b/weblate/utils/views.py index 95fce45c3976..0efbeee0ad92 100644 --- a/weblate/utils/views.py +++ b/weblate/utils/views.py @@ -154,8 +154,7 @@ def iter_files(filenames): for root, _unused, files in os.walk(filename): if "/.git/" in root or "/.hg/" in root: continue - for name in files: - yield os.path.join(root, name) + yield from (os.path.join(root, name) for name in files) else: yield filename diff --git a/weblate/vcs/git.py b/weblate/vcs/git.py index ab93f8d5f8f7..7f8d2a47059b 100644 --- a/weblate/vcs/git.py +++ b/weblate/vcs/git.py @@ -374,8 +374,7 @@ def parse_changed_files(self, lines): """Parses output with chanaged files.""" # Strip action prefix we do not use for line in lines: - for part in line.split('\t')[1:]: - yield part + yield from line.split('\t')[1:] class GitWithGerritRepository(GitRepository): diff --git a/weblate/vcs/mercurial.py b/weblate/vcs/mercurial.py index c6800d443f13..eb5f7c4e6c0d 100644 --- a/weblate/vcs/mercurial.py +++ b/weblate/vcs/mercurial.py @@ -344,5 +344,4 @@ def update_remote(self): def parse_changed_files(self, lines): """Parses output with chanaged files.""" # Strip action prefix we do not use - for line in lines: - yield line[2:] + yield from (line[2:] for line in lines)