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
56145242
Commit
56145242
authored
Apr 02, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Have namedtuple's field renamer assign names that
are consistent with the corresponding tuple index.
parent
c26d4396
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
7 deletions
+7
-7
Lib/collections.py
Lib/collections.py
+1
-1
Lib/test/test_collections.py
Lib/test/test_collections.py
+6
-6
No files found.
Lib/collections.py
View file @
56145242
...
...
@@ -174,7 +174,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
if
(
not
all
(
c
.
isalnum
()
or
c
==
'_'
for
c
in
name
)
or
_iskeyword
(
name
)
or
not
name
or
name
[
0
].
isdigit
()
or
name
.
startswith
(
'_'
)
or
name
in
seen
):
names
[
i
]
=
'_%d'
%
(
i
+
1
)
names
[
i
]
=
'_%d'
%
i
seen
.
add
(
name
)
field_names
=
tuple
(
names
)
for
name
in
(
typename
,)
+
field_names
:
...
...
Lib/test/test_collections.py
View file @
56145242
...
...
@@ -51,12 +51,12 @@ class TestNamedTuple(unittest.TestCase):
def
test_name_fixer
(
self
):
for
spec
,
renamed
in
[
[(
'efg'
,
'g%hi'
),
(
'efg'
,
'_
2
'
)],
# field with non-alpha char
[(
'abc'
,
'class'
),
(
'abc'
,
'_
2
'
)],
# field has keyword
[(
'8efg'
,
'9ghi'
),
(
'_
1'
,
'_2
'
)],
# field starts with digit
[(
'abc'
,
'_efg'
),
(
'abc'
,
'_
2
'
)],
# field with leading underscore
[(
'abc'
,
'efg'
,
'efg'
,
'ghi'
),
(
'abc'
,
'efg'
,
'_
3
'
,
'ghi'
)],
# duplicate field
[(
'abc'
,
''
,
'x'
),
(
'abc'
,
'_
2
'
,
'x'
)],
# fieldname is a space
[(
'efg'
,
'g%hi'
),
(
'efg'
,
'_
1
'
)],
# field with non-alpha char
[(
'abc'
,
'class'
),
(
'abc'
,
'_
1
'
)],
# field has keyword
[(
'8efg'
,
'9ghi'
),
(
'_
0'
,
'_1
'
)],
# field starts with digit
[(
'abc'
,
'_efg'
),
(
'abc'
,
'_
1
'
)],
# field with leading underscore
[(
'abc'
,
'efg'
,
'efg'
,
'ghi'
),
(
'abc'
,
'efg'
,
'_
2
'
,
'ghi'
)],
# duplicate field
[(
'abc'
,
''
,
'x'
),
(
'abc'
,
'_
1
'
,
'x'
)],
# fieldname is a space
]:
self
.
assertEqual
(
namedtuple
(
'NT'
,
spec
,
rename
=
True
).
_fields
,
renamed
)
...
...
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