diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-13 04:50:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-13 04:50:35 (GMT) |
commit | 35288c6b6c0323fd1159166c747705a1a022ad0b (patch) | |
tree | 0cae6e6965f0bba2e6ed98b8d7e7303a2d9f99ae /Lib/collections.py | |
parent | 783d73fc2f5ca7ca0a2c84380f3ddd6bce01de93 (diff) | |
download | cpython-35288c6b6c0323fd1159166c747705a1a022ad0b.zip cpython-35288c6b6c0323fd1159166c747705a1a022ad0b.tar.gz cpython-35288c6b6c0323fd1159166c747705a1a022ad0b.tar.bz2 |
Speed-up __repr__. Eliminate duplicate tests. Use a from-irmport.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 51 |
1 files changed, 3 insertions, 48 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 1d32d67..e5656b1 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -10,7 +10,7 @@ from operator import itemgetter as _itemgetter from keyword import iskeyword as _iskeyword import sys as _sys import heapq as _heapq -import itertools as _itertools +from itertools import repeat as _repeat, chain as _chain, starmap as _starmap ######################################################################## ### namedtuple ####################################################### @@ -217,9 +217,7 @@ class Counter(dict): ''' # Emulate Bag.do from Smalltalk and Multiset.begin from C++. - return _itertools.chain.from_iterable( - _itertools.starmap(_itertools.repeat, - self.iteritems())) + return _chain.from_iterable(_starmap(_repeat, self.iteritems())) # Override dict methods where necessary @@ -267,7 +265,7 @@ class Counter(dict): def __repr__(self): if not self: return '%s()' % self.__class__.__name__ - items = ', '.join('%r: %r' % item for item in self.most_common()) + items = ', '.join(map('%r: %r'.__mod__, self.most_common())) return '%s({%s})' % (self.__class__.__name__, items) @@ -303,49 +301,6 @@ if __name__ == '__main__': Point3D = namedtuple('Point3D', Point._fields + ('z',)) print Point3D.__doc__ - # Check that counters are copyable, deepcopyable, picklable, and have a - # repr/eval round-trip - import copy - words = Counter('which witch had which witches wrist watch'.split()) - update_test = Counter() - update_test.update(words) - for i, dup in enumerate([ - words.copy(), - copy.copy(words), - copy.deepcopy(words), - loads(dumps(words, 0)), - loads(dumps(words, 1)), - loads(dumps(words, 2)), - loads(dumps(words, -1)), - eval(repr(words)), - update_test, - ]): - msg = (i, dup, words) - assert dup is not words, msg - assert dup == words, msg - assert len(dup) == len(words), msg - assert type(dup) == type(words), msg - - # Verify that counters are unhashable - try: - hash(words) - except TypeError: - pass - else: - print 'Failed hashing test' - - # Verify that Counter.fromkeys() is disabled - try: - Counter.fromkeys('razmataz') - except NotImplementedError: - pass - else: - print 'Failed fromkeys() test' - - # Check ABCs - assert issubclass(Counter, Mapping) - assert isinstance(Counter('asdfasdf'), Mapping) - import doctest TestResults = namedtuple('TestResults', 'failed attempted') print TestResults(*doctest.testmod()) |