[Python-Dev] Re: Alternative ImplementationforPEP292:SimpleString Substitutions

Raymond Hettinger raymond.hettinger at verizon.net
Sat Sep 11 00:22:54 CEST 2004


> > * For someone who understands exactly what they are doing, perhaps
$ma
> > is the intended placeholder -- why force them to uses braces:
> > ${ma}ñana.
> 
> It also makes it more difficult to document.  IOW, right now the PEP
and
> the documentation say that the first non-identifier character
terminates
> the placeholder.  How would you word the rules with your change?

"""Placeholders must be a valid Python identifier (containing only ASCII
alphanumeric characters and an underscore).  If an unbraced identifier
ends with a non-ASCII alphanumeric character, such as the latin letter n
with tilde in $mañana, then a ValueError is raised for the specious
identifier.



> My only problem with that is the interference that the 'mapping'
> argument presents.  IOW, kwds can't contain 'mapping'. 

To support a case where both a mapping and keywords are present, perhaps
an auxiliary class could simplify matters:

   def substitute(self, mapping=None, **kwds):
       if mapping is None:
           mapping = kwds
       elif kwds:
           mapping = _altmap(kwds, mapping)
        . . .


class _altmap:
    def __init__(self, primary, secondary):
        self.primary = primary
        self.secondary = secondary
    def __getitem__(self, key):
        try:
            return self.primary[key]
        except KeyError:
            return self.secondary[key]
        


This matches the way keywords are used with the dict().


Raymond



More information about the Python-Dev mailing list