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
d032131d
Commit
d032131d
authored
Feb 26, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add __bool__ method. Add tests. Fix-up broken test.
parent
cb8b9444
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
8 deletions
+17
-8
Lib/collections/__init__.py
Lib/collections/__init__.py
+3
-0
Lib/test/test_collections.py
Lib/test/test_collections.py
+14
-8
No files found.
Lib/collections/__init__.py
View file @
d032131d
...
...
@@ -679,6 +679,9 @@ class ChainMap(MutableMapping):
def
__contains__
(
self
,
key
):
return
any
(
key
in
m
for
m
in
self
.
maps
)
def
__bool__
(
self
):
return
any
(
self
.
maps
)
@
_recursive_repr
()
def
__repr__
(
self
):
return
'{0.__class__.__name__}({1})'
.
format
(
...
...
Lib/test/test_collections.py
View file @
d032131d
...
...
@@ -72,17 +72,23 @@ class TestChainMap(unittest.TestCase):
for
m1
,
m2
in
zip
(
d
.
maps
,
e
.
maps
):
self
.
assertIsNot
(
m1
,
m2
,
e
)
d
.
new_child
()
d
[
'b'
]
=
5
self
.
assertEqual
(
d
.
maps
,
[{
'b'
:
5
},
{
'c'
:
30
},
{
'a'
:
1
,
'b'
:
2
}])
self
.
assertEqual
(
d
.
parents
.
maps
,
[{
'c'
:
30
},
{
'a'
:
1
,
'b'
:
2
}])
# check parents
self
.
assertEqual
(
d
[
'b'
],
5
)
# find first in chain
self
.
assertEqual
(
d
.
parents
[
'b'
],
2
)
# look beyond maps[0]
f
=
d
.
new_child
()
f
[
'b'
]
=
5
self
.
assertEqual
(
f
.
maps
,
[{
'b'
:
5
},
{
'c'
:
30
},
{
'a'
:
1
,
'b'
:
2
}])
self
.
assertEqual
(
f
.
parents
.
maps
,
[{
'c'
:
30
},
{
'a'
:
1
,
'b'
:
2
}])
# check parents
self
.
assertEqual
(
f
[
'b'
],
5
)
# find first in chain
self
.
assertEqual
(
f
.
parents
[
'b'
],
2
)
# look beyond maps[0]
def
test_contructor
(
self
):
self
.
assertEqual
(
Chain
edContext
().
maps
,
[{}])
# no-args --> one new dict
self
.
assertEqual
(
Chain
Map
().
maps
,
[{}])
# no-args --> one new dict
self
.
assertEqual
(
ChainMap
({
1
:
2
}).
maps
,
[{
1
:
2
}])
# 1 arg --> list
def
test_bool
(
self
):
self
.
assertFalse
(
ChainMap
())
self
.
assertFalse
(
ChainMap
({},
{}))
self
.
assertTrue
(
ChainMap
({
1
:
2
},
{}))
self
.
assertTrue
(
ChainMap
({},
{
1
:
2
}))
def
test_missing
(
self
):
class
DefaultChainMap
(
ChainMap
):
def
__missing__
(
self
,
key
):
...
...
@@ -1182,7 +1188,7 @@ import doctest, collections
def
test_main
(
verbose
=
None
):
NamedTupleDocs
=
doctest
.
DocTestSuite
(
module
=
collections
)
test_classes
=
[
TestNamedTuple
,
NamedTupleDocs
,
TestOneTrickPonyABCs
,
TestCollectionABCs
,
TestCounter
,
TestCollectionABCs
,
TestCounter
,
TestChainMap
,
TestOrderedDict
,
GeneralMappingTests
,
SubclassMappingTests
]
support
.
run_unittest
(
*
test_classes
)
support
.
run_doctest
(
collections
,
verbose
)
...
...
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