itertools candidate: warehouse()

Peter Otten __peter__ at web.de
Wed Oct 20 17:48:09 CEST 2004


Robert Brewer wrote:

> 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. ;)

Neither is warehouse(). But I get your point. 
And now, just for fun, a 2.3-compliant version:

>>> from itertools import *
>>> def peek(iterable):
...     it = iter(iterable)
...     try:
...             first = it.next()
...     except StopIteration:
...             raise ValueError("cannot peek into an empty iterable")
...     return chain([first], it), first
...
>>> data, first = peek("abc")
>>> first, list(data)
('a', ['a', 'b', 'c'])
 
>> 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,

Sorry that was confusing. I meant that the functions from the recipes page
in the documentation should also be available as a module in the
distribution, perhaps with a warning that they might change "without prior
notice".

> 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. :/

I think you're right. Sometimes, however, you have the chance to build the
higher-level stuff from those nifty itertools functions that do only one
thing, but fast. And that should be as much fun as the interpreter etudes
I've come to like.

Peter






More information about the Python-list mailing list