Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3793f95f
Commit
3793f95f
authored
Feb 11, 2018
by
Raymond Hettinger
Committed by
GitHub
Feb 11, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32792: Preserve mapping order in ChainMap() (GH-5586)
parent
8d1f2f40
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
1 deletion
+22
-1
Lib/collections/__init__.py
Lib/collections/__init__.py
+4
-1
Lib/test/test_collections.py
Lib/test/test_collections.py
+17
-0
Misc/NEWS.d/next/Library/2018-02-08-00-47-07.bpo-32792.NtyDb4.rst
...S.d/next/Library/2018-02-08-00-47-07.bpo-32792.NtyDb4.rst
+1
-0
No files found.
Lib/collections/__init__.py
View file @
3793f95f
...
...
@@ -920,7 +920,10 @@ class ChainMap(_collections_abc.MutableMapping):
return
len
(
set
().
union
(
*
self
.
maps
))
# reuses stored hash values if possible
def
__iter__
(
self
):
return
iter
(
set
().
union
(
*
self
.
maps
))
d
=
{}
for
mapping
in
reversed
(
self
.
maps
):
d
.
update
(
mapping
)
# reuses stored hash values if possible
return
iter
(
d
)
def
__contains__
(
self
,
key
):
return
any
(
key
in
m
for
m
in
self
.
maps
)
...
...
Lib/test/test_collections.py
View file @
3793f95f
...
...
@@ -141,6 +141,23 @@ class TestChainMap(unittest.TestCase):
with
self
.
assertRaises
(
KeyError
):
d
.
popitem
()
def
test_order_preservation
(
self
):
d
=
ChainMap
(
OrderedDict
(
j
=
0
,
h
=
88888
),
OrderedDict
(),
OrderedDict
(
i
=
9999
,
d
=
4444
,
c
=
3333
),
OrderedDict
(
f
=
666
,
b
=
222
,
g
=
777
,
c
=
333
,
h
=
888
),
OrderedDict
(),
OrderedDict
(
e
=
55
,
b
=
22
),
OrderedDict
(
a
=
1
,
b
=
2
,
c
=
3
,
d
=
4
,
e
=
5
),
OrderedDict
(),
)
self
.
assertEqual
(
''
.
join
(
d
),
'abcdefghij'
)
self
.
assertEqual
(
list
(
d
.
items
()),
[(
'a'
,
1
),
(
'b'
,
222
),
(
'c'
,
3333
),
(
'd'
,
4444
),
(
'e'
,
55
),
(
'f'
,
666
),
(
'g'
,
777
),
(
'h'
,
88888
),
(
'i'
,
9999
),
(
'j'
,
0
)])
def
test_dict_coercion
(
self
):
d
=
ChainMap
(
dict
(
a
=
1
,
b
=
2
),
dict
(
b
=
20
,
c
=
30
))
self
.
assertEqual
(
dict
(
d
),
dict
(
a
=
1
,
b
=
2
,
c
=
30
))
...
...
Misc/NEWS.d/next/Library/2018-02-08-00-47-07.bpo-32792.NtyDb4.rst
0 → 100644
View file @
3793f95f
collections.ChainMap() preserves the order of the underlying mappings.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment