[Python-checkins] cpython (merge 3.1 -> 3.2): merge

raymond.hettinger python-checkins at python.org
Sat Apr 16 02:44:53 CEST 2011


http://hg.python.org/cpython/rev/033dd346df57
changeset:   69385:033dd346df57
branch:      3.2
parent:      69382:3b700e6704b3
parent:      69384:9c8de0284c26
user:        Raymond Hettinger <python at rcn.com>
date:        Fri Apr 15 17:43:57 2011 -0700
summary:
  merge

files:
  Doc/library/collections.rst |  20 ++++++++++++++++++++
  1 files changed, 20 insertions(+), 0 deletions(-)


diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -845,6 +845,9 @@
    `Equivalent OrderedDict recipe <http://code.activestate.com/recipes/576693/>`_
    that runs on Python 2.4 or later.
 
+:class:`OrderedDict` Examples and Recipes
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
 Since an ordered dictionary remembers its insertion order, it can be used
 in conjuction with sorting to make a sorted dictionary::
 
@@ -874,11 +877,28 @@
 
     class LastUpdatedOrderedDict(OrderedDict):
         'Store items in the order the keys were last added'
+
         def __setitem__(self, key, value):
             if key in self:
                 del self[key]
             OrderedDict.__setitem__(self, key, value)
 
+An ordered dictionary can combined with the :class:`Counter` class
+so that the counter remembers the order elements are first encountered::
+
+   class OrderedCounter(Counter, OrderedDict):
+        'Counter that remembers the order elements are first encountered'
+
+        def __init__(self, iterable=None, **kwds):
+            OrderedDict.__init__(self)
+            Counter.__init__(self, iterable, **kwds)
+
+        def __repr__(self):
+            return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
+
+        def __reduce__(self):
+            return self.__class__, (OrderedDict(self),)
+
 
 :class:`UserDict` objects
 -------------------------

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list