A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-September/048562.html below:

[Python-Dev] Re: Alternative placeholder delimiters for PEP 292

[Python-Dev] Re: Alternative placeholder delimiters for PEP 292Scott 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4