Skip to content

mentalisttraceur/python-yieldfrom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python yield from as an Iterator

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

Portable down to Python 2.2 if the GeneratorExit exception is polyfilled or not used, but without bidirectional yield you'll need to adjust the replacement code above.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published