Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
1ba81ee1
Kaydet (Commit)
1ba81ee1
authored
Ock 11, 2013
tarafından
Vinay Sajip
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Closes #16613: Added optional mapping argument to ChainMap.new_child.
üst
569ff4fb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
7 deletions
+49
-7
collections.rst
Doc/library/collections.rst
+9
-4
__init__.py
Lib/collections/__init__.py
+8
-3
test_collections.py
Lib/test/test_collections.py
+32
-0
No files found.
Doc/library/collections.rst
Dosyayı görüntüle @
1ba81ee1
...
...
@@ -76,14 +76,19 @@ The class can be used to simulate nested scopes and is useful in templating.
be modified to change which mappings are searched. The list should
always contain at least one mapping.
.. method:: new_child()
.. method:: new_child(
m=None
)
Returns a new :class:`ChainMap` containing a new :class:`dict` followed by
all of the maps in the current instance. A call to ``d.new_child()`` is
equivalent to: ``ChainMap({}, *d.maps)``. This method is used for
Returns a new :class:`ChainMap` containing a new map followed by
all of the maps in the current instance. If ``m`` is specified,
it becomes the new map at the front of the list of mappings; if not
specified, an empty dict is used, so that a call to ``d.new_child()``
is equivalent to: ``ChainMap({}, *d.maps)``. This method is used for
creating subcontexts that can be updated without altering values in any
of the parent mappings.
.. versionchanged:: 3.4
The optional ``m`` parameter was added.
.. attribute:: parents
Property returning a new :class:`ChainMap` containing all of the maps in
...
...
Lib/collections/__init__.py
Dosyayı görüntüle @
1ba81ee1
...
...
@@ -821,9 +821,14 @@ class ChainMap(MutableMapping):
__copy__
=
copy
def
new_child
(
self
):
# like Django's Context.push()
'New ChainMap with a new dict followed by all previous maps.'
return
self
.
__class__
({},
*
self
.
maps
)
def
new_child
(
self
,
m
=
None
):
# like Django's Context.push()
'''
New ChainMap with a new map followed by all previous maps. If no
map is provided, an empty dict is used.
'''
if
m
is
None
:
m
=
{}
return
self
.
__class__
(
m
,
*
self
.
maps
)
@property
def
parents
(
self
):
# like Django's Context.pop()
...
...
Lib/test/test_collections.py
Dosyayı görüntüle @
1ba81ee1
...
...
@@ -112,6 +112,38 @@ class TestChainMap(unittest.TestCase):
self
.
assertEqual
(
dict
(
d
),
dict
(
a
=
1
,
b
=
2
,
c
=
30
))
self
.
assertEqual
(
dict
(
d
.
items
()),
dict
(
a
=
1
,
b
=
2
,
c
=
30
))
def
test_new_child
(
self
):
'Tests for changes for issue #16613.'
c
=
ChainMap
()
c
[
'a'
]
=
1
c
[
'b'
]
=
2
m
=
{
'b'
:
20
,
'c'
:
30
}
d
=
c
.
new_child
(
m
)
self
.
assertEqual
(
d
.
maps
,
[{
'b'
:
20
,
'c'
:
30
},
{
'a'
:
1
,
'b'
:
2
}])
# check internal state
self
.
assertIs
(
m
,
d
.
maps
[
0
])
# Use a different map than a dict
class
lowerdict
(
dict
):
def
__getitem__
(
self
,
key
):
if
isinstance
(
key
,
str
):
key
=
key
.
lower
()
return
dict
.
__getitem__
(
self
,
key
)
def
__contains__
(
self
,
key
):
if
isinstance
(
key
,
str
):
key
=
key
.
lower
()
return
dict
.
__contains__
(
self
,
key
)
c
=
ChainMap
()
c
[
'a'
]
=
1
c
[
'b'
]
=
2
m
=
lowerdict
(
b
=
20
,
c
=
30
)
d
=
c
.
new_child
(
m
)
self
.
assertIs
(
m
,
d
.
maps
[
0
])
for
key
in
'abc'
:
# check contains
self
.
assertIn
(
key
,
d
)
for
k
,
v
in
dict
(
a
=
1
,
B
=
20
,
C
=
30
,
z
=
100
)
.
items
():
# check get
self
.
assertEqual
(
d
.
get
(
k
,
100
),
v
)
################################################################################
### Named Tuples
...
...
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