[Python-Dev] Re: Alternative placeholder delimiters for PEP 292
Scott David Daniels
Scott.Daniels at Acm.Org
Thu Sep 2 22:51:15 CEST 2004
Andrew Durdin wrote:
> A Yet Simpler Proposal, modifying that of PEP 292 ...
> ... placeholders are delimited by braces {}.
Do you know about the techinique I use? It works w/o a new library:
Surround-style delimiting, using a single (specifiable) character.
def subst(template, _sep='$', **kwds):
if '' not in kwds:
kwds[''] = _sep # Allow doubled _sep for _sep.
parts = template.split(_sep)
parts[1::2] = [kwds[element] for element in parts[1::2]]
return template[0:0].join(parts)
For 2.4, use a generator expression, not a list comprehension:
def subst(template, _sep='$', **kwds):
if '' not in kwds:
kwds[''] = _sep # Allow doubled _sep for _sep.
parts = template.split(_sep)
parts[1::2] = (kwds[element] for element in parts[1::2])
return template[0:0].join(parts)
Then you can use:
subst('What I $mean$ is $$5.00', mean='really mean')
or subst(u'What I $mean$ is $$5.00', mean=u'really mean')
or subst('What I $mean$ is $$5.00', mean='really mean', *locals())
or ...
-- Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-Dev
mailing list