itertools candidate: warehouse()

Robert Brewer fumanchu at amor.org
Tue Oct 19 19:53:05 CEST 2004


Raymond Hettinger wrote:
> > "Robert Brewer" <fumanchu at amor.org> wrote ...
> > 
> > def warehouse(stock, factory=None):
> 
> Here are some random thoughts based on a first read through:
> 
> * The basic idea of iterating stock and then calling a 
> factory when the
> stock is depleted is already expressible in terms of itertools.  For
> example, if random.random() is the factory:
> 
>    iavailable = chain(stock, starmap(random.random, repeat(())))

Ah, thanks! The docs don't really make it clear that you can pass a
callable to repeat() and have it be called on each iteration. The Python
"equivalent" given in the docs looks like you get the same static object
every time... :/

> * The second iterator, iremainder, ventures well beyond the basic idea
> of a warehouse/factory chain.  It pushes the learning curve up steeply
> and makes the function less generic (i.e. plenty of situations that
> could use the warehouse/factory chain have no need for the separate
> iremainder iterator).

True. Dang, I wish generators could take attributes; I would've attached
iremainder there instead and reduced the learning curve a bit for those
who don't need that feature.

> * Making the factory argument optional is, IMO, also trying to do too
> much.  Using the first stock item to identify a type and then calling
> the type is probably more appropriate for prototype based OO 
> (i.e. with objects designed for cloning operations).  Also, the number

> of potential uses is limited because knowing the type does not imply
> that you know the signature for the constructor or that you can infer 
> argument values for constructors that take arguments.

The uses are limited only when you do not supply the factory argument;
when I need constructor arguments, I make a factory function for the
class and pass that for the 'factory' arg.

> * This function has not seen real world use outside your organization.

Right.

> Posting it as an ASPN recipe might give it a chance to be 
> kicked around a bit and perhaps re-emerge in a more mature form.

The cookbook is a good idea (this makes a better recipe than a module
func; I agree 100%). But I don't find ASPN a place which stimulates
"kicking things around", because there's no critical community mass.
c.l.p is still better for that, IMO.

> * Most of the itertools and their kin only try to encapsulate 
> a tiny bit
> of logic (enumerate and takewhile for example).  Even the most complex
> tool, groupby(), is built around an atomic idea (roughly akin to the
> uniq filter in Unix uniq).  The bite sized approach enables 
> itertools to
> simplify code without obscuring the program logic.  IMO, the proposed
> warehouse() function does not share that advantage (though it is
> possible that repetition may allow ones mind to eventually 
> condense the
> logic into a managable unit of thought).

Agreed. Thanks for the feedback!


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list