[PATCH] allow partial replace in string.Template
Stefan Behnel
stefan.behnel-n05pAM at web.de
Mon Feb 14 04:59:57 EST 2005
Hi!
Here's a trivial patch against Lib/string.py that adds two new methods. The
first replaces the template by a partially resolved substitution and the
second creates a new, partially substituted template. I find those two useful
enough for integration in the stdlib, especially the replacing one is very
useful when pre-replacing some placeholders outside of string generation loops
or when building a string from a template step by step.
Maybe the method names need some discussion. Also, the creation of a new
Template does not handle sub-classes. I didn't know the best was to do this.
Use type(self)? That doesn't necessarily mean the constructor of that type
takes the same arguments...
Any comments?
Stefan
--- Lib/string.py~ 2004-11-01 04:52:43.000000000 +0100
+++ Lib/string.py 2005-02-14 10:41:41.000000000 +0100
@@ -145,6 +145,12 @@
raise ValueError('Invalid placeholder in string: line %d, col %d' %
(lineno, colno))
+ def partial_replace(self, *args, **kwargs):
+ self.template = self.safe_substitute(*args, **kwargs)
+
+ def partial_substitute(self, *args, **kwargs):
+ return Template( self.safe_substitute(*args, **kwargs) )
+
def substitute(self, *args, **kws):
if len(args) > 1:
raise TypeError('Too many positional arguments')
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: string.py.partially-resolve-templates.patch
URL: <http://mail.python.org/pipermail/python-list/attachments/20050214/d41d9853/attachment.ksh>
More information about the Python-list
mailing list