mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Update PEP292 section
This commit is contained in:
parent
dff68d0ced
commit
a79ec22f9f
1 changed files with 5 additions and 10 deletions
|
@ -213,27 +213,22 @@ Unicode string:
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> import string
|
>>> import string
|
||||||
>>> t = string.Template('$page: $title')
|
>>> t = string.Template('$page: $title')
|
||||||
>>> t % {'page':2, 'title': 'The Best of Times'}
|
>>> t.substitute({'page':2, 'title': 'The Best of Times'})
|
||||||
u'2: The Best of Times'
|
u'2: The Best of Times'
|
||||||
>>> t2 % {'cost':42.50, 'action':'polish'}
|
|
||||||
u'$ 42.5: polishing'
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
% $ Terminate $-mode for Emacs
|
% $ Terminate $-mode for Emacs
|
||||||
|
|
||||||
If a key is missing from the dictionary, the \class{Template} class
|
If a key is missing from the dictionary, the \method{substitute} method
|
||||||
will raise a \exception{KeyError}. There's also a \class{SafeTemplate}
|
will raise a \exception{KeyError}. There's also a \method{safe_substitute}
|
||||||
class that ignores missing keys:
|
method that ignores missing keys:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> t = string.SafeTemplate('$page: $title')
|
>>> t = string.SafeTemplate('$page: $title')
|
||||||
>>> t % {'page':3}
|
>>> t.safe_substitute({'page':3})
|
||||||
u'3: $title'
|
u'3: $title'
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Because templates are Unicode strings, you can use a template with the
|
|
||||||
\module{gettext} module to look up translated versions of a message.
|
|
||||||
|
|
||||||
\begin{seealso}
|
\begin{seealso}
|
||||||
\seepep{292}{Simpler String Substitutions}{Written and implemented
|
\seepep{292}{Simpler String Substitutions}{Written and implemented
|
||||||
by Barry Warsaw.}
|
by Barry Warsaw.}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue