A 'Box' Function

M

mikelaskey

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/Cookbook/Python/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
 
T

Terry Reedy

The word-wrapping function I'm working with is

I have never used this, but it might help you
['TextWrapper', '__all__', '__builtins__', '__doc__', '__file__',
'__name__', '__revision__', '_whitespace', 'dedent', 'fill', 're',
'string', 'wrap']

tjr
 
K

Kent Johnson

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/Cookbook/Python/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,508
Members
48,193
Latest member
DannyRober

Latest Threads

Top