Commit 266e0dee authored by Raymond Hettinger's avatar Raymond Hettinger

Add OrderedDict support to collections.namedtuple().

parent c643ae29
...@@ -651,9 +651,9 @@ Example: ...@@ -651,9 +651,9 @@ Example:
def __repr__(self): def __repr__(self):
return 'Point(x=%r, y=%r)' % self return 'Point(x=%r, y=%r)' % self
<BLANKLINE> <BLANKLINE>
def _asdict(t): def _asdict(self):
'Return a new dict which maps field names to their values' 'Return a new OrderedDict which maps field names to their values'
return {'x': t[0], 'y': t[1]} return OrderedDict(zip(self._fields, self))
<BLANKLINE> <BLANKLINE>
def _replace(self, **kwds): def _replace(self, **kwds):
'Return a new Point object replacing specified fields with new values' 'Return a new Point object replacing specified fields with new values'
...@@ -711,10 +711,14 @@ field names, the method and attribute names start with an underscore. ...@@ -711,10 +711,14 @@ field names, the method and attribute names start with an underscore.
.. method:: somenamedtuple._asdict() .. method:: somenamedtuple._asdict()
Return a new dict which maps field names to their corresponding values:: Return a new :class:`OrderedDict` which maps field names to their corresponding
values::
>>> p._asdict() >>> p._asdict()
{'x': 11, 'y': 22} OrderedDict([('x', 11), ('y', 22)])
.. versionchanged 3.1
Returns an :class:`OrderedDict` instead of a regular :class:`dict`.
.. method:: somenamedtuple._replace(kwargs) .. method:: somenamedtuple._replace(kwargs)
......
...@@ -149,7 +149,6 @@ def namedtuple(typename, field_names, verbose=False, rename=False): ...@@ -149,7 +149,6 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
numfields = len(field_names) numfields = len(field_names)
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
reprtxt = ', '.join('%s=%%r' % name for name in field_names) reprtxt = ', '.join('%s=%%r' % name for name in field_names)
dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
template = '''class %(typename)s(tuple): template = '''class %(typename)s(tuple):
'%(typename)s(%(argtxt)s)' \n '%(typename)s(%(argtxt)s)' \n
__slots__ = () \n __slots__ = () \n
...@@ -165,9 +164,9 @@ def namedtuple(typename, field_names, verbose=False, rename=False): ...@@ -165,9 +164,9 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
return result \n return result \n
def __repr__(self): def __repr__(self):
return '%(typename)s(%(reprtxt)s)' %% self \n return '%(typename)s(%(reprtxt)s)' %% self \n
def _asdict(t): def _asdict(self):
'Return a new dict which maps field names to their values' 'Return a new OrderedDict which maps field names to their values'
return {%(dicttxt)s} \n return OrderedDict(zip(self._fields, self)) \n
def _replace(self, **kwds): def _replace(self, **kwds):
'Return a new %(typename)s object replacing specified fields with new values' 'Return a new %(typename)s object replacing specified fields with new values'
result = self._make(map(kwds.pop, %(field_names)r, self)) result = self._make(map(kwds.pop, %(field_names)r, self))
...@@ -183,7 +182,8 @@ def namedtuple(typename, field_names, verbose=False, rename=False): ...@@ -183,7 +182,8 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
# 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(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename,
OrderedDict=OrderedDict)
try: try:
exec(template, namespace) exec(template, namespace)
except SyntaxError as e: except SyntaxError as e:
......
...@@ -177,6 +177,8 @@ Library ...@@ -177,6 +177,8 @@ Library
- PEP 372: Added collections.OrderedDict(). - PEP 372: Added collections.OrderedDict().
- The _asdict() for method for namedtuples now returns an OrderedDict().
- Issue #1733986: Fixed mmap crash in accessing elements of second map object - Issue #1733986: Fixed mmap crash in accessing elements of second map object
with same tagname but larger size than first map. (Windows) with same tagname but larger size than first map. (Windows)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment