2007-08-15 11:28:22 -03:00
|
|
|
:mod:`textwrap` --- Text wrapping and filling
|
|
|
|
=============================================
|
|
|
|
|
|
|
|
.. module:: textwrap
|
|
|
|
:synopsis: Text wrapping and filling
|
|
|
|
.. moduleauthor:: Greg Ward <gward@python.net>
|
|
|
|
.. sectionauthor:: Greg Ward <gward@python.net>
|
|
|
|
|
|
|
|
|
|
|
|
The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and
|
|
|
|
:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work,
|
|
|
|
and a utility function :func:`dedent`. If you're just wrapping or filling one
|
|
|
|
or two text strings, the convenience functions should be good enough;
|
|
|
|
otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
|
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: wrap(text, width=70, **kwargs)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
Wraps the single paragraph in *text* (a string) so every line is at most
|
|
|
|
*width* characters long. Returns a list of output lines, without final
|
|
|
|
newlines.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Optional keyword arguments correspond to the instance attributes of
|
|
|
|
:class:`TextWrapper`, documented below. *width* defaults to ``70``.
|
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: fill(text, width=70, **kwargs)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Wraps the single paragraph in *text*, and returns a single string containing the
|
|
|
|
wrapped paragraph. :func:`fill` is shorthand for ::
|
|
|
|
|
|
|
|
"\n".join(wrap(text, ...))
|
|
|
|
|
|
|
|
In particular, :func:`fill` accepts exactly the same keyword arguments as
|
|
|
|
:func:`wrap`.
|
|
|
|
|
|
|
|
Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
|
|
|
|
instance and calling a single method on it. That instance is not reused, so for
|
|
|
|
applications that wrap/fill many text strings, it will be more efficient for you
|
|
|
|
to create your own :class:`TextWrapper` object.
|
|
|
|
|
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62998 | andrew.kuchling | 2008-05-10 15:51:55 -0400 (Sat, 10 May 2008) | 7 lines
#1858 from Tarek Ziade:
Allow multiple repositories in .pypirc; see http://wiki.python.org/moin/EnhancedPyPI
for discussion.
The patch is slightly revised from Tarek's last patch: I've simplified
the PyPIRCCommand.finalize_options() method to not look at sys.argv.
Tests still pass.
........
r63000 | alexandre.vassalotti | 2008-05-10 15:59:16 -0400 (Sat, 10 May 2008) | 5 lines
Cleaned up io._BytesIO.write().
I am amazed that the old code, for inserting null-bytes, actually
worked. Who wrote that thing? Oh, it is me... doh.
........
r63002 | brett.cannon | 2008-05-10 16:52:01 -0400 (Sat, 10 May 2008) | 2 lines
Revert r62998 as it broke the build (seems distutils.config is missing).
........
r63014 | andrew.kuchling | 2008-05-10 18:12:38 -0400 (Sat, 10 May 2008) | 1 line
#1858: add distutils.config module
........
r63027 | brett.cannon | 2008-05-10 21:09:32 -0400 (Sat, 10 May 2008) | 2 lines
Flesh out the 3.0 deprecation to suggest using the ctypes module.
........
r63028 | skip.montanaro | 2008-05-10 22:59:30 -0400 (Sat, 10 May 2008) | 4 lines
Copied two versions of the example from the interactive session. Delete
one.
........
r63037 | georg.brandl | 2008-05-11 03:02:17 -0400 (Sun, 11 May 2008) | 2 lines
reload() takes the module itself.
........
r63038 | alexandre.vassalotti | 2008-05-11 03:06:04 -0400 (Sun, 11 May 2008) | 4 lines
Added test framework for handling module renames.
Factored the import guard in test_py3kwarn.TestStdlibRemovals into
a context manager, namely test_support.CleanImport.
........
r63039 | georg.brandl | 2008-05-11 03:06:05 -0400 (Sun, 11 May 2008) | 2 lines
#2742: ``''`` is not converted to NULL in getaddrinfo.
........
r63040 | alexandre.vassalotti | 2008-05-11 03:08:12 -0400 (Sun, 11 May 2008) | 2 lines
Fixed typo in a comment of test_support.CleanImport.
........
r63041 | alexandre.vassalotti | 2008-05-11 03:10:25 -0400 (Sun, 11 May 2008) | 2 lines
Removed a dead line of code.
........
r63043 | georg.brandl | 2008-05-11 04:47:53 -0400 (Sun, 11 May 2008) | 2 lines
#2812: document property.getter/setter/deleter.
........
r63049 | georg.brandl | 2008-05-11 05:06:30 -0400 (Sun, 11 May 2008) | 2 lines
#1153769: document PEP 237 changes to string formatting.
........
r63050 | georg.brandl | 2008-05-11 05:11:40 -0400 (Sun, 11 May 2008) | 2 lines
#2809: elaborate str.split docstring a bit.
........
r63051 | georg.brandl | 2008-05-11 06:13:59 -0400 (Sun, 11 May 2008) | 2 lines
Fix typo.
........
r63052 | georg.brandl | 2008-05-11 06:33:27 -0400 (Sun, 11 May 2008) | 2 lines
#2709: clarification.
........
r63053 | georg.brandl | 2008-05-11 06:42:28 -0400 (Sun, 11 May 2008) | 2 lines
#2659: add ``break_on_hyphens`` to TextWrapper.
........
r63057 | georg.brandl | 2008-05-11 06:59:39 -0400 (Sun, 11 May 2008) | 2 lines
#2741: clarification of value range for address_family.
........
r63058 | georg.brandl | 2008-05-11 07:09:35 -0400 (Sun, 11 May 2008) | 2 lines
#2452: timeout is used for all blocking operations.
........
r63059 | andrew.kuchling | 2008-05-11 09:33:56 -0400 (Sun, 11 May 2008) | 2 lines
#1792: Improve performance of marshal.dumps() on large objects by increasing
the size of the buffer more quickly.
........
r63060 | andrew.kuchling | 2008-05-11 10:00:00 -0400 (Sun, 11 May 2008) | 1 line
#1858: re-apply patch for this, adding the missing files
........
r63061 | benjamin.peterson | 2008-05-11 10:13:25 -0400 (Sun, 11 May 2008) | 2 lines
Add the "until" command to pdb
........
r63062 | georg.brandl | 2008-05-11 10:17:13 -0400 (Sun, 11 May 2008) | 2 lines
Add some sentence endings.
........
2008-05-15 21:03:33 -03:00
|
|
|
Text is preferably wrapped on whitespaces and right after the hyphens in
|
|
|
|
hyphenated words; only then will long words be broken if necessary, unless
|
|
|
|
:attr:`TextWrapper.break_long_words` is set to false.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
An additional utility function, :func:`dedent`, is provided to remove
|
|
|
|
indentation from strings that have unwanted whitespace to the left of the text.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: dedent(text)
|
|
|
|
|
|
|
|
Remove any common leading whitespace from every line in *text*.
|
|
|
|
|
|
|
|
This can be used to make triple-quoted strings line up with the left edge of the
|
|
|
|
display, while still presenting them in the source code in indented form.
|
|
|
|
|
|
|
|
Note that tabs and spaces are both treated as whitespace, but they are not
|
|
|
|
equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
|
2008-05-12 15:05:20 -03:00
|
|
|
common leading whitespace.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
def test():
|
|
|
|
# end first line with \ to avoid the empty line!
|
|
|
|
s = '''\
|
|
|
|
hello
|
|
|
|
world
|
|
|
|
'''
|
2007-09-01 20:34:30 -03:00
|
|
|
print(repr(s)) # prints ' hello\n world\n '
|
|
|
|
print(repr(dedent(s))) # prints 'hello\n world\n'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. class:: TextWrapper(**kwargs)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The :class:`TextWrapper` constructor accepts a number of optional keyword
|
2009-09-16 12:58:14 -03:00
|
|
|
arguments. Each keyword argument corresponds to an instance attribute, so
|
|
|
|
for example ::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
wrapper = TextWrapper(initial_indent="* ")
|
|
|
|
|
|
|
|
is the same as ::
|
|
|
|
|
|
|
|
wrapper = TextWrapper()
|
|
|
|
wrapper.initial_indent = "* "
|
|
|
|
|
|
|
|
You can re-use the same :class:`TextWrapper` object many times, and you can
|
|
|
|
change any of its options through direct assignment to instance attributes
|
|
|
|
between uses.
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
The :class:`TextWrapper` instance attributes (and keyword arguments to the
|
|
|
|
constructor) are as follows:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: width
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``70``) The maximum length of wrapped lines. As long as there
|
|
|
|
are no individual words in the input text longer than :attr:`width`,
|
|
|
|
:class:`TextWrapper` guarantees that no output line will be longer than
|
|
|
|
:attr:`width` characters.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: expand_tabs
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``True``) If true, then all tab characters in *text* will be
|
|
|
|
expanded to spaces using the :meth:`expandtabs` method of *text*.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: replace_whitespace
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``True``) If true, each whitespace character (as defined by
|
|
|
|
``string.whitespace``) remaining after tab expansion will be replaced by a
|
|
|
|
single space.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. note::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
|
|
|
|
each tab character will be replaced by a single space, which is *not*
|
|
|
|
the same as tab expansion.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: drop_whitespace
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``True``) If true, whitespace that, after wrapping, happens to
|
|
|
|
end up at the beginning or end of a line is dropped (leading whitespace in
|
|
|
|
the first line is always preserved, though).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: initial_indent
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``''``) String that will be prepended to the first line of
|
|
|
|
wrapped output. Counts towards the length of the first line.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: subsequent_indent
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``''``) String that will be prepended to all lines of wrapped
|
|
|
|
output except the first. Counts towards the length of each line except
|
|
|
|
the first.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: fix_sentence_endings
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``False``) If true, :class:`TextWrapper` attempts to detect
|
|
|
|
sentence endings and ensure that sentences are always separated by exactly
|
|
|
|
two spaces. This is generally desired for text in a monospaced font.
|
|
|
|
However, the sentence detection algorithm is imperfect: it assumes that a
|
|
|
|
sentence ending consists of a lowercase letter followed by one of ``'.'``,
|
|
|
|
``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
|
|
|
|
followed by a space. One problem with this is algorithm is that it is
|
|
|
|
unable to detect the difference between "Dr." in ::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
[...] Dr. Frankenstein's monster [...]
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
and "Spot." in ::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
[...] See Spot. See Spot run [...]
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
:attr:`fix_sentence_endings` is false by default.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
Since the sentence detection algorithm relies on ``string.lowercase`` for
|
|
|
|
the definition of "lowercase letter," and a convention of using two spaces
|
|
|
|
after a period to separate sentences on the same line, it is specific to
|
|
|
|
English-language texts.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. attribute:: break_long_words
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
(default: ``True``) If true, then words longer than :attr:`width` will be
|
|
|
|
broken in order to ensure that no lines are longer than :attr:`width`. If
|
|
|
|
it is false, long words will not be broken, and some lines may be longer
|
|
|
|
than :attr:`width`. (Long words will be put on a line by themselves, in
|
|
|
|
order to minimize the amount by which :attr:`width` is exceeded.)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62998 | andrew.kuchling | 2008-05-10 15:51:55 -0400 (Sat, 10 May 2008) | 7 lines
#1858 from Tarek Ziade:
Allow multiple repositories in .pypirc; see http://wiki.python.org/moin/EnhancedPyPI
for discussion.
The patch is slightly revised from Tarek's last patch: I've simplified
the PyPIRCCommand.finalize_options() method to not look at sys.argv.
Tests still pass.
........
r63000 | alexandre.vassalotti | 2008-05-10 15:59:16 -0400 (Sat, 10 May 2008) | 5 lines
Cleaned up io._BytesIO.write().
I am amazed that the old code, for inserting null-bytes, actually
worked. Who wrote that thing? Oh, it is me... doh.
........
r63002 | brett.cannon | 2008-05-10 16:52:01 -0400 (Sat, 10 May 2008) | 2 lines
Revert r62998 as it broke the build (seems distutils.config is missing).
........
r63014 | andrew.kuchling | 2008-05-10 18:12:38 -0400 (Sat, 10 May 2008) | 1 line
#1858: add distutils.config module
........
r63027 | brett.cannon | 2008-05-10 21:09:32 -0400 (Sat, 10 May 2008) | 2 lines
Flesh out the 3.0 deprecation to suggest using the ctypes module.
........
r63028 | skip.montanaro | 2008-05-10 22:59:30 -0400 (Sat, 10 May 2008) | 4 lines
Copied two versions of the example from the interactive session. Delete
one.
........
r63037 | georg.brandl | 2008-05-11 03:02:17 -0400 (Sun, 11 May 2008) | 2 lines
reload() takes the module itself.
........
r63038 | alexandre.vassalotti | 2008-05-11 03:06:04 -0400 (Sun, 11 May 2008) | 4 lines
Added test framework for handling module renames.
Factored the import guard in test_py3kwarn.TestStdlibRemovals into
a context manager, namely test_support.CleanImport.
........
r63039 | georg.brandl | 2008-05-11 03:06:05 -0400 (Sun, 11 May 2008) | 2 lines
#2742: ``''`` is not converted to NULL in getaddrinfo.
........
r63040 | alexandre.vassalotti | 2008-05-11 03:08:12 -0400 (Sun, 11 May 2008) | 2 lines
Fixed typo in a comment of test_support.CleanImport.
........
r63041 | alexandre.vassalotti | 2008-05-11 03:10:25 -0400 (Sun, 11 May 2008) | 2 lines
Removed a dead line of code.
........
r63043 | georg.brandl | 2008-05-11 04:47:53 -0400 (Sun, 11 May 2008) | 2 lines
#2812: document property.getter/setter/deleter.
........
r63049 | georg.brandl | 2008-05-11 05:06:30 -0400 (Sun, 11 May 2008) | 2 lines
#1153769: document PEP 237 changes to string formatting.
........
r63050 | georg.brandl | 2008-05-11 05:11:40 -0400 (Sun, 11 May 2008) | 2 lines
#2809: elaborate str.split docstring a bit.
........
r63051 | georg.brandl | 2008-05-11 06:13:59 -0400 (Sun, 11 May 2008) | 2 lines
Fix typo.
........
r63052 | georg.brandl | 2008-05-11 06:33:27 -0400 (Sun, 11 May 2008) | 2 lines
#2709: clarification.
........
r63053 | georg.brandl | 2008-05-11 06:42:28 -0400 (Sun, 11 May 2008) | 2 lines
#2659: add ``break_on_hyphens`` to TextWrapper.
........
r63057 | georg.brandl | 2008-05-11 06:59:39 -0400 (Sun, 11 May 2008) | 2 lines
#2741: clarification of value range for address_family.
........
r63058 | georg.brandl | 2008-05-11 07:09:35 -0400 (Sun, 11 May 2008) | 2 lines
#2452: timeout is used for all blocking operations.
........
r63059 | andrew.kuchling | 2008-05-11 09:33:56 -0400 (Sun, 11 May 2008) | 2 lines
#1792: Improve performance of marshal.dumps() on large objects by increasing
the size of the buffer more quickly.
........
r63060 | andrew.kuchling | 2008-05-11 10:00:00 -0400 (Sun, 11 May 2008) | 1 line
#1858: re-apply patch for this, adding the missing files
........
r63061 | benjamin.peterson | 2008-05-11 10:13:25 -0400 (Sun, 11 May 2008) | 2 lines
Add the "until" command to pdb
........
r63062 | georg.brandl | 2008-05-11 10:17:13 -0400 (Sun, 11 May 2008) | 2 lines
Add some sentence endings.
........
2008-05-15 21:03:33 -03:00
|
|
|
|
|
|
|
.. attribute:: break_on_hyphens
|
|
|
|
|
|
|
|
(default: ``True``) If true, wrapping will occur preferably on whitespaces
|
|
|
|
and right after hyphens in compound words, as it is customary in English.
|
|
|
|
If false, only whitespaces will be considered as potentially good places
|
|
|
|
for line breaks, but you need to set :attr:`break_long_words` to false if
|
|
|
|
you want truly insecable words. Default behaviour in previous versions
|
|
|
|
was to always allow breaking hyphenated words.
|
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
:class:`TextWrapper` also provides two public methods, analogous to the
|
|
|
|
module-level convenience functions:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. method:: wrap(text)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
Wraps the single paragraph in *text* (a string) so every line is at most
|
|
|
|
:attr:`width` characters long. All wrapping options are taken from
|
|
|
|
instance attributes of the :class:`TextWrapper` instance. Returns a list
|
|
|
|
of output lines, without final newlines.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
.. method:: fill(text)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-04-24 22:59:09 -03:00
|
|
|
Wraps the single paragraph in *text*, and returns a single string
|
|
|
|
containing the wrapped paragraph.
|
2007-08-15 11:28:22 -03:00
|
|
|
|