In need of a binge-and-purge idiom

Magnus Lie Hetland mlh at furu.idi.ntnu.no
Mon Mar 24 19:36:20 EST 2003


In article <YoCfa.960$i26.18361 at news2.tin.it>, Alex Martelli wrote:
>Magnus Lie Hetland wrote:
>
>Ah, I recognize the outline of our joint contribution to the
>printed Cookbook (recipe 4.8...).

:)

I use this sort of thing in the "Instant Markup" chapter of my book as
well. But I haven't found a nice solution, except artificially tucking
an extra separator onto the iterator. Maybe that's nice enough,
though...

>> I've noticed that I use the following in several contexts:

>   [fixing as per followups]

Good :)

[snip]
>First refactoring that comes to mind is:

Yes. I thought of refactoring as a solution too. It does reduce the
duplication to a duplicated function call -- which may be good enough.
I just sort of hoped I could avoid it altogether :]

>but this wouldn't work for the specific use case you require:
>
>> If the iterable above is a file, isSeparator(element) is simply
>> defined as not element.strip() and doSomething(chunk) is
>> yield(''.join(chunk)) you have a paragraph splitter. I've been using
>
>i.e., factoring out a *yield* to maydosomething would NOT work.

Now -- the yield isn't important. I could very well update a list
instead. (Although it would be nice if the yield-thing were possible
too...)

>So I'll focus on the specific case of yield in the following,
>assuming a "munge" function such as
>def munge(chunk): return ''.join(chunk)
>is also passed as an argument.

OK.

[snip]
>Indeed, there ain't much "fiddling" needed at all

But some, though ;)

> -- you just
>DO need to know SOME acceptable separator, however:

Hm. Yeah -- that's sort of the thing I don't really like, I guess.
(It's a pretty vague feeling, though ;)

>import itertools
>
>    for element in itertools.chain(iterable, [aSeparator]):

Yup. This was discussed separately in another thread, where I
suggested some related tools to itertools (and got the above as a
suggested alternative).

This is, as I mentioned, more or less what I did in the "Instant
Markup" thing. I simply used something along the lines of

  def lines(file):
      for line in file:
          yield line
      yield '\n'

and then iterated over that when producing paragraphs.

[snip]
>Elegance is in the eye of the beholder, but...:

Indeed. My notion of elegance has been known to be a bit superficial
at times ;)

[snip]

I'm actually using something very similar in another context (where I
need to know wheter two thingies of the same kind are next to each
other :)

>...I've had occasion to use variants of this in order to be able
>to peek ahead, check if an iterator was done, or in small further
>variants to give an iterator one level of "pushback", etc, etc.

Indeed. Perhaps some simple, general version of this (maybe even like
the one you described above -- although perhaps with a slightly more
pithy name? -- might be a candidate for itertools? Lookahead can be
useful in many cases (such as when writing a parser, for instance :)

>So, if you have a wrapper such as this one around somewhere, you
>might choose to reuse it (though it probably wouldn't be worth
>developing for the sole purpose of this use!-):

Indeed...

[snip]
>        if issep or it.done: 

Yes -- this is exactly what I'm missing, I suppose.

>> I can't really see any good way of using the while/break idiom
>> either,
>
>Well, you COULD use a different wrapper class to obtain code such as:

Yeah... If I was to write a wrapper class in the first place, I could
do pretty much anything, I suppose :]

>but the wrapper wouldn't be all that nice under the covers AND it
>would in practice have to embody a bit too much of the control
>logic and bury it in a non-obvious place -- so I wouldn't pursue
>this tack, myself.

Agreed.

I guess what it all boils down to is that it would be nice to know
whether one is in the last iteration of a for loop. Sadly, I see no
way of doing that in the general case.

>Alex

-- 
Magnus Lie Hetland               "Nothing shocks me. I'm a scientist." 
http://hetland.org                                   -- Indiana Jones




More information about the Python-list mailing list