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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb1d268303dcb414c828446d3c14ecf3ad5ea1c9e0d5487b5752dc273ed16c77 |
|
MD5 | 91ef7f1afcdd659e693d44e57187acaa |
|
BLAKE2b-256 | 94bf8afcbc461e969df070be8dd8f28a93897e0a215ba21c8df7650768324546 |
Hashes for yield_from_as_an_iterator-1.2.0-py26.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fd45882f581295035730fbd45d2f4ae7f6e56282bc8c1729f4286afd6b15129 |
|
MD5 | a8aa41ee135f07b90ac0e0c2392aa98a |
|
BLAKE2b-256 | e95c0b3b208822630147d6df7b6f02e14fb6de19a4025b1c2114e39f3061d92e |
Hashes for yield_from_as_an_iterator-1.2.0-py20-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0da36573d8628fd1aa834c770f5925d7f5e3ef2359c9c80dd26a43f6f9e1db72 |
|
MD5 | 12580538ce83cf4ebd1d8236a111ca7b |
|
BLAKE2b-256 | 35b940e93cfc547f37a0497c0df31cb2d590157519650c2d2d38ed6631cbd8e0 |