Partial substitution similar to str % dict

Emile van Sebille emile at fenx.com
Sun Aug 18 13:26:44 EDT 2002


"Emile van Sebille" <emile at fenx.com> wrote in message
news:ajokq9$po6$1 at main.gmane.org...
> Stefan Schwarzer
> > I would like to do "partial" string substitutions, for example
> >
> >  >>> partial_substitute( "%(foo)s-%(bar)s", {'foo': 'abc'} )
> > 'abc-%(bar)s'

[ snip quick-reply non-working answer ;-) ]

Well, almost, but you probably want something that works...

class Partial(dict):
    def __getitem__(self, ky):
            return self.get(ky, '%%(%s)s' % ky)

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


--

Emile van Sebille
emile at fenx.com

---------







More information about the Python-list mailing list