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
88880b2d
Commit
88880b2d
authored
Dec 18, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more namedtuple() test cases. Neaten the code and comments.
parent
e64acfad
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
14 deletions
+44
-14
Doc/library/collections.rst
Doc/library/collections.rst
+5
-3
Lib/collections.py
Lib/collections.py
+11
-10
Lib/test/test_collections.py
Lib/test/test_collections.py
+28
-1
No files found.
Doc/library/collections.rst
View file @
88880b2d
...
...
@@ -388,8 +388,6 @@ Example::
__slots__ = ()
_fields = ('x', 'y')
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))
...
...
@@ -404,11 +402,15 @@ Example::
'Return a new Point object replacing specified fields with new values'
return Point(*map(kwds.get, ('x', 'y'), self))
@property
def _fields(self):
return ('x', 'y')
x = property(itemgetter(0))
y = property(itemgetter(1))
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
>>> p[0] + p[1] # indexable like the
regular
tuple (11, 22)
>>> p[0] + p[1] # indexable like the
plain
tuple (11, 22)
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
...
...
Lib/collections.py
View file @
88880b2d
__all__
=
[
'deque'
,
'defaultdict'
,
'namedtuple'
]
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
# They should however be considered an integral part of collections.py.
from
_abcoll
import
*
import
_abcoll
__all__
+=
_abcoll
.
__all__
from
_collections
import
deque
,
defaultdict
from
operator
import
itemgetter
as
_itemgetter
...
...
@@ -6,12 +11,6 @@ from itertools import izip as _izip
from
keyword
import
iskeyword
as
_iskeyword
import
sys
as
_sys
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
# They should however be considered an integral part of collections.py.
from
_abcoll
import
*
import
_abcoll
__all__
+=
_abcoll
.
__all__
def
namedtuple
(
typename
,
field_names
,
verbose
=
False
):
"""Returns a new subclass of tuple with named fields.
...
...
@@ -19,9 +18,9 @@ def namedtuple(typename, field_names, verbose=False):
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] #
works just like the tuple
(11, 22)
>>> p[0] + p[1] #
indexable like a plain tuple:
(11, 22)
33
>>> x, y = p # unpack
s just like a
tuple
>>> x, y = p # unpack
like a regular
tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessable by name
...
...
@@ -61,7 +60,6 @@ def namedtuple(typename, field_names, verbose=False):
template
=
'''class %(typename)s(tuple):
'%(typename)s(%(argtxt)s)'
\
n
__slots__ = ()
\
n
_fields = property(lambda self: %(field_names)r)
\
n
def __new__(cls, %(argtxt)s):
return tuple.__new__(cls, (%(argtxt)s))
\
n
def __repr__(self):
...
...
@@ -71,7 +69,10 @@ def namedtuple(typename, field_names, verbose=False):
return dict(zip(%(field_names)r, self))
\
n
def _replace(self, **kwds):
'Return a new %(typename)s object replacing specified fields with new values'
return %(typename)s(*map(kwds.get, %(field_names)r, self))
\
n
\
n
'''
%
locals
()
return %(typename)s(*map(kwds.get, %(field_names)r, self))
\
n
@property
def _fields(self):
return %(field_names)r
\
n
\
n
'''
%
locals
()
for
i
,
name
in
enumerate
(
field_names
):
template
+=
' %s = property(itemgetter(%d))
\
n
'
%
(
name
,
i
)
if
verbose
:
...
...
Lib/test/test_collections.py
View file @
88880b2d
...
...
@@ -90,9 +90,36 @@ class TestNamedTuple(unittest.TestCase):
def
test_odd_sizes
(
self
):
Zero
=
namedtuple
(
'Zero'
,
''
)
self
.
assertEqual
(
Zero
(),
())
self
.
assertEqual
(
repr
(
Zero
()),
'Zero()'
)
self
.
assertEqual
(
Zero
().
_asdict
(),
{})
self
.
assertEqual
(
Zero
().
_fields
,
())
Dot
=
namedtuple
(
'Dot'
,
'd'
)
self
.
assertEqual
(
Dot
(
1
),
(
1
,))
self
.
assertEqual
(
Dot
(
1
).
d
,
1
)
self
.
assertEqual
(
repr
(
Dot
(
1
)),
'Dot(d=1)'
)
self
.
assertEqual
(
Dot
(
1
).
_asdict
(),
{
'd'
:
1
})
self
.
assertEqual
(
Dot
(
1
).
_replace
(
d
=
999
),
(
999
,))
self
.
assertEqual
(
Dot
(
1
).
_fields
,
(
'd'
,))
n
=
10000
import
string
,
random
names
=
[
''
.
join
([
random
.
choice
(
string
.
letters
)
for
j
in
range
(
10
)])
for
i
in
range
(
n
)]
Big
=
namedtuple
(
'Big'
,
names
)
b
=
Big
(
*
range
(
n
))
self
.
assertEqual
(
b
,
tuple
(
range
(
n
)))
for
pos
,
name
in
enumerate
(
names
):
self
.
assertEqual
(
getattr
(
b
,
name
),
pos
)
repr
(
b
)
# make sure repr() doesn't blow-up
d
=
b
.
_asdict
()
d_expected
=
dict
(
zip
(
names
,
range
(
n
)))
self
.
assertEqual
(
d
,
d_expected
)
b2
=
b
.
_replace
(
**
dict
([(
names
[
1
],
999
),(
names
[
-
5
],
42
)]))
b2_expected
=
range
(
n
)
b2_expected
[
1
]
=
999
b2_expected
[
-
5
]
=
42
self
.
assertEqual
(
b2
,
tuple
(
b2_expected
))
self
.
assertEqual
(
b
.
_fields
,
tuple
(
names
))
class
TestOneTrickPonyABCs
(
unittest
.
TestCase
):
...
...
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