Update Template/PEP 292 documentation to current implementation.

This commit is contained in:
Barry Warsaw 2004-09-18 21:13:43 +00:00
parent 0273f5b6b2
commit 33db656dbf

View file

@ -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
\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} \begin{itemize}
>>> from string import SafeTemplate \item \var{delimiter} -- This is the literal string describing a placeholder
>>> s = SafeTemplate('$who likes $what for ${meal}') introducing delimiter. The default value \samp{\$}. Note that this
>>> print s % dict(who='tim') should \emph{not} be a regular expression, as the implementation will
tim likes $what for ${meal} call \method{re.escape()} on this string as needed.
\end{verbatim} \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-z0-9]*}.
\end{itemize}
The values in the mapping will automatically be converted to Unicode strings, Alternatively, you can provide the entire regular expression pattern by
using the built-in \function{unicode()} function, which will be called without overriding the class attribute \var{pattern}. If you do this, the value must
optional arguments \var{encoding} or \var{errors}. be a regular expression object with four named capturing groups. The
Advanced usage: you can derive subclasses of \class{Template} or
\class{SafeTemplate} to use application-specific placeholder rules. To do
this, you override the class attribute \member{pattern}; the value must be a
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 delimited placeholder name; \item \var{braced} -- This group matches the brace enclosed 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}