Partial substitution similar to str % dict

Emile van Sebille emile at fenx.com
Sun Aug 18 13:10:46 EDT 2002


Stefan Schwarzer
> I would like to do "partial" string substitutions, for example
> 
>  >>> partial_substitute( "%(foo)s-%(bar)s", {'foo': 'abc'} )
> 'abc-%(bar)s'
> 


class Partial(dict):
    def __getitem__(self, ky):
            try:
                    return self.__dict__[ky]
            except KeyError:
                return '%%(%s)' % ky

print "%(foo)s-%(bar)s" % Partial({'foo': 'abc'})

HTH,

--

Emile van Sebille
emile at fenx.com

---------







More information about the Python-list mailing list