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')
--- 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_substitute(self, *args, **kwargs):
+        return Template( self.safe_substitute(*args, **kwargs) )
+
+    def partial_replace(self, *args, **kwargs):
+        self.template = self.safe_substitute(*args, **kwargs)
+
     def substitute(self, *args, **kws):
         if len(args) > 1:
             raise TypeError('Too many positional arguments')
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to