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
efdbf751
Commit
efdbf751
authored
Sep 18, 2004
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Template/PEP 292 documentation to current implementation.
parent
f343340d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
37 deletions
+70
-37
Doc/lib/libstring.tex
Doc/lib/libstring.tex
+70
-37
No files found.
Doc/lib/libstring.tex
View file @
efdbf751
...
@@ -89,10 +89,8 @@ The constants defined in this module are:
...
@@ -89,10 +89,8 @@ The constants defined in this module are:
\subsection
{
Template strings
}
\subsection
{
Template strings
}
Templates are Unicode strings that can be used to provide string substitutions
Templates provide simpler string substitutions as described in
\pep
{
292
}
.
as described in
\pep
{
292
}
. There is a
\class
{
Template
}
class that is a
Instead of the normal
\samp
{
\%
}
-based substitutions, Templates support
subclass of
\class
{
unicode
}
, overriding the default
\method
{__
mod
__
()
}
method.
Instead of the normal
\samp
{
\%
}
-based substitutions, Template strings support
\samp
{
\$
}
-based substitutions, using the following rules:
\samp
{
\$
}
-based substitutions, using the following rules:
\begin{itemize}
\begin{itemize}
...
@@ -113,55 +111,90 @@ Any other appearance of \samp{\$} in the string will result in a
...
@@ -113,55 +111,90 @@ Any other appearance of \samp{\$} in the string will result in a
\versionadded
{
2.4
}
\versionadded
{
2.4
}
Template strings are used just like normal strings, in that the modulus
The
\module
{
string
}
module provides a
\class
{
Template
}
class that implements
operator is used to interpolate a dictionary of values into a Template string,
these rules. The methods of
\class
{
Template
}
are:
for example:
\begin{classdesc}
{
Template
}{
template
}
The constructor takes a single argument which is the template string.
\end{classdesc}
\begin{methoddesc}
[Template]
{
substitute
}{
mapping
\optional
{
, **kws
}}
Performs the template substitution, returning a new string.
\var
{
mapping
}
is
any dictionary-like object with keys that match the placeholders in the
template. Alternatively, you can provide keyword arguments, where the
keywords are the placeholders. When both
\var
{
mapping
}
and
\var
{
kws
}
are
given and there are duplicates, the placeholders from
\var
{
kws
}
take
precedence.
\end{methoddesc}
\begin{methoddesc}
[Template]
{
safe
_
substitute
}{
mapping
\optional
{
, **kws
}}
Like
\method
{
substitute()
}
, except that if placeholders are missing from
\var
{
mapping
}
and
\var
{
kws
}
, instead of raising a
\exception
{
KeyError
}
exception, the original placeholder will appear in the resulting string
intact. Note that other exceptions may still be raised, including
\exception
{
ValueError
}
as described above.
\end{methoddesc}
\class
{
Template
}
instances also provide one public data attribute:
\begin{memberdesc}
[string]
{
template
}
This is the object passed to the constructor's
\var
{
template
}
argument. In
general, you shouldn't change it, but read-only access is not enforced.
\end{memberdesc}
Here is an example of how to use a Template:
\begin{verbatim}
\begin{verbatim}
>>> from string import Template
>>> from string import Template
>>> s = Template('
$
who likes
$
what')
>>> s = Template('
$
who likes
$
what')
>>> print s
% dict(who='tim', what='kung pao')
>>> s.substitute(who='tim', what='kung pao')
tim likes kung pao
'tim likes kung pao'
>>> Template('Give
$
who
$
100')
% dict(who='tim')
>>> d = dict(who='tim')
>>> Template('Give
$
who
$
100').substitute(d)
Traceback (most recent call last):
Traceback (most recent call last):
[...]
[...]
ValueError: Invalid placeholder at index 10
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('
$
who likes
$
what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
>>> Template('
$
who likes
$
what').safe
_
substitute(d)
'tim likes
$
what'
\end
{
verbatim
}
\end
{
verbatim
}
There is also a
\class
{
SafeTemplate
}
class, derived from
\class
{
Template
}
Advanced usage: you can derive subclasses of
\class
{
Template
}
to customize the
which acts the same as
\class
{
Template
}
, except that if placeholders are
placeholder syntax, delimiter character, or the entire regular expression used
missing in the interpolation dictionary, no
\exception
{
KeyError
}
will be
to parse template strings. To do this, you can override these class
raised. Instead the original placeholder (with or without the braces, as
attributes:
appropriate) will be used:
\begin{verbatim}
>>> from string import SafeTemplate
>>> s = SafeTemplate('
$
who likes
$
what for
${
meal
}
'
)
>>> print s
% dict(who='tim')
tim likes
$
what for
${
meal
}
\end
{
verbatim
}
The values in the mapping will automatically be converted to Unicode strings,
\begin
{
itemize
}
using the built
-
in
\function
{
unicode
()
}
function, which will be called without
\item
\var
{
delimiter
}
--
This is the literal string describing a placeholder
optional arguments
\var
{
encoding
}
or
\var
{
errors
}
.
introducing delimiter. The default value
\samp
{
\$
}
. Note that this
should
\emph
{
not
}
be a regular expression, as the implementation will
call
\method
{
re.escape
()
}
on this string as needed.
\item
\var
{
idpattern
}
--
This is the regular expression describing the pattern
for non
-
braced placeholders
(
the braces will be added automatically as
appropriate
)
. The default value is the regular expression
\samp
{
[
_
a
-
z
][
_
a
-
z
0
-
9
]*
}
.
\end
{
itemize
}
Advanced usage: you can derive subclasses of
\class
{
Template
}
or
Alternatively, you can provide the entire regular expression pattern by
\class
{
SafeTemplate
}
to use application
-
specific placeholder rules. To do
overriding the class attribute
\var
{
pattern
}
. If you do this, the value must
this, you override the class attribute
\member
{
pattern
}
; the value must be a
be a regular expression object with four named capturing groups. The
compiled regular expression object with four named capturing groups. The
capturing groups correspond to the rules given above, along with the invalid
capturing groups correspond to the rules given above, along with the invalid
placeholder rule:
placeholder rule:
\begin
{
itemize
}
\begin
{
itemize
}
\item
\var
{
escaped
}
--
This group matches the escape sequence,
\samp
{
\$\$
}
,
\item
\var
{
escaped
}
--
This group matches the escape sequence,
in the default pattern.
e.g.
\samp
{
\$\$
}
,
in the default pattern.
\item
\var
{
named
}
--
This group matches the unbraced placeholder name; it
\item
\var
{
named
}
--
This group matches the unbraced placeholder name; it
should not include the
\samp
{
\$
}
in capturing group.
should not include the
delimiter
in capturing group.
\item
\var
{
braced
}
--
This group matches the brace
delimit
ed placeholder name;
\item
\var
{
braced
}
--
This group matches the brace
enclos
ed placeholder name;
it should not include either the
\samp
{
\$
}
or braces in the capturing
it should not include either the
delimiter
or braces in the capturing
group.
group.
\item
\var
{
bogus
}
--
This group matches any other
\samp
{
\$
}
. It usually just
\item
\var
{
invalid
}
--
This group matches any other delimiter pattern
(
usually
matches a single
\samp
{
\$
}
and should appear last.
a single delimiter
)
, and it should appear last in the regular
expression.
\end
{
itemize
}
\end
{
itemize
}
\subsection
{
String functions
}
\subsection
{
String functions
}
...
...
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