yieldfrom is a backport of the yield from semantic from Python 3.x to
Python 2.7. It also supports Python 3.x so it can be used as compatibility
library for code that supports both major versions of Python.
If you want to nest generators in Python 3.x, you can use the yield from
keywords. This allows you to automatically iterate over sub-generators and
transparently pass exceptions and return values from the top level caller
to the lowest generator.
def subgen():
yield 2
yield 3
def gen():
yield 1
yield from subgen() # Python 3.x only
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
This functionality is not available in Python 2.x, and we emulate it using the
yieldfrom decorator and the helper From class:
from yieldfrom import yieldfrom, From
def subgen():
yield 2
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
Advanced usage allows returning a value from the subgenerator using
StopIteration. Using Return does this conveniently:
from yieldfrom import yieldfrom, From, Return
def subgen():
yield 2
yield 3
Return(100) # Raises `StopIteration(100)`
@yieldfrom
def gen():
yield 1
ret = (yield From(subgen()))
yield 4
yield ret
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4 100
Subgenerators can be nested on multiple levels, each one requiring additional
decoration by yieldfrom:
def subsubgen():
yield 2
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
for i in gen():
print i,
>>> main()
... 1 2 3 4
Exceptions thrown into the top-level generator can be handled in relevant subgenerators:
def subsubgen():
try:
yield 2
except ValueError:
yield 200
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
def main():
try:
g = gen()
while True:
i = g.next()
if i == 2:
i = g.throw(ValueError())
print i,
except StopIteration:
pass
>>> main()
... 1 200 3 4
Note that if you use yield From() on a simple iterable (list,
tuple, etc) then the individual members of the iterator will be yielded on
each iteration (perhaps in that case you need the usual yield).
@yieldfrom
def gen():
yield From([1, 2, 3])
yield [1, 2, 3]
def main():
for i in gen():
print i
>>> main()
... 1
... 2
... 3
... [1, 2, 3]
Passing non-iterable objects to From will result in an empty
generator that does nothing.
@yieldfrom
def gen():
yield From(None)
yield 1
def main():
for i in gen():
print i
>>> main()
... 1
This module is an adaptation of the following Python recipe:
http://code.activestate.com/recipes/576727
Modifications include bug fixes in exception handling, naming, documentation,
handling of empty generators, etc.
Metadata
Release files for yieldfrom 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| yieldfrom-1.0.5.tar.gz | 5.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| yieldfrom-1.0.5-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / yieldfrom-1.0.5.tar.gz
| Download URL | yieldfrom-1.0.5.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb75e58641410f06083f85013a438f28bceaabfe5af9c3206d5049362c37aca0
|
|
BLAKE2b-256 checksum How to use checksums |
4df9395917f574ace618eb234bcbae8df3fabaa9624532d96d1fbd3a20678b1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.0
|
Release files / yieldfrom-1.0.5-py2.py3-none-any.whl
| Download URL | yieldfrom-1.0.5-py2.py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ab52d7b4128b795dd2d942fad0126fcf2d7dbd35cde2a340cdafb66273b0ee3a
|
|
BLAKE2b-256 checksum How to use checksums |
d6e84bc727e1fefe1b1b8f5a3516df2f17621271e9a9700afecd86f5c311c613
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.0
|
Security for yieldfrom 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.