Skip to main content

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.

Release files for yield-from-as-an-iterator 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yield-from-as-an-iterator 1.2.0
File Size Uploaded
yield-from-as-an-iterator-1.2.0.tar.gz 5.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for yield-from-as-an-iterator 1.2.0
File Interpreter ABI Platform
yield_from_as_an_iterator-1.2.0-py26.py3-none-any.whl Python 3, Python 2.6 none any Details
yield_from_as_an_iterator-1.2.0-py20-none-any.whl Python 2.0 none any Details

Total release size: 16.2 kB

Release files / yield-from-as-an-iterator-1.2.0.tar.gz

Download URL yield-from-as-an-iterator-1.2.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fb1d268303dcb414c828446d3c14ecf3ad5ea1c9e0d5487b5752dc273ed16c77
BLAKE2b-256 checksum
How to use checksums
94bf8afcbc461e969df070be8dd8f28a93897e0a215ba21c8df7650768324546
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / yield_from_as_an_iterator-1.2.0-py26.py3-none-any.whl

Download URL yield_from_as_an_iterator-1.2.0-py26.py3-none-any.whl
Size 5.2 kB
Tags Python 2.6 Python 3
SHA-256 checksum
How to use checksums
7fd45882f581295035730fbd45d2f4ae7f6e56282bc8c1729f4286afd6b15129
BLAKE2b-256 checksum
How to use checksums
e95c0b3b208822630147d6df7b6f02e14fb6de19a4025b1c2114e39f3061d92e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / yield_from_as_an_iterator-1.2.0-py20-none-any.whl

Download URL yield_from_as_an_iterator-1.2.0-py20-none-any.whl
Size 5.2 kB
Tags Python 2.0
SHA-256 checksum
How to use checksums
0da36573d8628fd1aa834c770f5925d7f5e3ef2359c9c80dd26a43f6f9e1db72
BLAKE2b-256 checksum
How to use checksums
35b940e93cfc547f37a0497c0df31cb2d590157519650c2d2d38ed6631cbd8e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.2.0 This release

3 release files

1.1.4

3 release files

1.1.3

3 release files

1.1.2

3 release files

1.1.1

3 release files

1.1.0

3 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page