# [Python-checkins] r87676 - python/branches/release27-maint/Lib/collections.py

raymond.hettinger python-checkins at python.org
Mon Jan 3 09:59:18 CET 2011

```Author: raymond.hettinger
Date: Mon Jan  3 09:59:18 2011
New Revision: 87676

Log:
Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using  __reduce__ method for pickling.

Modified:
python/branches/release27-maint/Lib/collections.py

Modified: python/branches/release27-maint/Lib/collections.py
==============================================================================
--- python/branches/release27-maint/Lib/collections.py	(original)
+++ python/branches/release27-maint/Lib/collections.py	Mon Jan  3 09:59:18 2011
@@ -344,16 +344,16 @@
or multiset.  Elements are stored as dictionary keys and their counts
are stored as dictionary values.

-    >>> c = Counter('abracadabra')      # count elements from a string
+    >>> c = Counter('abcdeabcdabcaba')  # count elements from a string

>>> c.most_common(3)                # three most common elements
-    [('a', 5), ('r', 2), ('b', 2)]
+    [('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c)                       # list all unique elements
-    ['a', 'b', 'c', 'd', 'r']
+    ['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements()))   # list elements with repetitions
-    'aaaaabbcdrr'
+    'aaaaabbbbcccdde'
>>> sum(c.values())                 # total of all counts
-    11
+    15

>>> c['a']                          # count of letter 'a'
5
@@ -361,8 +361,8 @@
...     c[elem] += 1                # by adding 1 to each element's count
>>> c['a']                          # now there are seven 'a'
7
-    >>> del c['r']                      # remove all 'r'
-    >>> c['r']                          # now there are zero 'r'
+    >>> del c['b']                      # remove all 'b'
+    >>> c['b']                          # now there are zero 'b'
0

>>> d = Counter('simsalabim')       # make another counter
@@ -401,6 +401,7 @@
>>> c = Counter(a=4, b=2)                   # a new counter from keyword args

'''
+        super(Counter, self).__init__()
self.update(iterable, **kwds)

def __missing__(self, key):
@@ -412,8 +413,8 @@
'''List the n most common elements and their counts from the most
common to the least.  If n is None, then list all element counts.

-        [('a', 5), ('r', 2), ('b', 2)]
+        >>> Counter('abcdeabcdabcaba').most_common(3)
+        [('a', 5), ('b', 4), ('c', 3)]

'''
# Emulate Bag.sortedByCount from Smalltalk
@@ -479,7 +480,7 @@
for elem, count in iterable.iteritems():
self[elem] = self_get(elem, 0) + count
else:
-                    dict.update(self, iterable) # fast path when counter is empty
+                    super(Counter, self).update(iterable) # fast path when counter is empty
else:
self_get = self.get
for elem in iterable:
@@ -518,10 +519,13 @@
'Like dict.copy() but returns a Counter instance instead of a dict.'
return Counter(self)

+    def __reduce__(self):
+        return self.__class__, (dict(self),)
+
def __delitem__(self, elem):
'Like dict.__delitem__() but does not raise KeyError for missing values.'
if elem in self:
-            dict.__delitem__(self, elem)
+            super(Counter, self).__delitem__(elem)

def __repr__(self):
if not self:
```