]> granicus.if.org Git - python/commitdiff
utilize yield from
authorPhilip Jenvey <pjenvey@underboss.org>
Mon, 1 Oct 2012 19:53:43 +0000 (12:53 -0700)
committerPhilip Jenvey <pjenvey@underboss.org>
Mon, 1 Oct 2012 19:53:43 +0000 (12:53 -0700)
Lib/argparse.py
Lib/collections/abc.py
Lib/concurrent/futures/_base.py
Lib/difflib.py
Lib/email/_header_value_parser.py
Lib/email/iterators.py
Lib/glob.py
Lib/mailbox.py
Lib/pkgutil.py
Lib/traceback.py
Lib/weakref.py

index f25b1b66109596f0646d4743fd9101ce3c62dd05..202f2cb972784f1c5b3f376f982d558560d9efdb 100644 (file)
@@ -606,8 +606,7 @@ class HelpFormatter(object):
             pass
         else:
             self._indent()
-            for subaction in get_subactions():
-                yield subaction
+            yield from get_subactions()
             self._dedent()
 
     def _split_lines(self, text, width):
index d17cfdcd9b2d67fef70d853db1004dc219699fe7..440c35ba05f7b99d53752c593f48c63b9024dd12 100644 (file)
@@ -430,8 +430,7 @@ class KeysView(MappingView, Set):
         return key in self._mapping
 
     def __iter__(self):
-        for key in self._mapping:
-            yield key
+        yield from self._mapping
 
 KeysView.register(dict_keys)
 
index 1e098be33ff11751c91ec512e5583fce7f707653..883d993ce3d2f228cd14b282e78dffcc50d4807b 100644 (file)
@@ -198,8 +198,7 @@ def as_completed(fs, timeout=None):
         waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
 
     try:
-        for future in finished:
-            yield future
+        yield from finished
 
         while pending:
             if timeout is None:
index ae377d745dd3efe096bc60fcc27eadbda427afcf..db5f82ec035055616bc6a6d0aadebd38b393d977 100644 (file)
@@ -922,8 +922,7 @@ class Differ:
             else:
                 raise ValueError('unknown tag %r' % (tag,))
 
-            for line in g:
-                yield line
+            yield from g
 
     def _dump(self, tag, x, lo, hi):
         """Generate comparison results for a same-tagged range."""
@@ -942,8 +941,7 @@ class Differ:
             second = self._dump('+', b, blo, bhi)
 
         for g in first, second:
-            for line in g:
-                yield line
+            yield from g
 
     def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
         r"""
@@ -997,8 +995,7 @@ class Differ:
             # no non-identical "pretty close" pair
             if eqi is None:
                 # no identical pair either -- treat it as a straight replace
-                for line in self._plain_replace(a, alo, ahi, b, blo, bhi):
-                    yield line
+                yield from self._plain_replace(a, alo, ahi, b, blo, bhi)
                 return
             # no close pair, but an identical pair -- synch up on that
             best_i, best_j, best_ratio = eqi, eqj, 1.0
@@ -1010,8 +1007,7 @@ class Differ:
         # identical
 
         # pump out diffs from before the synch point
-        for line in self._fancy_helper(a, alo, best_i, b, blo, best_j):
-            yield line
+        yield from self._fancy_helper(a, alo, best_i, b, blo, best_j)
 
         # do intraline marking on the synch pair
         aelt, belt = a[best_i], b[best_j]
@@ -1033,15 +1029,13 @@ class Differ:
                     btags += ' ' * lb
                 else:
                     raise ValueError('unknown tag %r' % (tag,))
-            for line in self._qformat(aelt, belt, atags, btags):
-                yield line
+            yield from self._qformat(aelt, belt, atags, btags)
         else:
             # the synch pair is identical
             yield '  ' + aelt
 
         # pump out diffs from after the synch point
-        for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi):
-            yield line
+        yield from self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi)
 
     def _fancy_helper(self, a, alo, ahi, b, blo, bhi):
         g = []
@@ -1053,8 +1047,7 @@ class Differ:
         elif blo < bhi:
             g = self._dump('+', b, blo, bhi)
 
-        for line in g:
-            yield line
+        yield from g
 
     def _qformat(self, aline, bline, atags, btags):
         r"""
