Simplify Counter() API. Replace items keyword argument
with a mapping. Makes Counter() idempotent, makes update()
API the same as Counter.__init__(), makes a more readable
repr, makes the API more dict-like, and allows Steven
Bethard's update() example to work.
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 00882e2..80ee2c5 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -370,8 +370,7 @@
self.assertEqual(c.get('b', 10), 2)
self.assertEqual(c.get('z', 10), 10)
self.assertEqual(c, dict(a=3, b=2, c=1))
- self.assertEqual(repr(c),
- "Counter(items=[('a', 3), ('b', 2), ('c', 1)])")
+ self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)])
for i in range(5):
self.assertEqual(c.most_common(i),
@@ -396,8 +395,8 @@
self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc')
self.assertRaises(TypeError, hash, c)
c.update(dict(a=5, b=3, c=1))
- c.update(Counter(items=[('a', 50), ('b', 30)]))
- c.__init__(items=[('a', 500), ('b', 300)])
+ c.update(Counter('a' * 50 + 'b' * 30))
+ c.__init__('a' * 500 + 'b' * 300)
c.__init__('cdc')
self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
self.assertEqual(c.setdefault('d', 5), 1)
@@ -425,6 +424,7 @@
cPickle.loads(cPickle.dumps(words, -1)),
eval(repr(words)),
update_test,
+ Counter(words),
]):
msg = (i, dup, words)
self.assert_(dup is not words)