From 9e69c2e0c46d9fb972739764cb06d9c959cac8ac Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 22 Feb 2011 01:55:36 +0000 Subject: Factor-out common code for helper classes. --- Lib/string.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index ef0334c472..2bc5d00803 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -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() -- cgit v1.2.1 From 2886808d3809d69a6e9b360380080140b95df0b6 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 26 Feb 2011 01:02:51 +0000 Subject: Issue #11297: Add collections.ChainMap() --- Lib/string.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index 2bc5d00803..d4f9cd9355 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -46,7 +46,7 @@ def capwords(s, sep=None): #################################################################### import re as _re -from collections import _ChainMap +from collections import ChainMap class _TemplateMetaclass(type): pattern = r""" @@ -100,7 +100,7 @@ class Template(metaclass=_TemplateMetaclass): if not args: mapping = kws elif kws: - mapping = _ChainMap(kws, args[0]) + mapping = ChainMap(kws, args[0]) else: mapping = args[0] # Helper function for .sub() @@ -126,7 +126,7 @@ class Template(metaclass=_TemplateMetaclass): if not args: mapping = kws elif kws: - mapping = _ChainMap(kws, args[0]) + mapping = ChainMap(kws, args[0]) else: mapping = args[0] # Helper function for .sub() -- cgit v1.2.1 From f8e71ee91e75eb009168f0406eab580a5723f419 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Wed, 28 Sep 2011 17:37:55 +0300 Subject: #13012: use splitlines(keepends=True/False) instead of splitlines(0/1). --- Lib/string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index d4f9cd9355..8bcd1dc6b9 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -84,7 +84,7 @@ class Template(metaclass=_TemplateMetaclass): def _invalid(self, mo): i = mo.start('invalid') - lines = self.template[:i].splitlines(True) + lines = self.template[:i].splitlines(keepends=True) if not lines: colno = 1 lineno = 1 -- cgit v1.2.1