PyWart: Itertools module needs attention

Ian Kelly ian.g.kelly at gmail.com
Tue Sep 13 17:45:03 CEST 2011


On Mon, Sep 12, 2011 at 4:04 PM, rantingrick <rantingrick at gmail.com> wrote:
>
> ############################################################
> #                          Quote                           #
> ############################################################
> # The itertools module is great HOWEVER i believe most     #
> # people are recreating the functionalities due to the     #
> # insanely cryptic and/or missing examples from each       #
> # method                                                   #
> ############################################################

Have you looked at the online itertools documentation at all?

http://docs.python.org/library/itertools.html

> py> ''.join(list(itertools.dropwhile(lambda x:x==" ", "    hello
> word    ")))
> 'hello word    '
> py> ''.join(list(itertools.takewhile(lambda x:x==" ", "    hello
> word    ")))
> '    '

These are too complex to be good examples.  Drop the lambda and
replace it with a built-in.  Also, str.join is perfectly capable of
taking an iterator as its argument.  There is no reason at all to
construct a list first.

> py> print itertools.compress.__doc__
> compress(data, selectors) --> iterator over selected data
> Return data elements corresponding to true selector elements.
> Forms a shorter iterator from selected data elements using the
> selectors to choose the data elements.
>
> ############################################################
> #                          Quote                           #
> ############################################################
> # WTF! Would you like to define a Python "selector". Could #
> # it be that we should be using "selector function" or     #
> # "predicate function" instead?                            #
> ############################################################

Notice that it says "selector elements", not "selector functions".
You have misconstrued what this function does.  Hint: it does not use
predicates at all.

I can agree though that this could probably use a simple example in
the doc string.



More information about the Python-list mailing list