Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ae64f3ad
Commit
ae64f3ad
authored
Jun 29, 2002
by
Greg Ward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add documentation for new textwrap module.
parent
8b46c71d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
0 deletions
+143
-0
Doc/Makefile.deps
Doc/Makefile.deps
+1
-0
Doc/lib/lib.tex
Doc/lib/lib.tex
+1
-0
Doc/lib/libtextwrap.tex
Doc/lib/libtextwrap.tex
+141
-0
No files found.
Doc/Makefile.deps
View file @
ae64f3ad
...
...
@@ -124,6 +124,7 @@ LIBFILES= $(MANSTYLES) $(INDEXSTYLES) $(COMMONTEX) \
lib/libmain.tex
\
lib/libstrings.tex
\
lib/libstring.tex
\
lib/libtextwrap.tex
\
lib/libcodecs.tex
\
lib/libunicodedata.tex
\
lib/libstruct.tex
\
...
...
Doc/lib/lib.tex
View file @
ae64f3ad
...
...
@@ -107,6 +107,7 @@ and how to embed it in other applications.
\input
{
libdifflib
}
\input
{
libfpformat
}
\input
{
libstringio
}
\input
{
libtextwrap
}
\input
{
libcodecs
}
\input
{
libunicodedata
}
...
...
Doc/lib/libtextwrap.tex
0 → 100644
View file @
ae64f3ad
\section
{
\module
{
textwrap
}
---
Text wrapping and filling
}
\declaremodule
{
standard
}{
textwrap
}
\modulesynopsis
{
Text wrapping and filling
}
\moduleauthor
{
Greg Ward
}{
gward@python.net
}
\sectionauthor
{
Greg Ward
}{
gward@python.net
}
\versionadded
{
2.3
}
The
\module
{
textwrap
}
module provides two convenience functions,
\function
{
wrap()
}
and
\function
{
fill()
}
, as well as
\class
{
TextWrapper
}
, the class that does all the work. 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.
\begin{funcdesc}
{
wrap
}{
text, width=70, **kwargs
}
Wraps the single paragraph in
\var
{
text
}
(a string) so every line is at
most
\var
{
width
}
characters long. Returns a list of output lines,
without final newlines.
Optional keyword arguments correspond to the instance attributes of
\class
{
TextWrapper
}
, documented below.
\end{funcdesc}
\begin{funcdesc}
{
fill
}{
text, width=70, **kwargs
}
Wraps the single paragraph in
\var
{
text
}
, and returns a single string
containing the wrapped paragraph.
\function
{
fill()
}
is shorthand for
\begin{verbatim}
"
\n
".join(wrap(text, ...))
\end{verbatim}
In particular,
\function
{
fill()
}
accepts exactly the same keyword
arguments as
\function
{
wrap()
}
.
\end{funcdesc}
Both
\function
{
wrap()
}
and
\function
{
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.
% XXX how to typeset long argument lists? this just spills off
% the edge of the page, with or without \\ delimiters
\begin{classdesc}
{
TextWrapper
}{
width=70,
\\
initial
_
indent="",
\\
subsequent
_
indent="",
\\
expand
_
tabs=True,
\\
replace
_
whitespace=True,
\\
fix
_
sentence
_
endings=False,
\\
break
_
long
_
words=True
}
Each keyword argument to the constructor corresponds to an instance
attribute, so for example
\begin{verbatim}
wrapper = TextWrapper(initial
_
indent="* ")
\end{verbatim}
is the same as
\begin{verbatim}
wrapper = TextWrapper()
wrapper.initial
_
indent = "* "
\end{verbatim}
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. The effects of the instance attributes are as
follows:
\begin{memberdesc}
[bool]
{
expand
_
tabs
}
If true (the default), then all tab characters in
\var
{
text
}
will be
expanded to spaces using the
\method
{
expand
_
tabs()
}
method of
\var
{
text
}
.
\end{memberdesc}
\begin{memberdesc}
[bool]
{
replace
_
whitespace
}
If true (the default), each whitespace character (as defined by
\var
{
string.whitespace
}
) remaining after tab expansion will be replaced
by a single space.
\note
{
If
\var
{
expand
_
tabs
}
is false and
\var
{
replace
_
whitespace
}
is true, each tab character will be replaced by
a single space, which is
\emph
{
not
}
the same as tab expansion.
}
\end{memberdesc}
% XXX how to typeset the empty string? this looks awful, and "" is worse.
\begin{memberdesc}
[string]
{
initial
_
indent
}
(default: '') String that will be prepended to the first line of wrapped
output. Counts towards the length of the first line.
\end{memberdesc}
\begin{memberdesc}
[string]
{
subsequent
_
indent
}
(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.
\end{memberdesc}
\begin{memberdesc}
[bool]
{
fix
_
sentence
_
endings
}
(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
\character
{
.
}
,
\character
{
!
}
, or
\character
{
?
}
, possibly followed by one of
\character
{
"
}
or
\character
{
'
}
. One problem with this is algoritm is
that it is unable to detect the difference between ``Dr.'' in
\begin{verbatim}
[...] Dr. Frankenstein's monster [...]
\end{verbatim}
and ``Spot.'' in
\begin{verbatim}
[...] See Spot. See Spot run [...]
\end{verbatim}
Furthermore, since it relies on
\var
{
string.lowercase
}
for the
definition of ``lowercase letter'', it is specific to English-language
texts. Thus,
\var
{
fix
_
sentence
_
endings
}
is false by default.
\end{memberdesc}
\begin{memberdesc}
[bool]
{
break
_
long
_
words
}
If true (the default), then words longer than
\var
{
width
}
will be broken
in order to ensure that no lines are longer than
\var
{
width
}
. If it is
false, long words will not be broken, and some lines may be longer than
\var
{
width
}
. (Long words will be put on a line by themselves, in order
to minimize the amount by which
\var
{
width
}
is exceeded.)
\end{memberdesc}
\class
{
TextWrapper
}
also provides two public methods, analogous to the
module-level convenience functions:
\begin{methoddesc}
{
wrap
}{
text
}
Wraps the single paragraph in
\var
{
text
}
(a string) so every line is at
most
\var
{
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.
\end{methoddesc}
\begin{methoddesc}
{
fill
}{
text
}
Wraps the single paragraph in
\var
{
text
}
, and returns a single string
containing the wrapped paragraph.
\end{methoddesc}
\end{classdesc}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment