Skip to main content

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:

  1. The next function (just the one-argument form) (added in Python 2.6).

  2. The GeneratorExit exception (added in Python 2.5).

  3. The iter function (just the one-argument form) (added in Python 2.2).

  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yield-from-as-an-iterator-1.2.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distributions

yield_from_as_an_iterator-1.2.0-py26.py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 2.6 Python 3

yield_from_as_an_iterator-1.2.0-py20-none-any.whl (5.2 kB view hashes)

Uploaded Python 2.0

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page