index 1924ed158862a1c8ee2cf3e853ade3795ba320e2..b5964627e7341e7e48bcf99574990cebc0823dd8 100644 (file)
@@ -367,8 +367,7 @@ class TokenList(list):
                 yield (indent + '    !! invalid element in token '
                                         'list: {!r}'.format(token))
             else:
-                for line in token._pp(indent+'    '):
-                    yield line
+                yield from token._pp(indent+'    ')
         if self.defects:
             extra = ' Defects: {}'.format(self.defects)
         else:
index 3adc4a04ba87cfc0a9cb33ea2930b593578b4d77..b5502ee975266ba17a3dbff387a82b9f3a8040e6 100644 (file)
@@ -26,8 +26,7 @@ def walk(self):
     yield self
     if self.is_multipart():
         for subpart in self.get_payload():
-            for subsubpart in subpart.walk():
-                yield subsubpart
+            yield from subpart.walk()
 
 
 \f
@@ -40,8 +39,7 @@ def body_line_iterator(msg, decode=False):
     for subpart in msg.walk():
         payload = subpart.get_payload(decode=decode)
         if isinstance(payload, str):
-            for line in StringIO(payload):
-                yield line
+            yield from StringIO(payload)
 
 
 def typed_subpart_iterator(msg, maintype='text', subtype=None):
index 36d493d7a8032656ce49e1531ab1e9508564220d..3431a695bbe2f5d3043b51ee04adac086d5b5030 100644 (file)
@@ -26,8 +26,7 @@ def iglob(pathname):
         return
     dirname, basename = os.path.split(pathname)
     if not dirname:
-        for name in glob1(None, basename):
-            yield name
+        yield from glob1(None, basename)
         return
     if has_magic(dirname):
         dirs = iglob(dirname)
index d3bf3fd80b7dc2a375ec2a89d6ee92d1f8728215..0fea2160fcc68fe3750cf034b2b20a26c7dc42a3 100644 (file)
@@ -631,8 +631,7 @@ class _singlefileMailbox(Mailbox):
     def iterkeys(self):
         """Return an iterator over keys."""
         self._lookup()
-        for key in self._toc.keys():
-            yield key
+        yield from self._toc.keys()
 
     def __contains__(self, key):
         """Return True if the keyed message exists, False otherwise."""
index 8bdeb32e8188e64b32a0d663bf66a93b11d6c98b..3117c1779584e7fa26be12ad757c5db51a09e4cf 100644 (file)
@@ -121,8 +121,7 @@ def walk_packages(path=None, prefix='', onerror=None):
                 # don't traverse path items we've seen before
                 path = [p for p in path if not seen(p)]
 
-                for item in walk_packages(path, name+'.', onerror):
-                    yield item
+                yield from walk_packages(path, name+'.', onerror)
 
 
 def iter_modules(path=None, prefix=''):
index eeb9e7387c73b9b29d165ea6bc6bc64f557464cc..33b86c7e6e5042d5cb3e51ef5000a6e66c4eaf6a 100644 (file)
@@ -132,8 +132,7 @@ def _iter_chain(exc, custom_tb=None, seen=None):
     its.append([(exc, custom_tb or exc.__traceback__)])
     # itertools.chain is in an extension module and may be unavailable
     for it in its:
-        for x in it:
-            yield x
+        yield from it
 
 
 def print_exception(etype, value, tb, limit=None, file=None, chain=True):
index fcb6b74d1b1afba4e8dd3f2dc4df6ac5f0dc25e6..339fcf4718d5da377e1d06753865dcba5607a0df 100644 (file)
@@ -153,8 +153,7 @@ class WeakValueDictionary(collections.MutableMapping):
 
         """
         with _IterationGuard(self):
-            for wr in self.data.values():
-                yield wr
+            yield from self.data.values()
 
     def values(self):
         with _IterationGuard(self):