[Python-checkins] r70121 - in python/trunk: Doc/library/collections.rst Lib/collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Tue Mar 3 05:51:25 CET 2009


Author: raymond.hettinger
Date: Tue Mar 3 05:51:24 2009
New Revision: 70121
Log:
Backport 70106: Add OrderedDict support to collections.namedtuple().
Modified:
 python/trunk/Doc/library/collections.rst
 python/trunk/Lib/collections.py
 python/trunk/Misc/NEWS
Modified: python/trunk/Doc/library/collections.rst
==============================================================================
--- python/trunk/Doc/library/collections.rst	(original)
+++ python/trunk/Doc/library/collections.rst	Tue Mar 3 05:51:24 2009
@@ -676,9 +676,9 @@
 def __repr__(self):
 return 'Point(x=%r, y=%r)' % self
 <BLANKLINE>
- def _asdict(t):
- 'Return a new dict which maps field names to their values'
- return {'x': t[0], 'y': t[1]}
+ def _asdict(self):
+ 'Return a new OrderedDict which maps field names to their values'
+ return OrderedDict(zip(self._fields, self))
 <BLANKLINE>
 def _replace(self, **kwds):
 'Return a new Point object replacing specified fields with new values'
@@ -736,10 +736,14 @@
 
 .. 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()
- {'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)
 
Modified: python/trunk/Lib/collections.py
==============================================================================
--- python/trunk/Lib/collections.py	(original)
+++ python/trunk/Lib/collections.py	Tue Mar 3 05:51:24 2009
@@ -164,7 +164,6 @@
 numfields = len(field_names)
 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
 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):
 '%(typename)s(%(argtxt)s)' \n
 __slots__ = () \n
@@ -180,9 +179,9 @@
 return result \n
 def __repr__(self):
 return '%(typename)s(%(reprtxt)s)' %% self \n
- def _asdict(t):
- 'Return a new dict which maps field names to their values'
- return {%(dicttxt)s} \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))
@@ -198,7 +197,8 @@
 
 # Execute the template string in a temporary namespace and
 # 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:
 exec template in namespace
 except SyntaxError, e:
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Mar 3 05:51:24 2009
@@ -170,6 +170,8 @@
 
 - PEP 372: Added collections.OrderedDict().
 
+- The _asdict() for method for namedtuples now returns an OrderedDict().
+
 - Issue #4308: httplib.IncompleteRead's repr doesn't include all of the data all
 ready received.
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /