Bugs item #1158231, was opened at 2005-03-07 12:50 Message generated for change (Tracker Item Submitted) made by Item Submitter You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1158231&group_id=5470
Category: Python Library Group: Python 2.4 Status: Open Resolution: None Priority: 5 Submitted By: Stefan Behnel (scoder) Assigned to: Nobody/Anonymous (nobody) Summary: string.Template does not allow step-by-step replacements Initial Comment: A common use case for templates is partially replacing placeholders in loops or method cascades. The safe_substitute method of the Template class provides rudimentary support for this as long as Templates do not contain the "$$" escape. In cases where multiple replacements are necessary, a new Template must be created. This messes up the "$$" replacement. An example: .>>> from string import Template .>>> a = Template('$a + $$ != $b') .>>> b = Template(a.safe_substitute(a=1)) .>>> [ b.substitute(b=i) for i in range(3) ] Traceback [...] ValueError: Invalid placeholder in string: line 1, col 5 In the current implementation, there is no way of getting around this problem. I suggest adding a new class-method as constructor to the Template class: "Template.from_template", that takes a template, applies a safe_substitute with the provided (kw)arguments and returns a *valid* Template (not a string). However, this method *must not* replace occurrences of "$$" in the original template. The above example would then look like this: .>>> from string import Template .>>> a = Template('$va + $$ != $vb') .>>> b = Template.from_template(a, va=1) .>>> [ b.substitute(vb=i) for i in range(3) ] [ '1 + $ != 0', '1 + $ != 1', '1 + $ != 2' ] ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1158231&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com