itertools candidate: warehouse()

Robert Brewer fumanchu at amor.org
Tue Oct 19 18:50:42 CEST 2004


Peter Otten wrote:
> Robert Brewer wrote:
> > def warehouse(stock, factory=None):
> 
> Most of the building blocks for the warehouse() are already 
> there, but you didn't use them, oddly enough.

Oddly enough, tee isn't in my version (2.3.2) of Python except as an
example. ;)
Thanks for the exhaustive discussion below.

> So here comes a variant written in terms of current (2.4) itertools:
> 
> >>> from itertools import *
> >>> def peek(iterable):
> ...     a, b = tee(iterable)
> ...     try:
> ...             return a, b.next()
> ...     except StopIteration:
> ...             raise ValueError("cannot peek into an empty iterable")
> ...
> >>> iterable = iter("abc")
> >>> iterable, first = peek(iterable)
> >>> factory = first.__class__
> >>> data = range(10)
> >>> for a, b in izip(data, chain(iterable, starmap(factory, 
> repeat(())))):
> ...     print a, repr(b)
> ...
> 0 'a'
> 1 'b'
> 2 'c'
> 3 ''
> 4 ''
> 5 ''
> 6 ''
> 7 ''
> 8 ''
> 9 ''

[snip]

> So now you can do
> 
> chain(iterable, repeatfunc(factory))
> 
> after putting a copy of these recipes into your 
> site-packages. Why aren't they already there, btw?

I'm not sure which particular recipes you mean (peek?). But in general,
I don't write scripts for my own limited use; I'm writing frameworks,
which shouldn't depend upon little recipes scattered hither and yon. :/

> The only extra your warehouse() has to offer is the (last) 
> item's class as the default factory for the padding items.
> I don't think that is needed often enough to warrant the
> inclusion in the itertools.

Understood. I think Raymond is right; at most, it should be a Cookbook
entry.

Actually, the big "extra" I see with warehouse() is wrapping up all of
the iteration cruft(combined with the return of the inner iterator to
the caller, who can then clean up any remaining items). Having seen your
alternative, I can see it basically comes down to readability for me; I
find warehouse() to be a nice packaging of a common idiom for my apps.
I'm the only programmer here at my organization; sometimes I just need a
"guy down the hall" to keep me in line. :) Thanks for doing that.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list