Re: more fun with iterators (mux, demux)

2009-04-08 Thread Raymond Hettinger
[Miles] > I assume that "smallish values of n" refers to the fact that > itertools.tee places items into every generator's internal deque, > which islice then skips over, whereas your version places items only > into the deque of the generator that needs it. The pure python equivalent listed in th

Re: more fun with iterators (mux, demux)

2009-04-08 Thread Miles
On Wed, Apr 8, 2009 at 1:21 PM, pataphor wrote: > On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote: > >> What was wrong with this one? >> >> def demux(iterable, n): >>     return tuple(islice(it, i, None, n) for (i, it) in >> enumerate(tee(iterable, n))) > > Nothing much, I only noticed after p

Re: more fun with iterators (mux, demux)

2009-04-08 Thread pataphor
On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote: > What was wrong with this one? > > def demux(iterable, n): > return tuple(islice(it, i, None, n) for (i, it) in > enumerate(tee(iterable, n))) Nothing much, I only noticed after posting that this one handles infinite sequences too. For

Re: more fun with iterators (mux, demux)

2009-04-08 Thread Neal Becker
pataphor wrote: > On 07 Apr 2009 02:05:59 GMT > Steven D'Aprano wrote: > >> The demuxer can't be an iterator, since it needs to run through the >> entire collection. > > Then your demuxer obviously cannot handle infinite sequences. > >> def demux(it, n): >> collectors = [[] for i in xrang

Re: more fun with iterators (mux, demux)

2009-04-08 Thread pataphor
On 07 Apr 2009 02:05:59 GMT Steven D'Aprano wrote: > The demuxer can't be an iterator, since it needs to run through the > entire collection. Then your demuxer obviously cannot handle infinite sequences. > def demux(it, n): > collectors = [[] for i in xrange(n)] > i = 0 > for item

Re: more fun with iterators (mux, demux)

2009-04-06 Thread Miles
On Mon, Apr 6, 2009 at 10:05 PM, Steven D'Aprano wrote: > On Mon, 06 Apr 2009 20:05:51 -0400, Neal Becker wrote: > >> I'm trying to make a multiplexor and demultiplexor, using generators. >> The multiplexor will multiplex N sequences -> 1 sequence  (assume equal >> length). The demultiplexor will d

Re: more fun with iterators (mux, demux)

2009-04-06 Thread Miles
On Mon, Apr 6, 2009 at 8:05 PM, Neal Becker wrote: > I'm trying to make a multiplexor and demultiplexor, using generators.  The > multiplexor will multiplex N sequences -> 1 sequence  (assume equal length). > The demultiplexor will do the inverse. > > The demux has me stumped.  The demux should ret

Re: more fun with iterators (mux, demux)

2009-04-06 Thread Steven D'Aprano
On Mon, 06 Apr 2009 20:05:51 -0400, Neal Becker wrote: > I'm trying to make a multiplexor and demultiplexor, using generators. > The multiplexor will multiplex N sequences -> 1 sequence (assume equal > length). The demultiplexor will do the inverse. > > The mux seems easy enough: > > -

more fun with iterators (mux, demux)

2009-04-06 Thread Neal Becker
I'm trying to make a multiplexor and demultiplexor, using generators. The multiplexor will multiplex N sequences -> 1 sequence (assume equal length). The demultiplexor will do the inverse. The mux seems easy enough: --- def mux (*ranges): iterables = [iter (r) for r i