Back to index

python3.2  3.2.2
Classes | Functions | Variables
textwrap Namespace Reference

Classes

class  TextWrapper

Functions

def wrap
def fill
def dedent

Variables

list __all__ = ['TextWrapper', 'wrap', 'fill', 'dedent']
string _whitespace = '\t\n\x0b\x0c\r '
tuple _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
tuple _leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)

Detailed Description

Text wrapping and filling.

Function Documentation

def textwrap.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 common leading whitespace.  (This behaviour is
new in Python 2.5; older versions of this module incorrectly
expanded tabs before searching for common leading whitespace.)

Definition at line 336 of file textwrap.py.

00336 
00337 def dedent(text):
00338     """Remove any common leading whitespace from every line in `text`.
00339 
00340     This can be used to make triple-quoted strings line up with the left
00341     edge of the display, while still presenting them in the source code
00342     in indented form.
00343 
00344     Note that tabs and spaces are both treated as whitespace, but they
00345     are not equal: the lines "  hello" and "\thello" are
00346     considered to have no common leading whitespace.  (This behaviour is
00347     new in Python 2.5; older versions of this module incorrectly
00348     expanded tabs before searching for common leading whitespace.)
00349     """
00350     # Look for the longest leading string of spaces and tabs common to
00351     # all lines.
00352     margin = None
00353     text = _whitespace_only_re.sub('', text)
00354     indents = _leading_whitespace_re.findall(text)
00355     for indent in indents:
00356         if margin is None:
00357             margin = indent
00358 
00359         # Current line more deeply indented than previous winner:
00360         # no change (previous winner is still on top).
00361         elif indent.startswith(margin):
00362             pass
00363 
00364         # Current line consistent with and no deeper than previous winner:
00365         # it's the new winner.
00366         elif margin.startswith(indent):
00367             margin = indent
00368 
00369         # Current line and previous winner have no common whitespace:
00370         # there is no margin.
00371         else:
00372             margin = ""
00373             break
00374 
00375     # sanity check (testing/debugging only)
00376     if 0 and margin:
00377         for line in text.split("\n"):
00378             assert not line or line.startswith(margin), \
00379                    "line = %r, margin = %r" % (line, margin)
00380 
00381     if margin:
00382         text = re.sub(r'(?m)^' + margin, '', text)
00383     return text

Here is the call graph for this function:

def textwrap.fill (   text,
  width = 70,
  kwargs 
)
Fill a single paragraph of text, returning a new string.

Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph.  As with wrap(), tabs are expanded and other
whitespace characters converted to space.  See TextWrapper class for
available keyword args to customize wrapping behaviour.

Definition at line 318 of file textwrap.py.

00318 
00319 def fill(text, width=70, **kwargs):
00320     """Fill a single paragraph of text, returning a new string.
00321 
00322     Reformat the single paragraph in 'text' to fit in lines of no more
00323     than 'width' columns, and return a new string containing the entire
00324     wrapped paragraph.  As with wrap(), tabs are expanded and other
00325     whitespace characters converted to space.  See TextWrapper class for
00326     available keyword args to customize wrapping behaviour.
00327     """
00328     w = TextWrapper(width=width, **kwargs)
00329     return w.fill(text)
00330 
00331 
00332 # -- Loosely related functionality -------------------------------------

Here is the caller graph for this function:

def textwrap.wrap (   text,
  width = 70,
  kwargs 
)
Wrap a single paragraph of text, returning a list of wrapped lines.

Reformat the single paragraph in 'text' so it fits in lines of no
more than 'width' columns, and return a list of wrapped lines.  By
default, tabs in 'text' are expanded with string.expandtabs(), and
all other whitespace characters (including newline) are converted to
space.  See TextWrapper class for available keyword args to customize
wrapping behaviour.

Definition at line 305 of file textwrap.py.

00305 
00306 def wrap(text, width=70, **kwargs):
00307     """Wrap a single paragraph of text, returning a list of wrapped lines.
00308 
00309     Reformat the single paragraph in 'text' so it fits in lines of no
00310     more than 'width' columns, and return a list of wrapped lines.  By
00311     default, tabs in 'text' are expanded with string.expandtabs(), and
00312     all other whitespace characters (including newline) are converted to
00313     space.  See TextWrapper class for available keyword args to customize
00314     wrapping behaviour.
00315     """
00316     w = TextWrapper(width=width, **kwargs)
00317     return w.wrap(text)

Here is the caller graph for this function:


Variable Documentation

Definition at line 10 of file textwrap.py.

tuple textwrap._leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)

Definition at line 334 of file textwrap.py.

string textwrap._whitespace = '\t\n\x0b\x0c\r '

Definition at line 20 of file textwrap.py.

tuple textwrap._whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)

Definition at line 333 of file textwrap.py.