[Python-Dev] Re: Where to put wrap_text()?

François Pinard pinard@iro.umontreal.ca
02 Jun 2002 09:09:57 -0400

[Greg Ward]

> Do you have a reference for this algorithm apart from GNU fmt's source
> code?

Surely not handy.  I heard about it, and others even more capable, many
years ago.  If I remember well, Knuth's algorithm plays by moving line
cuts and optimising a global function through dynamic programming, giving
more points, say, when punctuation coincides with end of lines, removing
points when a single letter words appear at end of lines, and such thing.
So lines are not guaranteed to be as filled as possible, but the overall
appearance of the paragraph gets better, sometimes much better.

I'm Cc:ing Ross Paterson, who wrote GNU `fmt', in hope he could shed some
light about references, or otherwise.

Some filling algorithms used by typographers (or so I heard) are even
careful about dismantling vertical or diagonal (aliased) white lines which
sometimes build up across paragraphs by the effect of dumbier filling.

> I'm leaning towards a TextWrapper class, so that everyone may impose
> their desires through subclassing.

Distutils experience speaking here? :-)

By the way, I would like if the module was not named `text'.  I use `text'
all over in my programs already as a common variable name, as a way to not
use `string' for a common variable name, for obvious reasons.  Granted that
`string' is progressively becoming available again :-).  Maybe Python should
try to not name modules with likely to be use-everywhere local variables.

> Not if it grows to accomodate Optik/OptionParser, Mailman, regrtest,
> etc.

At some places in my things, I have unusual wrapping/filling needs, and
wonder if they could all fit in a generic scheme.  An interesting question
and exercise, surely.

François Pinard   http://www.iro.umontreal.ca/~pinard