micropython / micropython/micropython-lib

itertools.tee wrongly programmed

Open
#580 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

The crux of the tee() function of itertools is that in duplicates/multiplies the generator so that you have independent generator copies: If one is depleated (StopIteration Exception) the others still are able to generate the sequence.

MPs itertools tee definition:

def tee(iterable, n=2):
    return [iter(iterable)] * n

does not achieve this.
My very first example from the book "Clean Code in Python" by Mariano Anaya using tee thus lead to an error.

Circuitpythons itertools practically has the same definition. -> same problem.

tee in CPython has to store the objects that some, but not all of the generator copies have produced. It is coded in C.
I see no other way to replicate this behaviour than to grow a list of those values. A deque with the option to retrieve intermediate objects would be optimal, which we do not have?!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the itertools tee() definition shown in the issue and compare its behavior with CPython's tee implementation. Reproduce the case where one iterator is exhausted before the others; done means the returned iterators remain independent and can continue producing the sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.