469,951 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

A 'Box' Function

Hey guys.

I should warn you, first off, that I'm relatively new to Python.
Basically, what I'm trying to do is create a word-wrapping function
with the added complication that it add a character at the beginning
and end of each line, so that it encloses the text in a sort of 'box':

----------------
| Like this |
----------------

The word-wrapping function I'm working with is similar to the one given
here:
http://aspn.activestate.com/ASPN/Coo.../Recipe/148061

def wrap(text, width):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
' \n'[(len(line)-line.rfind('\n')-1
+ len(word.split('\n',1)[0]
) >= width)],
word),
text.split(' ')
)

Does anyone have any ideas on how it could be modified? Am I
approaching it the right way? Thanks a bunch!

Mike

Apr 18 '06 #1
2 2986

<mi********@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
The word-wrapping function I'm working with is


I have never used this, but it might help you
import textwrap
dir(textwrap)

['TextWrapper', '__all__', '__builtins__', '__doc__', '__file__',
'__name__', '__revision__', '_whitespace', 'dedent', 'fill', 're',
'string', 'wrap']

tjr


Apr 18 '06 #2
mi********@gmail.com wrote:
Hey guys.

I should warn you, first off, that I'm relatively new to Python.
Basically, what I'm trying to do is create a word-wrapping function
with the added complication that it add a character at the beginning
and end of each line, so that it encloses the text in a sort of 'box':

----------------
| Like this |
----------------

The word-wrapping function I'm working with is similar to the one given
here:
http://aspn.activestate.com/ASPN/Coo.../Recipe/148061

Does anyone have any ideas on how it could be modified? Am I
approaching it the right way? Thanks a bunch!


Rather than rewriting wrap(), I would suggest wrapping it with a new
function, wrap_box(), that adds the header, line endings and trailer to
the lines returned from wrap().

Kent
Apr 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by mr_burns | last post: by
2 posts views Thread by Microsoft News | last post: by
1 post views Thread by The Eclectic Electric | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.