From 71b6de5e626078a1c7c9eef531dac826fbe439c3 Mon Sep 17 00:00:00 2001 From: zrothberg Date: Thu, 27 Oct 2022 06:24:41 -0400 Subject: [PATCH 1/3] Lazy slicing and iter --- Lib/collections/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 58607874be93d6..b50d5042e7ac0c 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -32,6 +32,7 @@ from itertools import chain as _chain from itertools import repeat as _repeat from itertools import starmap as _starmap +from itertools import islice as _isclice from keyword import iskeyword as _iskeyword from operator import eq as _eq from operator import itemgetter as _itemgetter @@ -1010,9 +1011,8 @@ def __len__(self): return len(set().union(*self.maps)) # reuses stored hash values if possible def __iter__(self): - d = {} - for mapping in reversed(self.maps): - d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible + lazy_views = (keyview.keys() for keyview in reversed(self.maps)) + d = dict.fromkeys(_chain.from_iterable(lazy_views)) return iter(d) def __contains__(self, key): @@ -1032,7 +1032,7 @@ def fromkeys(cls, iterable, *args): def copy(self): 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]' - return self.__class__(self.maps[0].copy(), *self.maps[1:]) + return self.__class__(self.maps[0].copy(), *_isclice(self.maps, 1, None)) __copy__ = copy @@ -1050,7 +1050,7 @@ def new_child(self, m=None, **kwargs): # like Django's Context.push() @property def parents(self): # like Django's Context.pop() 'New ChainMap from maps[1:].' - return self.__class__(*self.maps[1:]) + return self.__class__(*_isclice(self.maps, 1, None)) def __setitem__(self, key, value): self.maps[0][key] = value From eabdf02aa844ca2abfee560fc840e47b469e41df Mon Sep 17 00:00:00 2001 From: zrothberg Date: Thu, 27 Oct 2022 07:02:46 -0400 Subject: [PATCH 2/3] correct key_view spelling to match outer --- Lib/collections/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index b50d5042e7ac0c..d52eeb8886d91d 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1011,7 +1011,7 @@ def __len__(self): return len(set().union(*self.maps)) # reuses stored hash values if possible def __iter__(self): - lazy_views = (keyview.keys() for keyview in reversed(self.maps)) + lazy_views = (key_view.keys() for key_view in reversed(self.maps)) d = dict.fromkeys(_chain.from_iterable(lazy_views)) return iter(d) From 14b21cdf254f3ce831b721ca6b9a4fabece2f44d Mon Sep 17 00:00:00 2001 From: zrothberg Date: Thu, 27 Oct 2022 10:13:05 -0400 Subject: [PATCH 3/3] Fix typos --- Lib/collections/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index d52eeb8886d91d..46849b0985d4d4 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -32,7 +32,7 @@ from itertools import chain as _chain from itertools import repeat as _repeat from itertools import starmap as _starmap -from itertools import islice as _isclice +from itertools import islice as _islice from keyword import iskeyword as _iskeyword from operator import eq as _eq from operator import itemgetter as _itemgetter @@ -1032,7 +1032,7 @@ def fromkeys(cls, iterable, *args): def copy(self): 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]' - return self.__class__(self.maps[0].copy(), *_isclice(self.maps, 1, None)) + return self.__class__(self.maps[0].copy(), *_islice(self.maps, 1, None)) __copy__ = copy @@ -1050,7 +1050,7 @@ def new_child(self, m=None, **kwargs): # like Django's Context.push() @property def parents(self): # like Django's Context.pop() 'New ChainMap from maps[1:].' - return self.__class__(*_isclice(self.maps, 1, None)) + return self.__class__(*_islice(self.maps, 1, None)) def __setitem__(self, key, value): self.maps[0][key] = value