Skip to main content

Add a RepeatableIterable type and a function to obtain it

Project description

python-repeatable-iterable

pypi-version

A new type RepeatableIterable for Python and a way to obtain one instance

Since in Python an Iterator is an Iterable and that you cannot iterate multiple times on an iterator, you may encounter WTF bugs, even with type checking. This package provides possible solutions to this problem. See here for a discussion on this problem: https://stackoverflow.com/questions/63104689/what-is-the-pythonic-way-to-represent-an-iterable-that-can-be-iterated-over-mult

Before:

def foo(iterable: Iterable):
    for that in iterable:
        bar(that)
    for that in iterable:
        # possible bug
        baz(that)

foo(something)

After solution 1:

from python_repeatable_iterable import RepeatableIterable, get_repeatable_iterable

def foo(iterable: RepeatableIterable):
    for that in iterable:
        bar(that)
    for that in iterable:
        baz(that)

something_else = get_repeatable_iterable(something)
foo(something_else)

After solution 2:

from python_repeatable_iterable import get_repeatable_iterable

def foo(iterable: Iterable):
    iterable = get_repeatable_iterable(iterable)
    for that in iterable:
        bar(that)
    for that in iterable:
        baz(that)

foo(something)

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

python_repeatable_iterable-1.1.0.tar.gz (16.8 kB view hashes)

Uploaded Source

Built Distribution

python_repeatable_iterable-1.1.0-py3-none-any.whl (20.5 kB view hashes)

Uploaded Python 3

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