A robust implementation of ``yield from`` behavior.
Project description
A robust implementation of yield from behavior. Good for transpilers, backpilers, and code that needs to be portable to minimal or old Pythons.
This implementation avoids the complexity and overheads of typical yield from backports - the tradeoff is that it is less obvious and does not resemble yield from syntax.
Versioning
This library’s version numbers follow the SemVer 2.0.0 specification.
Installation
pip install yield-from-as-an-iterator
Usage
Import yield_from:
from yieldfrom import yield_from
Replace yield from ... with:
for value, handle_send, handle_throw in yield_from(...):
sent = None
try:
sent = yield value
except:
if not handle_throw(*sys.exc_info()):
raise
handle_send(sent)
Replace result = yield from ... with:
wrapper = yield_from(...)
for value, handle_send, handle_throw in wrapper:
sent = None
try:
sent = yield value
except:
if not handle_throw(*sys.exc_info()):
raise
handle_send(sent)
result = wrapper.result
Portability
Portable to all releases of Python 3, and releases of Python 2 starting with 2.6.
On older or more minimal Pythons, the code will still import, so long as the right variant of the module file was chosen (because Python below 2.6 did not have except ... as ... syntax), and should work so long as the following are built-in or polyfilled:
The next function (just the one-argument form) (added in Python 2.6).
The GeneratorExit exception (added in Python 2.5).
The iter function (just the one-argument form) (added in Python 2.2).
The StopIteration exception (added in Python 2.2).
But as you go lower you will run into bigger problems:
generators only gained the ability to move data bidirectionally, and the .send and .throw methods to do so, in Python 2.5,
generators and yield were only added in Python 2.2 (and needed a from __future__ import generators until 2.3), and
the iterator protocol was only added in Python 2.2.
But, so long as you have objects which implement those interfaces, this module should help you get yield from behavior with them.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for yield-from-as-an-iterator-1.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c60104966a9ace83c477810e8a48aab79a593ecfdd93239681f28a1d95b8e357 |
|
MD5 | 52d3329021c1fde6a525a706675064b5 |
|
BLAKE2b-256 | d04ae6d426c7cf01be7cd1a1800cbb780183e2334f79f8484622e9d9d0379742 |
Hashes for yield_from_as_an_iterator-1.1.4-py26.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 815cbd29666999d4820929ca72d73edd0359da16da4599abed330cec971b384b |
|
MD5 | 9ee6df98e280653ea147df4c00125b2e |
|
BLAKE2b-256 | 3a93f65a5a89364ce8dccc376dfdd53d4fd87ce2e18aa2626e420e5c6c989f21 |
Hashes for yield_from_as_an_iterator-1.1.4-py20-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a4f3fb54749e66ae99fbf602971a47d4fc6a752f61fd4ff9959d1c13c3c95fb |
|
MD5 | 27234df32ef639e7a50cc4ba783b8c5b |
|
BLAKE2b-256 | 4a212481645cd64e29771cf070d6eb9a4f2eb611336f7f810b23b21e6d36e00c |