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
879ddf30
Commit
879ddf30
authored
Sep 14, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweak the wording. Improve examples. Make more brief.
parent
29c6a79b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
26 deletions
+15
-26
Doc/tut/tut.tex
Doc/tut/tut.tex
+15
-26
No files found.
Doc/tut/tut.tex
View file @
879ddf30
...
...
@@ -4841,26 +4841,26 @@ with group separators:
The
\ulink
{
\module
{
string
}}{
../lib/module-string.html
}
module includes a
versatile
\class
{
Template
}
class with a simplified syntax suitable for
editing by end-users. This allows users to customize their applications
without having to alter the
Python program
.
without having to alter the
application
.
The format uses
\samp
{
\$
}
for placeholder names that are
valid Python
The format uses
placeholder names formed by
\samp
{
\$
}
with
valid Python
identifiers (alphanumeric characters and underscores). Surrounding the
placeholder with braces allows it to be followed by more alphanumeric letters
with no intervening spaces.
\samp
{
\$\$
}
is the way to create
a single escaped
with no intervening spaces.
Writing
\samp
{
\$\$
}
creates
a single escaped
\samp
{
\$
}
:
\begin{verbatim}
>>> from string import Template
>>> t = Template('
${
village
}
folk send
$$
10
to
$
cause.')
>>> t.substitute(village='
n
ottingham', cause='the ditch fund')
'
n
ottinghamfolk send
$
10
to the ditch fund.'
>>> t.substitute(village='
N
ottingham', cause='the ditch fund')
'
N
ottinghamfolk send
$
10
to the ditch fund.'
\end
{
verbatim
}
The
\method
{
substitute
}
method raises a
\exception
{
KeyError
}
when a
placeholder is not supplied in a dictionary or a keyword argument.
For mail
-
merge style applications, user supplied data may be incomplet
e
and the
\method
{
safe
_
substitute
}
method may be more appropriate
---
it
will leave
placeholders unchanged if data is missing:
placeholder is not supplied in a dictionary or a keyword argument.
For
mail
-
merge style applications, user supplied data may be incomplete and th
e
\method
{
safe
_
substitute
}
method may be more appropriate
---
it will leave
placeholders unchanged if data is missing:
\begin
{
verbatim
}
>>> t
=
Template
(
'Return the
$
item to
$
owner.'
)
...
...
@@ -4875,7 +4875,7 @@ KeyError: 'owner'
Template subclasses can specify a custom delimiter. For example, a batch
renaming utility for a photo browser may elect to use percent signs for
metadata
such as the current date, image sequence number, or file format:
placeholders
such as the current date, image sequence number, or file format:
\begin{verbatim}
>>> import time, os.path
...
...
@@ -4885,11 +4885,11 @@ metadata such as the current date, image sequence number, or file format:
>>> fmt = raw
_
input('Enter rename style (
%d-date %n-seqnum %f-format): ')
Enter rename style (
%d-date %n-seqnum %f-format): Ashley_%n%f
>>>
rename
= BatchRename(fmt)
>>>
t
= BatchRename(fmt)
>>> date = time.strftime('
%d%b%y')
>>> for i, filename in enumerate(photofiles):
... base, ext = os.path.splitext(filename)
... newname =
rename
.substitute(d=date, n=i, f=ext)
... newname =
t
.substitute(d=date, n=i, f=ext)
... print '
%s --> %s' % (filename, newname)
img
_
1074.jpg --> Ashley
_
0.jpg
...
...
@@ -4897,20 +4897,9 @@ img_1076.jpg --> Ashley_1.jpg
img
_
1077.jpg --> Ashley
_
2.jpg
\end{verbatim}
For internationalization applications, it may be appropriate to coerce all
templates to
\class
{
unicode
}
. This can be done with subclassing and
overriding the
\method
{__
init
__}
method:
\begin{verbatim}
>>> class UnicodeTemplate(unicode, Template):
... def
__
init
__
(self, template):
... self.template = unicode(template)
>>> t = UnicodeTemplate('Knights who say
$
what'
)
>>> t.substitute
(
what
=
'Ni'
)
u'Knights who say Ni'
>>> t.capitalize
()
u'Knights who say
$
what'
\end{verbatim}
Another application for templating is separating program logic from the
details of multiple output formats. The makes it possible to substitute
custom templates for XML files, plain text reports, and HMTL web reports.
\section
{
Working with Binary Data Record Layouts
\label
{
binary-formats
}}
...
...
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