Skip to content

Commit

Permalink
Misc: Utilize yield from
Browse files Browse the repository at this point in the history
It is more effective than iterating and yielding every item.

Issue #2201
  • Loading branch information
nijel committed Feb 20, 2020
1 parent af737b4 commit 8adafb7
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 20 deletions.
9 changes: 5 additions & 4 deletions weblate/addons/cleanup.py
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions weblate/checks/flags.py
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions weblate/formats/base.py
Expand Up @@ -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():
Expand Down
3 changes: 1 addition & 2 deletions weblate/screenshots/views.py
Expand Up @@ -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()


Expand Down
5 changes: 3 additions & 2 deletions weblate/utils/stats.py
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions weblate/utils/views.py
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions weblate/vcs/git.py
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions weblate/vcs/mercurial.py
Expand Up @@ -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)

0 comments on commit 8adafb7

Please sign in to comment.