[Python-checkins] python/dist/src/Lib string.py,1.77,1.78

bwarsaw at users.sourceforge.net bwarsaw at users.sourceforge.net
Mon Sep 13 17:25:18 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9370

Modified Files:
	string.py 
Log Message:
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
with positional arguments.


Index: string.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/string.py,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -d -r1.77 -r1.78
--- string.py	13 Sep 2004 14:35:04 -0000	1.77
+++ string.py	13 Sep 2004 15:25:15 -0000	1.78
@@ -143,28 +143,36 @@
         raise ValueError('Invalid placeholder in string: line %d, col %d' %
                          (lineno, colno))
 
-    def substitute(self, __mapping=None, **kws):
-        if __mapping is None:
-            __mapping = kws
+    def substitute(self, *args, **kws):
+        if len(args) > 1:
+            raise TypeError('Too many positional arguments')
+        if not args:
+            mapping = kws
         elif kws:
-            __mapping = _multimap(kws, __mapping)
+            mapping = _multimap(kws, args[0])
+        else:
+            mapping = args[0]
         # Helper function for .sub()
         def convert(mo):
             if mo.group('escaped') is not None:
                 return '$'
             if mo.group('bogus') is not None:
                 self._bogus(mo)
-            val = __mapping[mo.group('named') or mo.group('braced')]
+            val = mapping[mo.group('named') or mo.group('braced')]
             # We use this idiom instead of str() because the latter will fail
             # if val is a Unicode containing non-ASCII characters.
             return '%s' % val
         return self.pattern.sub(convert, self.template)
 
-    def safe_substitute(self, __mapping=None, **kws):
-        if __mapping is None:
-            __mapping = kws
+    def safe_substitute(self, *args, **kws):
+        if len(args) > 1:
+            raise TypeError('Too many positional arguments')
+        if not args:
+            mapping = kws
         elif kws:
-            __mapping = _multimap(kws, __mapping)
+            mapping = _multimap(kws, args[0])
+        else:
+            mapping = args[0]
         # Helper function for .sub()
         def convert(mo):
             if mo.group('escaped') is not None:
@@ -176,12 +184,12 @@
                 try:
                     # We use this idiom instead of str() because the latter
                     # will fail if val is a Unicode containing non-ASCII
-                    return '%s' % __mapping[named]
+                    return '%s' % mapping[named]
                 except KeyError:
                     return '$' + named
             braced = mo.group('braced')
             try:
-                return '%s' % __mapping[braced]
+                return '%s' % mapping[braced]
             except KeyError:
                 return '${' + braced + '}'
         return self.pattern.sub(convert, self.template)



More information about the Python-checkins mailing list