String templates in Python: what are legal characters?

python, string, templates

Solution

From the documentation...

$identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" must spell a Python identifier. The first non-identifier character after the $ character terminates this placeholder specification.

The period is a non-identifier character, and braces are simply used to separate the identifier from adjacent non-identifier text.

Problem

I can't quite figure out what's going on with string templates: ``` t = Template('cannot teach an ${dog.old} ${tricks.new}. ${why} is this ${not} working') print t.safe_substitute({'dog.old': 'old dog', 'tricks.new': 'new tricks', 'why': 'OH WHY', 'not': '@#%@#% NOT'}) ``` This prints: ``` cannot teach an ${dog.old} ${tricks.new}. OH WHY is this @#%@#% NOT working ``` I thought that the braces handled arbitrary strings. What characters are allowed in braces and is there any way I can subclass `Template` to do what I want?

Original source