Skip to main content

Add a RepeatableIterable type and a function to obtain it

Project description

python-repeatable-iterable

PyPI-version-badge Downloads-badge Code-style:black:badge Imports:isort:badge Typecheck:mypy:badge Linting:pylint:badge CodeFactor-badge CodeClimateMaintainability-badge Codacy-badge GitHub-top-language-badge GitHub-license-badge PyPI-python-version-badge GitHub-code-size-in-bytes-badge

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

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

something_else = RepeatableIterable(something)
foo(something_else)

After solution 2:

from python_repeatable_iterable import RepeatableIterable

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

foo(something)

If you develop something where you have no control on what another dev might give you as input, you have 2 possibilities:

  • hope for the best ;),
  • or harden your code to have less support work to do :).

This applies if you dev something that is:

  • closed source or open source,
  • available to everyone on the Internet, available only to customers or colleagues that you may personally know or not.

Solution 2 above is a nice solution with a reasonable performance cost :).

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-2.1.8.tar.gz (104.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_repeatable_iterable-2.1.8-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file python_repeatable_iterable-2.1.8.tar.gz.

File metadata

File hashes

Hashes for python_repeatable_iterable-2.1.8.tar.gz
Algorithm Hash digest
SHA256 0851055596ec500b5829215d0ab1dbb8179e6d8a6ae0117366745a56e576a501
MD5 cea5ccb37fff97f4fc35c9b4b7253af5
BLAKE2b-256 baa826244c4958a0d009fdfedc898e1b207b4cebe1a53b7f4b3666e1f2dad2b1

See more details on using hashes here.

File details

Details for the file python_repeatable_iterable-2.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for python_repeatable_iterable-2.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 174a25c69d9ac0769c0f82f5aa1988c91ec00cf665a535c8ecc4bb3bd6c5217c
MD5 6497104d705d42a3e6856340369d5500
BLAKE2b-256 9256fd66b5e6d37072cf0d1ab47c977a7e70b22d0b03f53560351d8360075e26

See more details on using hashes here.

Supported by

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