[Python-ideas] textwrap.TextWrapper width=None (or Inf)

Steven D'Aprano steve at pearwood.info
Fri Dec 27 00:17:44 CET 2013


On Thu, Dec 26, 2013 at 11:17:34PM +0100, Matej Cepl wrote:
> Hi,
> 
> I wonder why nobody asked on bugs.python.org for rather obvious 
> functionality of being able to reflow a paragraph to one line?  

Possibly because nobody needed the functionality?

Or because they didn't think that *wrapping* a long line into a 
paragraph and *unwrapping* a paragraph into a single line should be 
handled by the same function?


> Meaning, that any paragraph would be stripped of all whitespace 
> (etc. ... whatever is configured by the additional parameters of 
> the TextWrapper class) and then joined into long line. I know 
> that
> 
>    ''.join(text.splitlines())
>    
> does something similar, but

I would expect that you should use ' '.join, rather than the empty 
string. Otherwises lines will be incorrectly concatenated:

"""the cat in
the hat"""

=> "the cat inthe hat"


>    a) it doesn't handle all whitespace munging,

Can you given an example of what whitespace munging it fails to handle?


>    b) it just seems like an obvious functionality for 
>    TextWrapper to have.

py> text = """the cat in
... the hat"""
py> textwrap.wrap(text, width=len(text))
['the cat in the hat']

Is there a case that this does not handle?

Perhaps this is not obvious enough. A simple helper function may 
increase discoverability:

def unwrap(text):
    return wrap(text, width=len(text))



-- 
Steven


More information about the Python-ideas mailing list