]> granicus.if.org Git - python/commitdiff
Factor-out common code for helper classes.
authorRaymond Hettinger <python@rcn.com>
Tue, 22 Feb 2011 01:55:36 +0000 (01:55 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 22 Feb 2011 01:55:36 +0000 (01:55 +0000)
Lib/collections/__init__.py
Lib/string.py

index 6c41db37286ab394a065b03a9f0514974cdb3ccb..89cf2edd3cb8786655cd7aaca4150d83a1cdd74e 100644 (file)
@@ -633,7 +633,7 @@ class Counter(dict):
 
 
 ########################################################################
-###  ChainMap (helper for configparser)
+###  ChainMap (helper for configparser and string.Template)
 ########################################################################
 
 class _ChainMap(MutableMapping):
index ef0334c472fcb70fd2fb571020e95a527fb517f6..2bc5d0080368788d973ae0b24f27abec00d9b84b 100644 (file)
@@ -46,23 +46,7 @@ def capwords(s, sep=None):
 
 ####################################################################
 import re as _re
-
-class _multimap:
-    """Helper class for combining multiple mappings.
-
-    Used by .{safe_,}substitute() to combine the mapping and keyword
-    arguments.
-    """
-    def __init__(self, primary, secondary):
-        self._primary = primary
-        self._secondary = secondary
-
-    def __getitem__(self, key):
-        try:
-            return self._primary[key]
-        except KeyError:
-            return self._secondary[key]
-
+from collections import _ChainMap
 
 class _TemplateMetaclass(type):
     pattern = r"""
@@ -116,7 +100,7 @@ class Template(metaclass=_TemplateMetaclass):
         if not args:
             mapping = kws
         elif kws:
-            mapping = _multimap(kws, args[0])
+            mapping = _ChainMap(kws, args[0])
         else:
             mapping = args[0]
         # Helper function for .sub()
@@ -142,7 +126,7 @@ class Template(metaclass=_TemplateMetaclass):
         if not args:
             mapping = kws
         elif kws:
-            mapping = _multimap(kws, args[0])
+            mapping = _ChainMap(kws, args[0])
         else:
             mapping = args[0]
         # Helper function for .sub()