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
81b96569
Commit
81b96569
authored
May 02, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backport code cleanup for namedtuples.
parent
cafa2efe
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
54 deletions
+81
-54
Lib/collections.py
Lib/collections.py
+81
-54
No files found.
Lib/collections.py
View file @
81b96569
...
@@ -236,10 +236,62 @@ class OrderedDict(dict):
...
@@ -236,10 +236,62 @@ class OrderedDict(dict):
### namedtuple
### namedtuple
################################################################################
################################################################################
_class_template
=
'''
\
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
class {typename}(tuple):
'{typename}({arg_list})'
__slots__ = ()
_fields = {field_names!r}
def __new__(_cls, {arg_list}):
'Create new instance of {typename}({arg_list})'
return _tuple.__new__(_cls, ({arg_list}))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new {typename} object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != {num_fields:d}:
raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '({repr_fmt})' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
def _replace(_self, **kwds):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
{field_defs}
'''
_repr_template
=
'{name}=%r'
_field_template
=
'''
\
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
'''
def
namedtuple
(
typename
,
field_names
,
verbose
=
False
,
rename
=
False
):
def
namedtuple
(
typename
,
field_names
,
verbose
=
False
,
rename
=
False
):
"""Returns a new subclass of tuple with named fields.
"""Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point',
'x y'
)
>>> Point = namedtuple('Point',
['x', 'y']
)
>>> Point.__doc__ # docstring for the new class
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p = Point(11, y=22) # instantiate with positional args or keywords
...
@@ -264,79 +316,54 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
...
@@ -264,79 +316,54 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
# generating informative error messages and preventing template injection attacks.
# generating informative error messages and preventing template injection attacks.
if
isinstance
(
field_names
,
str
):
if
isinstance
(
field_names
,
str
):
field_names
=
field_names
.
replace
(
','
,
' '
).
split
()
# names separated by whitespace and/or commas
field_names
=
field_names
.
replace
(
','
,
' '
).
split
()
# names separated by whitespace and/or commas
field_names
=
tuple
(
map
(
str
,
field_names
))
field_names
=
list
(
map
(
str
,
field_names
))
if
rename
:
if
rename
:
names
=
list
(
field_names
)
seen
=
set
()
seen
=
set
()
for
i
,
name
in
enumerate
(
names
):
for
index
,
name
in
enumerate
(
field_names
):
if
(
not
all
(
c
.
isalnum
()
or
c
==
'_'
for
c
in
name
)
or
_iskeyword
(
name
)
if
(
not
all
(
c
.
isalnum
()
or
c
==
'_'
for
c
in
name
)
or
not
name
or
name
[
0
].
isdigit
()
or
name
.
startswith
(
'_'
)
or
_iskeyword
(
name
)
or
not
name
or
name
[
0
].
isdigit
()
or
name
.
startswith
(
'_'
)
or
name
in
seen
):
or
name
in
seen
):
names
[
i
]
=
'_%d'
%
i
field_names
[
index
]
=
'_%d'
%
index
seen
.
add
(
name
)
seen
.
add
(
name
)
field_names
=
tuple
(
names
)
for
name
in
[
typename
]
+
field_names
:
for
name
in
(
typename
,)
+
field_names
:
if
not
all
(
c
.
isalnum
()
or
c
==
'_'
for
c
in
name
):
if
not
all
(
c
.
isalnum
()
or
c
==
'_'
for
c
in
name
):
raise
ValueError
(
'Type names and field names can only contain alphanumeric characters and underscores: %r'
%
name
)
raise
ValueError
(
'Type names and field names can only contain alphanumeric characters and underscores: %r'
%
name
)
if
_iskeyword
(
name
):
if
_iskeyword
(
name
):
raise
ValueError
(
'Type names and field names cannot be a keyword: %r'
%
name
)
raise
ValueError
(
'Type names and field names cannot be a keyword: %r'
%
name
)
if
name
[
0
].
isdigit
():
if
name
[
0
].
isdigit
():
raise
ValueError
(
'Type names and field names cannot start with a number: %r'
%
name
)
raise
ValueError
(
'Type names and field names cannot start with a number: %r'
%
name
)
seen
_names
=
set
()
seen
=
set
()
for
name
in
field_names
:
for
name
in
field_names
:
if
name
.
startswith
(
'_'
)
and
not
rename
:
if
name
.
startswith
(
'_'
)
and
not
rename
:
raise
ValueError
(
'Field names cannot start with an underscore: %r'
%
name
)
raise
ValueError
(
'Field names cannot start with an underscore: %r'
%
name
)
if
name
in
seen
_names
:
if
name
in
seen
:
raise
ValueError
(
'Encountered duplicate field name: %r'
%
name
)
raise
ValueError
(
'Encountered duplicate field name: %r'
%
name
)
seen_names
.
add
(
name
)
seen
.
add
(
name
)
# Create and fill-in the class template
# Fill-in the class template
numfields
=
len
(
field_names
)
class_definition
=
_class_template
.
format
(
argtxt
=
repr
(
field_names
).
replace
(
"'"
,
""
)[
1
:
-
1
]
# tuple repr without parens or quotes
typename
=
typename
,
reprtxt
=
', '
.
join
(
'%s=%%r'
%
name
for
name
in
field_names
)
field_names
=
tuple
(
field_names
),
template
=
'''class %(typename)s(tuple):
num_fields
=
len
(
field_names
),
'%(typename)s(%(argtxt)s)'
\
n
arg_list
=
repr
(
tuple
(
field_names
)).
replace
(
"'"
,
""
)[
1
:
-
1
],
__slots__ = ()
\
n
repr_fmt
=
', '
.
join
(
_repr_template
.
format
(
name
=
name
)
for
name
in
field_names
),
_fields = %(field_names)r
\
n
field_defs
=
'
\
n
'
.
join
(
_field_template
.
format
(
index
=
index
,
name
=
name
)
def __new__(_cls, %(argtxt)s):
for
index
,
name
in
enumerate
(
field_names
))
'Create new instance of %(typename)s(%(argtxt)s)'
)
return _tuple.__new__(_cls, (%(argtxt)s))
\
n
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new %(typename)s object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != %(numfields)d:
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
return result
\
n
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '(%(reprtxt)s)' %% self
\
n
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
\
n
def _replace(_self, **kwds):
'Return a new %(typename)s object replacing specified fields with new values'
result = _self._make(map(kwds.pop, %(field_names)r, _self))
if kwds:
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
return result
\
n
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
\
n
\
n
'''
%
locals
()
for
i
,
name
in
enumerate
(
field_names
):
template
+=
" %s = _property(_itemgetter(%d), doc='Alias for field number %d')
\
n
"
%
(
name
,
i
,
i
)
if
verbose
:
print
(
template
)
# Execute the template string in a temporary namespace and
# Execute the template string in a temporary namespace and
# support tracing utilities by setting a value for frame.f_globals['__name__']
# support tracing utilities by setting a value for frame.f_globals['__name__']
namespace
=
dict
(
_itemgetter
=
_itemgetter
,
__name__
=
'namedtuple_%s'
%
typename
,
namespace
=
dict
(
__name__
=
'namedtuple_%s'
%
typename
)
OrderedDict
=
OrderedDict
,
_property
=
property
,
_tuple
=
tuple
)
try
:
try
:
exec
(
template
,
namespace
)
exec
(
class_definition
,
namespace
)
except
SyntaxError
as
e
:
except
SyntaxError
as
e
:
raise
SyntaxError
(
e
.
msg
+
':
\
n
\
n
'
+
template
)
raise
SyntaxError
(
e
.
msg
+
':
\
n
\
n
'
+
class_definition
)
result
=
namespace
[
typename
]
result
=
namespace
[
typename
]
if
verbose
:
print
(
class_definition
)
# For pickling to work, the __module__ variable needs to be set to the frame
# For pickling to work, the __module__ variable needs to be set to the frame
# where the named tuple is created. Bypass this step in enviroments where
# where the named tuple is created. Bypass this step in enviroments where
...
...
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