[Pythoncard-users] wordwrap function added to util.py module
by Kevin Altis other posts by this author
Dec 19 2002 6:31PM messages near this date
[Pythoncard-users] case-insensitive sort added to util.py module
|
[Pythoncard-users] added Lexicon window to Life sample
Mike Brown <mike@[...].org> fixed the bug I found while generating the sample
pages. The version that appears on ASPN currently has some os.linesep code
that I don't consider useful and may introduce other problems, so I decided
to put a slightly simplified version in the PythonCardPrototype\util.py
module. This is a very useful function.
If you are trying to word wrap and don't want to preserve linefeeds then you
can simply convert linefeeds to spaces prior to calling this function. You
may need to need to do \n\n to \n reductions to avoid double-spaces in the
final output since the function preserves spaces. If someone has a
suggestion for a different algorithm that doesn't preserve linefeeds, then
we could add a preserve flag to the args list and an if statement to use the
appropriate word wrap block of code based on the preserve variable.
ka
---
def wordwrap(text, width):
"""word-wrap function that preserves line breaks and spaces
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061"""
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
' \n'[(len(line[line.rfind('\n')+1:])
+ len(word.split('\n')[0]
) > = width)],
word),
text.split(' ')
)
-------------------------------------------------------
This SF.NET email is sponsored by: Geek Gift Procrastinating?
Get the perfect geek gift now! Before the Holidays pass you by.
T H I N K G E E K . C O M http://www.thinkgeek.com/sf/
_______________________________________________
Pythoncard-users mailing list
Pythoncard-users@[...].net
https://lists.sourceforge.net/lists/listinfo/pythoncard-users
|