A backport of the `yield from` semantic from Python 3.x to Python 2.7
Project description
A backport of the `yield from` semantic from Python 3.x to Python 2.7
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.
```.py
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:
```.py
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:
```.py
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`:
```.py
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:
```.py
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`).
```.py
@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.
```.py
@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.
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.
```.py
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:
```.py
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:
```.py
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`:
```.py
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:
```.py
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`).
```.py
@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.
```.py
@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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
yieldfrom-1.0.2.tar.gz
(4.3 kB
view details)
Built Distribution
File details
Details for the file yieldfrom-1.0.2.tar.gz
.
File metadata
- Download URL: yieldfrom-1.0.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 772e46e62d41fb045d22deeaf62fa071673e201ce0478f5477860b663153a03e |
|
MD5 | 5cab50e40e0025c6ba9dd8e801ae2b1c |
|
BLAKE2b-256 | 152c33c6222cc91254e644db3b4b446f03a34a5d47a1a0e17ff8360d2c4a5b5e |
File details
Details for the file yieldfrom-1.0.2-py2-none-any.whl
.
File metadata
- Download URL: yieldfrom-1.0.2-py2-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d30454790c70e6c099dbc729a2ff5812f149860d0c2b0093537df0b1cc29d125 |
|
MD5 | 00be2cbab9bc65cb7a46e69354d3c794 |
|
BLAKE2b-256 | 1855cd81b9c079829f289c933244ab9bf2b377d230833c23ab371b8d96775cd5 |