Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-05-14 Thread Franklin? Lee
The topic reminded me of Stephan Houben's streamtee. https://github.com/stephanh42/streamtee/blob/master/streamtee.py It was an attempt at a memory-efficient tee, but it turned out tee was efficient enough. It uses a thunk-like method and recursion to remove the need for an explicit linked list.

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-05-07 Thread Tim Peters
I posted this several weeks ago, just for fun - an obscure but surprisingly brief Python implementation of itertools.tee, sharing a single stream (as a singly linked list) among all the returned iterables. Didn't think about it again until today, when recent discussions of lexical scoping made me

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Steven D'Aprano
On Sun, Apr 15, 2018 at 08:35:51PM +0300, Serhiy Storchaka wrote: > I have ideas about implementing zero-overhead try/except, but I have > doubts that it is worth. The benefit seems too small. It is conventional wisdom that catching exceptions is expensive, and that in performance critical code

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Koos Zevenhoven
On Sun, Apr 15, 2018 at 11:55 PM, Chris Angelico wrote: > On Mon, Apr 16, 2018 at 6:46 AM, Koos Zevenhoven > wrote: > > Anyway, the whole linked list is unnecessary if the iterable can be > iterated > > over multiple times. But "tee" won't know when to do that. *That* is > what I > > call overhe

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Koos Zevenhoven
On Mon, Apr 16, 2018 at 12:06 AM, Tim Peters wrote: > [Koos Zevenhoven ] > > It's definitely possible to write the above in a more > > readable way, and FWIW I don't think it involves "assignments as > > expressions". > > Of course it is. The point was brevity and speed, not readability. > I

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Tim Peters
[Koos Zevenhoven ] > It's definitely possible to write the above in a more > readable way, and FWIW I don't think it involves "assignments as > expressions". Of course it is. The point was brevity and speed, not readability. It was presented partly as a puzzle :-) >> What I find kind of hila

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Chris Angelico
On Mon, Apr 16, 2018 at 6:46 AM, Koos Zevenhoven wrote: > Anyway, the whole linked list is unnecessary if the iterable can be iterated > over multiple times. But "tee" won't know when to do that. *That* is what I > call overhead (unless of course all the tee branches are consumed in an > interleav

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Koos Zevenhoven
On Sun, Apr 15, 2018 at 8:05 AM, Tim Peters wrote: ​[...]​ > Then I thought "this is stupid! Python already does reference > counting." Voila! Vast swaths of tedious code vanished, giving this > remarkably simple implementation: > > def mytee(xs, n): > last = [None, None] > >

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Serhiy Storchaka
15.04.18 19:52, Tim Peters пише: No, I don't ;-) If I have to catch StopIteration myself now, then I want the entire "white True:" loop in the "try" block. Setting up try/except machinery anew on each iteration would add significant overhead; doing it just once per derived generator wouldn't.

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Tim Peters
[Antoine Pitrou ] > This implementation doesn't work with Python 3.7 or 3.8. > I've tried it here: > https://gist.github.com/pitrou/b3991f638300edb6d06b5be23a4c66d6 > > and get: > Traceback (most recent call last): > File "mytee.py", line 14, in gen > mylast = last[1] = last = [next(it), None

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Antoine Pitrou
On Sun, 15 Apr 2018 00:05:58 -0500 Tim Peters wrote: > Just for fun - no complaint, no suggestion, just sharing a bit of code > that tickled me. > > The docs for `itertools.tee()` contain a Python work-alike, which is > easy to follow. It gives each derived generator its own deque, and > when a

[Python-ideas] A cute Python implementation of itertools.tee

2018-04-14 Thread Tim Peters
Just for fun - no complaint, no suggestion, just sharing a bit of code that tickled me. The docs for `itertools.tee()` contain a Python work-alike, which is easy to follow. It gives each derived generator its own deque, and when a new value is obtained from the original iterator it pushes that va