textwrap.py wordsep_re

Hi, textwrap.fill() is awesome. Except when the string to wrap contains dates -- which I would like not to be filled. In general I think wordsep_re can be smarter about what it decides are hyphenated words. For example, this code: print textwrap.fill('aaaaaaaaaa 2005-02-21', 18) produces: aaaaaaaaaa 2005- 02-21 A slightly tweaked wordsep_re: textwrap.TextWrapper.wordsep_re =\ re.compile(r'(\s+|' # any whitespace r'[^\s\w]*\w+[a-zA-Z]-(?=[a-zA-Z]\w+)|' # hyphenated words r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash print textwrap.fill('aaaaaaaaaa 2005-02-21', 18) behaves better: aaaaaaaaaa 2005-02-21 What do you think about changing the default wordsep_re? -- Karl 2005-02-21 03:32

On 21 February 2005, Karl Chen said:
Except when the string to wrap contains dates -- which I would like not to be filled. In general I think wordsep_re can be smarter about what it decides are hyphenated words.
For example, this code: print textwrap.fill('aaaaaaaaaa 2005-02-21', 18) produces: aaaaaaaaaa 2005- 02-21
Oops!
A slightly tweaked wordsep_re: textwrap.TextWrapper.wordsep_re =\ re.compile(r'(\s+|' # any whitespace r'[^\s\w]*\w+[a-zA-Z]-(?=[a-zA-Z]\w+)|' # hyphenated words r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash print textwrap.fill('aaaaaaaaaa 2005-02-21', 18) behaves better: aaaaaaaaaa 2005-02-21
Post a patch to SF and assign it to me. Make sure the unit tests still pass, and add a new one that doesn't pass without your fix. Pester me mercilessly until I act on it. (I think your change is probably fine, but I need more time to examine it than I have right now.) Greg -- Greg Ward <gward@python.net> http://www.gerg.ca/ Cheops' Law: Nothing *ever* gets built on schedule or within budget.

On 2005-02-24 05:04 PST, Greg Ward writes:
Greg> Post a patch to SF and assign it to me. Make sure the Greg> unit tests still pass, and add a new one that doesn't Greg> pass without your fix. Pester me mercilessly until I Greg> act on it. (I think your change is probably fine, but I Greg> need more time to examine it than I have right now.) I had already posted a patch on Aahz's advice. I'll write a unit test. -- Karl 2005-02-24 13:18
participants (2)
-
Greg Ward
-
Karl Chen