[Python-Dev] Replacement for print in Python 3.0
Nick Coghlan
ncoghlan at gmail.com
Thu Sep 8 16:10:19 CEST 2005
Barry Warsaw wrote:
> On Wed, 2005-09-07 at 08:55, Nick Coghlan wrote:
>
>
>>The leading 'p' (for 'positional') is necessary to get around the fact that $1
>>is currently an illegal identifier in a Template
>
>
> That should be fixable. Ideally, $1 is better than $p1.
I also looked into the idea of adding formatting support to the
string.Template syntax.
Given a reimplementation of string.Template with the following pattern:
pattern = r"""
%(delim)s(?:
(?P<escaped>%(delim)s) | # An escape sequence of two delimiters, or
(
(\[(?P<format>[^%%]*)\])? # an optional simple format string,
(
(?P<named>%(id)s) | # and a Python identifier, or
{(?P<braced>%(id)s)} # a braced identifier
)
) |
(?P<invalid>) # An ill-formed delimiter expr
)
"""
And "convert" functions modified to use "fmt" where "'%s'" is currently used,
with "fmt" defined via:
fmt = mo.group('format')
if fmt is None:
fmt = '%s' # Default to simple string format
else:
fmt = '%' + fmt
The following works:
Py> t = format.Template("$item: $[0.2f]quantity")
Py> t.format(quantity=0.5, item='Bees')
'Bees: 0.50'
Combining with a 'format' function similar to the one in my previous message,
and an id pattern modified to permit numbers as identifiers:
Py> format("$1: $[0.2f]2", 'Bees', 0.5)
'Bees: 0.50'
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list