Candidate for a new itertool

Raymond Hettinger python at rcn.com
Mon Mar 9 04:12:01 EDT 2009


On Mar 7, 7:58 pm, bearophileH... at lycos.com wrote:
> Raymond Hettinger, maybe it can be useful to add an optional argument
> flag to tell such split_on to keep the separators or not? This is the
> xsplit I usually use:

In your experiences with xsplit(), do most use cases involve removing
the separators?

My thought is that if you know the separator is at the beginning, it
is easy to strip it off with a next() on the subgroup iterator.
If so, then there's no reason to complicate the API with another
set of options.

Also, separation events do not necessarily tie themselves to specific
values that can be throw-away.  Perhaps, the event detector is looking
for the number of lines written on a page and generates a new page
event every time a page is full.


> Maybe it's better to separate or denote the separators in some way?

Probably not.  Itertools work together best when a stream of tokens
all have the same meaning.  Otherwise, you end-up complicating
consumer
code with something like:

   for val in split(...):
        if val == separator:
            do_onething(val)
        else:
            do_another(val)



> Is it useful to merge successive separators (notice two X)?

Probably depends on real-world use cases.  Do you have any?



> Is a name like isplitter or splitter better this itertool?

Like groupbyer, filterer, teer, starmapper, and chainer ;-)



Raymond




More information about the Python-list mailing list