blob: 4ac50d8a66df7b6e218fcd125c8dc0bf50e8643d [file] [log] [blame]
Brett Cannon23a4a7b2008-05-12 00:56:28 +00001__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
Raymond Hettinger2d32f632009-03-02 21:24:57 +00002 'UserString', 'Counter', 'OrderedDict']
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
4# They should however be considered an integral part of collections.py.
5from _abcoll import *
6import _abcoll
7__all__ += _abcoll.__all__
8
Christian Heimes99170a52007-12-19 02:07:34 +00009from _collections import deque, defaultdict
10from operator import itemgetter as _itemgetter
11from keyword import iskeyword as _iskeyword
12import sys as _sys
Raymond Hettingerb8baf632009-01-14 02:20:07 +000013import heapq as _heapq
Raymond Hettinger798ee1a2009-03-23 18:29:11 +000014from weakref import proxy as _proxy
Raymond Hettingerea9f8db2009-03-02 21:28:41 +000015from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
Raymond Hettinger2d32f632009-03-02 21:24:57 +000016
17################################################################################
18### OrderedDict
19################################################################################
20
Raymond Hettingerf1736542009-03-23 05:19:21 +000021class _Link(object):
22 __slots__ = 'prev', 'next', 'key', '__weakref__'
23
Raymond Hettinger2d32f632009-03-02 21:24:57 +000024class OrderedDict(dict, MutableMapping):
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +000025 'Dictionary that remembers insertion order'
Raymond Hettingerf1736542009-03-23 05:19:21 +000026 # An inherited dict maps keys to values.
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +000027 # The inherited dict provides __getitem__, __len__, __contains__, and get.
28 # The remaining methods are order-aware.
Raymond Hettingerf1736542009-03-23 05:19:21 +000029 # Big-O running times for all methods are the same as for regular dictionaries.
30
31 # The internal self.__map dictionary maps keys to links in a doubly linked list.
32 # The circular doubly linked list starts and ends with a sentinel element.
33 # The sentinel element never gets deleted (this simplifies the algorithm).
34 # The prev/next links are weakref proxies (to prevent circular references).
35 # Individual links are kept alive by the hard reference in self.__map.
36 # Those hard references disappear when a key is deleted from an OrderedDict.
Raymond Hettinger2d32f632009-03-02 21:24:57 +000037
38 def __init__(self, *args, **kwds):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000039 '''Initialize an ordered dictionary. Signature is the same as for
40 regular dictionaries, but keyword arguments are not recommended
41 because their insertion order is arbitrary.
42
43 '''
Raymond Hettinger2d32f632009-03-02 21:24:57 +000044 if len(args) > 1:
45 raise TypeError('expected at most 1 arguments, got %d' % len(args))
Raymond Hettinger69976a72010-09-12 05:28:42 +000046 self.__in_repr = False # detects recursive repr
Raymond Hettinger08c70cf2009-03-03 20:47:29 +000047 try:
Raymond Hettingerf1736542009-03-23 05:19:21 +000048 self.__root
Raymond Hettinger08c70cf2009-03-03 20:47:29 +000049 except AttributeError:
Raymond Hettinger52dc06b2009-03-25 22:45:22 +000050 self.__root = root = _Link() # sentinel node for the doubly linked list
51 root.prev = root.next = root
52 self.__map = {}
Raymond Hettinger2d32f632009-03-02 21:24:57 +000053 self.update(*args, **kwds)
54
55 def clear(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000056 'od.clear() -> None. Remove all items from od.'
Raymond Hettingerf1736542009-03-23 05:19:21 +000057 root = self.__root
58 root.prev = root.next = root
Raymond Hettinger52dc06b2009-03-25 22:45:22 +000059 self.__map.clear()
Raymond Hettinger2d32f632009-03-02 21:24:57 +000060 dict.clear(self)
61
62 def __setitem__(self, key, value):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000063 'od.__setitem__(i, y) <==> od[i]=y'
Raymond Hettingerf1736542009-03-23 05:19:21 +000064 # Setting a new item creates a new link which goes at the end of the linked
65 # list, and the inherited dictionary is updated with the new key/value pair.
Raymond Hettinger2d32f632009-03-02 21:24:57 +000066 if key not in self:
Raymond Hettingerf1736542009-03-23 05:19:21 +000067 self.__map[key] = link = _Link()
68 root = self.__root
69 last = root.prev
70 link.prev, link.next, link.key = last, root, key
Raymond Hettinger798ee1a2009-03-23 18:29:11 +000071 last.next = root.prev = _proxy(link)
Raymond Hettinger2d32f632009-03-02 21:24:57 +000072 dict.__setitem__(self, key, value)
73
74 def __delitem__(self, key):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000075 'od.__delitem__(y) <==> del od[y]'
Raymond Hettingerf1736542009-03-23 05:19:21 +000076 # Deleting an existing item uses self.__map to find the link which is
77 # then removed by updating the links in the predecessor and successor nodes.
Raymond Hettinger2d32f632009-03-02 21:24:57 +000078 dict.__delitem__(self, key)
Raymond Hettingerf1736542009-03-23 05:19:21 +000079 link = self.__map.pop(key)
80 link.prev.next = link.next
81 link.next.prev = link.prev
Raymond Hettinger2d32f632009-03-02 21:24:57 +000082
83 def __iter__(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000084 'od.__iter__() <==> iter(od)'
Raymond Hettingerf1736542009-03-23 05:19:21 +000085 # Traverse the linked list in order.
86 root = self.__root
87 curr = root.next
88 while curr is not root:
89 yield curr.key
90 curr = curr.next
Raymond Hettinger2d32f632009-03-02 21:24:57 +000091
92 def __reversed__(self):
Raymond Hettinger2352cf32009-04-08 01:16:27 +000093 'od.__reversed__() <==> reversed(od)'
Raymond Hettingerf1736542009-03-23 05:19:21 +000094 # Traverse the linked list in reverse order.
95 root = self.__root
96 curr = root.prev
97 while curr is not root:
98 yield curr.key
99 curr = curr.prev
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000100
101 def __reduce__(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000102 'Return state information for pickling'
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000103 items = [[k, self[k]] for k in self]
Raymond Hettinger69976a72010-09-12 05:28:42 +0000104 tmp = self.__map, self.__root, self.__in_repr
105 del self.__map, self.__root, self.__in_repr
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000106 inst_dict = vars(self).copy()
Raymond Hettinger69976a72010-09-12 05:28:42 +0000107 self.__map, self.__root, self.__in_repr = tmp
Raymond Hettinger14b89ff2009-03-03 22:20:56 +0000108 if inst_dict:
109 return (self.__class__, (items,), inst_dict)
110 return self.__class__, (items,)
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000111
112 setdefault = MutableMapping.setdefault
113 update = MutableMapping.update
114 pop = MutableMapping.pop
115 keys = MutableMapping.keys
116 values = MutableMapping.values
117 items = MutableMapping.items
118
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +0000119 def popitem(self, last=True):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000120 '''od.popitem() -> (k, v), return and remove a (key, value) pair.
121 Pairs are returned in LIFO order if last is true or FIFO order if false.
122
123 '''
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +0000124 if not self:
125 raise KeyError('dictionary is empty')
Raymond Hettinger446a4f22009-04-08 08:28:28 +0000126 key = next(reversed(self) if last else iter(self))
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +0000127 value = self.pop(key)
128 return key, value
129
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000130 def __repr__(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000131 'od.__repr__() <==> repr(od)'
Raymond Hettinger69976a72010-09-12 05:28:42 +0000132 if self.__in_repr:
133 return '...'
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000134 if not self:
135 return '%s()' % (self.__class__.__name__,)
Raymond Hettinger69976a72010-09-12 05:28:42 +0000136 self.__in_repr = True
137 try:
138 result = '%s(%r)' % (self.__class__.__name__, list(self.items()))
139 finally:
140 self.__in_repr = False
141 return result
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000142
143 def copy(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000144 'od.copy() -> a shallow copy of od'
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000145 return self.__class__(self)
146
147 @classmethod
148 def fromkeys(cls, iterable, value=None):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000149 '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
150 and values equal to v (which defaults to None).
151
152 '''
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000153 d = cls()
154 for key in iterable:
155 d[key] = value
156 return d
157
158 def __eq__(self, other):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000159 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
160 while comparison to a regular mapping is order-insensitive.
161
162 '''
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000163 if isinstance(other, OrderedDict):
Raymond Hettinger798ee1a2009-03-23 18:29:11 +0000164 return len(self)==len(other) and \
165 all(p==q for p, q in zip(self.items(), other.items()))
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000166 return dict.__eq__(self, other)
167
Benjamin Peterson2504b7a2009-04-04 17:26:32 +0000168 def __ne__(self, other):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000169 '''od.__ne__(y) <==> od!=y. Comparison to another OD is order-sensitive
170 while comparison to a regular mapping is order-insensitive.
171
172 '''
Benjamin Peterson2504b7a2009-04-04 17:26:32 +0000173 return not self == other
174
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000175
Christian Heimes99170a52007-12-19 02:07:34 +0000176
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000177################################################################################
178### namedtuple
179################################################################################
180
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000181def namedtuple(typename, field_names, verbose=False, rename=False):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000182 """Returns a new subclass of tuple with named fields.
183
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000184 >>> Point = namedtuple('Point', 'x y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000185 >>> Point.__doc__ # docstring for the new class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 'Point(x, y)'
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000187 >>> p = Point(11, y=22) # instantiate with positional args or keywords
Christian Heimes99170a52007-12-19 02:07:34 +0000188 >>> p[0] + p[1] # indexable like a plain tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189 33
Christian Heimes99170a52007-12-19 02:07:34 +0000190 >>> x, y = p # unpack like a regular tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000191 >>> x, y
192 (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000193 >>> p.x + p.y # fields also accessable by name
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 33
Christian Heimes0449f632007-12-15 01:27:15 +0000195 >>> d = p._asdict() # convert to a dictionary
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000196 >>> d['x']
197 11
198 >>> Point(**d) # convert from a dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +0000199 Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000200 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000201 Point(x=100, y=22)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000202
203 """
204
Christian Heimes2380ac72008-01-09 00:17:24 +0000205 # Parse and validate the field names. Validation serves two purposes,
206 # generating informative error messages and preventing template injection attacks.
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000207 if isinstance(field_names, str):
208 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000209 field_names = tuple(map(str, field_names))
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000210 if rename:
211 names = list(field_names)
212 seen = set()
213 for i, name in enumerate(names):
214 if (not all(c.isalnum() or c=='_' for c in name) or _iskeyword(name)
215 or not name or name[0].isdigit() or name.startswith('_')
216 or name in seen):
Raymond Hettinger56145242009-04-02 22:31:59 +0000217 names[i] = '_%d' % i
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000218 seen.add(name)
219 field_names = tuple(names)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000220 for name in (typename,) + field_names:
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000221 if not all(c.isalnum() or c=='_' for c in name):
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000222 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
223 if _iskeyword(name):
224 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
225 if name[0].isdigit():
226 raise ValueError('Type names and field names cannot start with a number: %r' % name)
227 seen_names = set()
228 for name in field_names:
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000229 if name.startswith('_') and not rename:
Christian Heimes0449f632007-12-15 01:27:15 +0000230 raise ValueError('Field names cannot start with an underscore: %r' % name)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000231 if name in seen_names:
232 raise ValueError('Encountered duplicate field name: %r' % name)
233 seen_names.add(name)
234
235 # Create and fill-in the class template
Christian Heimesfaf2f632008-01-06 16:59:19 +0000236 numfields = len(field_names)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000237 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000238 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
239 template = '''class %(typename)s(tuple):
Christian Heimes0449f632007-12-15 01:27:15 +0000240 '%(typename)s(%(argtxt)s)' \n
241 __slots__ = () \n
Christian Heimesfaf2f632008-01-06 16:59:19 +0000242 _fields = %(field_names)r \n
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000243 def __new__(_cls, %(argtxt)s):
244 return _tuple.__new__(_cls, (%(argtxt)s)) \n
Christian Heimesfaf2f632008-01-06 16:59:19 +0000245 @classmethod
Christian Heimes043d6f62008-01-07 17:19:16 +0000246 def _make(cls, iterable, new=tuple.__new__, len=len):
Christian Heimesfaf2f632008-01-06 16:59:19 +0000247 'Make a new %(typename)s object from a sequence or iterable'
Christian Heimes043d6f62008-01-07 17:19:16 +0000248 result = new(cls, iterable)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000249 if len(result) != %(numfields)d:
250 raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
251 return result \n
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000252 def __repr__(self):
Christian Heimes0449f632007-12-15 01:27:15 +0000253 return '%(typename)s(%(reprtxt)s)' %% self \n
Raymond Hettingera4f52b12009-03-02 22:28:31 +0000254 def _asdict(self):
255 'Return a new OrderedDict which maps field names to their values'
256 return OrderedDict(zip(self._fields, self)) \n
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000257 def _replace(_self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000258 'Return a new %(typename)s object replacing specified fields with new values'
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000259 result = _self._make(map(kwds.pop, %(field_names)r, _self))
Christian Heimesfaf2f632008-01-06 16:59:19 +0000260 if kwds:
261 raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000262 return result \n
263 def __getnewargs__(self):
264 return tuple(self) \n\n''' % locals()
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000265 for i, name in enumerate(field_names):
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000266 template += ' %s = _property(_itemgetter(%d))\n' % (name, i)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000267 if verbose:
268 print(template)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000269
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000270 # Execute the template string in a temporary namespace and
271 # support tracing utilities by setting a value for frame.f_globals['__name__']
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000272 namespace = dict(_itemgetter=_itemgetter, __name__='namedtuple_%s' % typename,
273 OrderedDict=OrderedDict, _property=property, _tuple=tuple)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000274 try:
275 exec(template, namespace)
276 except SyntaxError as e:
Christian Heimes99170a52007-12-19 02:07:34 +0000277 raise SyntaxError(e.msg + ':\n' + template) from e
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000278 result = namespace[typename]
279
280 # For pickling to work, the __module__ variable needs to be set to the frame
281 # where the named tuple is created. Bypass this step in enviroments where
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000282 # sys._getframe is not defined (Jython for example) or sys._getframe is not
283 # defined for arguments greater than 0 (IronPython).
284 try:
Raymond Hettinger0f055172009-01-27 10:06:09 +0000285 result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000286 except (AttributeError, ValueError):
287 pass
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000288
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000289 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +0000290
Guido van Rossumd8faa362007-04-27 19:54:29 +0000291
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000292########################################################################
293### Counter
294########################################################################
295
296class Counter(dict):
297 '''Dict subclass for counting hashable items. Sometimes called a bag
298 or multiset. Elements are stored as dictionary keys and their counts
299 are stored as dictionary values.
300
301 >>> c = Counter('abracadabra') # count elements from a string
302
303 >>> c.most_common(3) # three most common elements
304 [('a', 5), ('r', 2), ('b', 2)]
305 >>> sorted(c) # list all unique elements
306 ['a', 'b', 'c', 'd', 'r']
307 >>> ''.join(sorted(c.elements())) # list elements with repetitions
308 'aaaaabbcdrr'
309 >>> sum(c.values()) # total of all counts
310 11
311
312 >>> c['a'] # count of letter 'a'
313 5
314 >>> for elem in 'shazam': # update counts from an iterable
315 ... c[elem] += 1 # by adding 1 to each element's count
316 >>> c['a'] # now there are seven 'a'
317 7
318 >>> del c['r'] # remove all 'r'
319 >>> c['r'] # now there are zero 'r'
320 0
321
322 >>> d = Counter('simsalabim') # make another counter
323 >>> c.update(d) # add in the second counter
324 >>> c['a'] # now there are nine 'a'
325 9
326
327 >>> c.clear() # empty the counter
328 >>> c
329 Counter()
330
331 Note: If a count is set to zero or reduced to zero, it will remain
332 in the counter until the entry is deleted or the counter is cleared:
333
334 >>> c = Counter('aaabbc')
335 >>> c['b'] -= 2 # reduce the count of 'b' by two
336 >>> c.most_common() # 'b' is still in, but its count is zero
337 [('a', 3), ('c', 1), ('b', 0)]
338
339 '''
340 # References:
341 # http://en.wikipedia.org/wiki/Multiset
342 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
343 # http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
344 # http://code.activestate.com/recipes/259174/
345 # Knuth, TAOCP Vol. II section 4.6.3
346
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000347 def __init__(self, iterable=None, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000348 '''Create a new, empty Counter object. And if given, count elements
349 from an input iterable. Or, initialize the count from another mapping
350 of elements to their counts.
351
352 >>> c = Counter() # a new, empty counter
353 >>> c = Counter('gallahad') # a new counter from an iterable
354 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000355 >>> c = Counter(a=4, b=2) # a new counter from keyword args
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000356
357 '''
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000358 self.update(iterable, **kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000359
360 def __missing__(self, key):
361 'The count of elements not in the Counter is zero.'
362 # Needed so that self[missing_item] does not raise KeyError
363 return 0
364
365 def most_common(self, n=None):
366 '''List the n most common elements and their counts from the most
367 common to the least. If n is None, then list all element counts.
368
369 >>> Counter('abracadabra').most_common(3)
370 [('a', 5), ('r', 2), ('b', 2)]
371
372 '''
373 # Emulate Bag.sortedByCount from Smalltalk
374 if n is None:
375 return sorted(self.items(), key=_itemgetter(1), reverse=True)
376 return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
377
378 def elements(self):
379 '''Iterator over elements repeating each as many times as its count.
380
381 >>> c = Counter('ABCABC')
382 >>> sorted(c.elements())
383 ['A', 'A', 'B', 'B', 'C', 'C']
384
385 # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
386 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
387 >>> product = 1
388 >>> for factor in prime_factors.elements(): # loop over factors
389 ... product *= factor # and multiply them
390 >>> product
391 1836
392
393 Note, if an element's count has been set to zero or is a negative
394 number, elements() will ignore it.
395
396 '''
397 # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
398 return _chain.from_iterable(_starmap(_repeat, self.items()))
399
400 # Override dict methods where necessary
401
402 @classmethod
403 def fromkeys(cls, iterable, v=None):
404 # There is no equivalent method for counters because setting v=1
405 # means that no element can have a count greater than one.
406 raise NotImplementedError(
407 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
408
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000409 def update(self, iterable=None, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000410 '''Like dict.update() but add counts instead of replacing them.
411
412 Source can be an iterable, a dictionary, or another Counter instance.
413
414 >>> c = Counter('which')
415 >>> c.update('witch') # add elements from another iterable
416 >>> d = Counter('watch')
417 >>> c.update(d) # add elements from another counter
418 >>> c['h'] # four 'h' in which, witch, and watch
419 4
420
421 '''
422 # The regular dict.update() operation makes no sense here because the
423 # replace behavior results in the some of original untouched counts
424 # being mixed-in with all of the other counts for a mismash that
425 # doesn't have a straight-forward interpretation in most counting
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000426 # contexts. Instead, we implement straight-addition. Both the inputs
427 # and outputs are allowed to contain zero and negative counts.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000428
429 if iterable is not None:
430 if isinstance(iterable, Mapping):
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000431 if self:
Raymond Hettinger77b31ef2009-06-29 18:34:46 +0000432 self_get = self.get
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000433 for elem, count in iterable.items():
Raymond Hettinger77b31ef2009-06-29 18:34:46 +0000434 self[elem] = count + self_get(elem, 0)
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000435 else:
436 dict.update(self, iterable) # fast path when counter is empty
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000437 else:
Raymond Hettinger77b31ef2009-06-29 18:34:46 +0000438 self_get = self.get
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000439 for elem in iterable:
Raymond Hettinger77b31ef2009-06-29 18:34:46 +0000440 self[elem] = 1 + self_get(elem, 0)
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000441 if kwds:
442 self.update(kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000443
444 def copy(self):
445 'Like dict.copy() but returns a Counter instance instead of a dict.'
446 return Counter(self)
447
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000448 def __delitem__(self, elem):
449 'Like dict.__delitem__() but does not raise KeyError for missing values.'
450 if elem in self:
451 dict.__delitem__(self, elem)
452
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000453 def __repr__(self):
454 if not self:
455 return '%s()' % self.__class__.__name__
456 items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
457 return '%s({%s})' % (self.__class__.__name__, items)
458
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000459 # Multiset-style mathematical operations discussed in:
460 # Knuth TAOCP Volume II section 4.6.3 exercise 19
461 # and at http://en.wikipedia.org/wiki/Multiset
462 #
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000463 # Outputs guaranteed to only include positive counts.
464 #
465 # To strip negative and zero counts, add-in an empty counter:
466 # c += Counter()
467
468 def __add__(self, other):
469 '''Add counts from two counters.
470
471 >>> Counter('abbb') + Counter('bcc')
472 Counter({'b': 4, 'c': 2, 'a': 1})
473
474 '''
475 if not isinstance(other, Counter):
476 return NotImplemented
477 result = Counter()
478 for elem in set(self) | set(other):
479 newcount = self[elem] + other[elem]
480 if newcount > 0:
481 result[elem] = newcount
482 return result
483
484 def __sub__(self, other):
485 ''' Subtract count, but keep only results with positive counts.
486
487 >>> Counter('abbbc') - Counter('bccd')
488 Counter({'b': 2, 'a': 1})
489
490 '''
491 if not isinstance(other, Counter):
492 return NotImplemented
493 result = Counter()
Raymond Hettingere0d1b9f2009-01-21 20:36:27 +0000494 for elem in set(self) | set(other):
495 newcount = self[elem] - other[elem]
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000496 if newcount > 0:
497 result[elem] = newcount
498 return result
499
500 def __or__(self, other):
501 '''Union is the maximum of value in either of the input counters.
502
503 >>> Counter('abbb') | Counter('bcc')
504 Counter({'b': 3, 'c': 2, 'a': 1})
505
506 '''
507 if not isinstance(other, Counter):
508 return NotImplemented
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000509 result = Counter()
510 for elem in set(self) | set(other):
Raymond Hettingerc4791702009-04-04 08:48:03 +0000511 p, q = self[elem], other[elem]
512 newcount = q if p < q else p
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000513 if newcount > 0:
514 result[elem] = newcount
515 return result
516
517 def __and__(self, other):
518 ''' Intersection is the minimum of corresponding counts.
519
520 >>> Counter('abbb') & Counter('bcc')
521 Counter({'b': 1})
522
523 '''
524 if not isinstance(other, Counter):
525 return NotImplemented
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000526 result = Counter()
527 if len(self) < len(other):
528 self, other = other, self
529 for elem in filter(self.__contains__, other):
Raymond Hettingerc4791702009-04-04 08:48:03 +0000530 p, q = self[elem], other[elem]
531 newcount = p if p < q else q
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000532 if newcount > 0:
533 result[elem] = newcount
534 return result
535
Guido van Rossumd8faa362007-04-27 19:54:29 +0000536
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000537################################################################################
538### UserDict
539################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000540
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000541class UserDict(MutableMapping):
542
543 # Start by filling-out the abstract methods
544 def __init__(self, dict=None, **kwargs):
545 self.data = {}
546 if dict is not None:
547 self.update(dict)
548 if len(kwargs):
549 self.update(kwargs)
550 def __len__(self): return len(self.data)
551 def __getitem__(self, key):
552 if key in self.data:
553 return self.data[key]
554 if hasattr(self.__class__, "__missing__"):
555 return self.__class__.__missing__(self, key)
556 raise KeyError(key)
557 def __setitem__(self, key, item): self.data[key] = item
558 def __delitem__(self, key): del self.data[key]
559 def __iter__(self):
560 return iter(self.data)
561
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000562 # Modify __contains__ to work correctly when __missing__ is present
563 def __contains__(self, key):
564 return key in self.data
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000565
566 # Now, add the methods in dicts but not in MutableMapping
567 def __repr__(self): return repr(self.data)
568 def copy(self):
569 if self.__class__ is UserDict:
570 return UserDict(self.data.copy())
571 import copy
572 data = self.data
573 try:
574 self.data = {}
575 c = copy.copy(self)
576 finally:
577 self.data = data
578 c.update(self)
579 return c
580 @classmethod
581 def fromkeys(cls, iterable, value=None):
582 d = cls()
583 for key in iterable:
584 d[key] = value
585 return d
586
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000587
588
589################################################################################
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000590### UserList
591################################################################################
592
593class UserList(MutableSequence):
594 """A more or less complete user-defined wrapper around list objects."""
595 def __init__(self, initlist=None):
596 self.data = []
597 if initlist is not None:
598 # XXX should this accept an arbitrary sequence?
599 if type(initlist) == type(self.data):
600 self.data[:] = initlist
601 elif isinstance(initlist, UserList):
602 self.data[:] = initlist.data[:]
603 else:
604 self.data = list(initlist)
605 def __repr__(self): return repr(self.data)
606 def __lt__(self, other): return self.data < self.__cast(other)
607 def __le__(self, other): return self.data <= self.__cast(other)
608 def __eq__(self, other): return self.data == self.__cast(other)
609 def __ne__(self, other): return self.data != self.__cast(other)
610 def __gt__(self, other): return self.data > self.__cast(other)
611 def __ge__(self, other): return self.data >= self.__cast(other)
612 def __cast(self, other):
613 return other.data if isinstance(other, UserList) else other
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000614 def __contains__(self, item): return item in self.data
615 def __len__(self): return len(self.data)
616 def __getitem__(self, i): return self.data[i]
617 def __setitem__(self, i, item): self.data[i] = item
618 def __delitem__(self, i): del self.data[i]
619 def __add__(self, other):
620 if isinstance(other, UserList):
621 return self.__class__(self.data + other.data)
622 elif isinstance(other, type(self.data)):
623 return self.__class__(self.data + other)
624 return self.__class__(self.data + list(other))
625 def __radd__(self, other):
626 if isinstance(other, UserList):
627 return self.__class__(other.data + self.data)
628 elif isinstance(other, type(self.data)):
629 return self.__class__(other + self.data)
630 return self.__class__(list(other) + self.data)
631 def __iadd__(self, other):
632 if isinstance(other, UserList):
633 self.data += other.data
634 elif isinstance(other, type(self.data)):
635 self.data += other
636 else:
637 self.data += list(other)
638 return self
639 def __mul__(self, n):
640 return self.__class__(self.data*n)
641 __rmul__ = __mul__
642 def __imul__(self, n):
643 self.data *= n
644 return self
645 def append(self, item): self.data.append(item)
646 def insert(self, i, item): self.data.insert(i, item)
647 def pop(self, i=-1): return self.data.pop(i)
648 def remove(self, item): self.data.remove(item)
649 def count(self, item): return self.data.count(item)
650 def index(self, item, *args): return self.data.index(item, *args)
651 def reverse(self): self.data.reverse()
652 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
653 def extend(self, other):
654 if isinstance(other, UserList):
655 self.data.extend(other.data)
656 else:
657 self.data.extend(other)
658
659
660
661################################################################################
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000662### UserString
663################################################################################
664
665class UserString(Sequence):
666 def __init__(self, seq):
667 if isinstance(seq, str):
668 self.data = seq
669 elif isinstance(seq, UserString):
670 self.data = seq.data[:]
671 else:
672 self.data = str(seq)
673 def __str__(self): return str(self.data)
674 def __repr__(self): return repr(self.data)
675 def __int__(self): return int(self.data)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000676 def __float__(self): return float(self.data)
677 def __complex__(self): return complex(self.data)
678 def __hash__(self): return hash(self.data)
679
680 def __eq__(self, string):
681 if isinstance(string, UserString):
682 return self.data == string.data
683 return self.data == string
684 def __ne__(self, string):
685 if isinstance(string, UserString):
686 return self.data != string.data
687 return self.data != string
688 def __lt__(self, string):
689 if isinstance(string, UserString):
690 return self.data < string.data
691 return self.data < string
692 def __le__(self, string):
693 if isinstance(string, UserString):
694 return self.data <= string.data
695 return self.data <= string
696 def __gt__(self, string):
697 if isinstance(string, UserString):
698 return self.data > string.data
699 return self.data > string
700 def __ge__(self, string):
701 if isinstance(string, UserString):
702 return self.data >= string.data
703 return self.data >= string
704
705 def __contains__(self, char):
706 if isinstance(char, UserString):
707 char = char.data
708 return char in self.data
709
710 def __len__(self): return len(self.data)
711 def __getitem__(self, index): return self.__class__(self.data[index])
712 def __add__(self, other):
713 if isinstance(other, UserString):
714 return self.__class__(self.data + other.data)
715 elif isinstance(other, str):
716 return self.__class__(self.data + other)
717 return self.__class__(self.data + str(other))
718 def __radd__(self, other):
719 if isinstance(other, str):
720 return self.__class__(other + self.data)
721 return self.__class__(str(other) + self.data)
722 def __mul__(self, n):
723 return self.__class__(self.data*n)
724 __rmul__ = __mul__
725 def __mod__(self, args):
726 return self.__class__(self.data % args)
727
728 # the following methods are defined in alphabetical order:
729 def capitalize(self): return self.__class__(self.data.capitalize())
730 def center(self, width, *args):
731 return self.__class__(self.data.center(width, *args))
732 def count(self, sub, start=0, end=_sys.maxsize):
733 if isinstance(sub, UserString):
734 sub = sub.data
735 return self.data.count(sub, start, end)
736 def encode(self, encoding=None, errors=None): # XXX improve this?
737 if encoding:
738 if errors:
739 return self.__class__(self.data.encode(encoding, errors))
740 return self.__class__(self.data.encode(encoding))
741 return self.__class__(self.data.encode())
742 def endswith(self, suffix, start=0, end=_sys.maxsize):
743 return self.data.endswith(suffix, start, end)
744 def expandtabs(self, tabsize=8):
745 return self.__class__(self.data.expandtabs(tabsize))
746 def find(self, sub, start=0, end=_sys.maxsize):
747 if isinstance(sub, UserString):
748 sub = sub.data
749 return self.data.find(sub, start, end)
750 def format(self, *args, **kwds):
751 return self.data.format(*args, **kwds)
752 def index(self, sub, start=0, end=_sys.maxsize):
753 return self.data.index(sub, start, end)
754 def isalpha(self): return self.data.isalpha()
755 def isalnum(self): return self.data.isalnum()
756 def isdecimal(self): return self.data.isdecimal()
757 def isdigit(self): return self.data.isdigit()
758 def isidentifier(self): return self.data.isidentifier()
759 def islower(self): return self.data.islower()
760 def isnumeric(self): return self.data.isnumeric()
761 def isspace(self): return self.data.isspace()
762 def istitle(self): return self.data.istitle()
763 def isupper(self): return self.data.isupper()
764 def join(self, seq): return self.data.join(seq)
765 def ljust(self, width, *args):
766 return self.__class__(self.data.ljust(width, *args))
767 def lower(self): return self.__class__(self.data.lower())
768 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
769 def partition(self, sep):
770 return self.data.partition(sep)
771 def replace(self, old, new, maxsplit=-1):
772 if isinstance(old, UserString):
773 old = old.data
774 if isinstance(new, UserString):
775 new = new.data
776 return self.__class__(self.data.replace(old, new, maxsplit))
777 def rfind(self, sub, start=0, end=_sys.maxsize):
778 return self.data.rfind(sub, start, end)
779 def rindex(self, sub, start=0, end=_sys.maxsize):
780 return self.data.rindex(sub, start, end)
781 def rjust(self, width, *args):
782 return self.__class__(self.data.rjust(width, *args))
783 def rpartition(self, sep):
784 return self.data.rpartition(sep)
785 def rstrip(self, chars=None):
786 return self.__class__(self.data.rstrip(chars))
787 def split(self, sep=None, maxsplit=-1):
788 return self.data.split(sep, maxsplit)
789 def rsplit(self, sep=None, maxsplit=-1):
790 return self.data.rsplit(sep, maxsplit)
791 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
792 def startswith(self, prefix, start=0, end=_sys.maxsize):
793 return self.data.startswith(prefix, start, end)
794 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
795 def swapcase(self): return self.__class__(self.data.swapcase())
796 def title(self): return self.__class__(self.data.title())
797 def translate(self, *args):
798 return self.__class__(self.data.translate(*args))
799 def upper(self): return self.__class__(self.data.upper())
800 def zfill(self, width): return self.__class__(self.data.zfill(width))
801
802
803
804################################################################################
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000805### Simple tests
806################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000807
808if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000809 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000810 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000811 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000812 p = Point(x=10, y=20)
813 assert p == loads(dumps(p))
814
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000815 # test and demonstrate ability to override methods
Christian Heimes043d6f62008-01-07 17:19:16 +0000816 class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000817 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000818 @property
819 def hypot(self):
820 return (self.x ** 2 + self.y ** 2) ** 0.5
Christian Heimes790c8232008-01-07 21:14:23 +0000821 def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000822 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Christian Heimes043d6f62008-01-07 17:19:16 +0000823
Christian Heimes25bb7832008-01-11 16:17:00 +0000824 for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes790c8232008-01-07 21:14:23 +0000825 print (p)
Christian Heimes043d6f62008-01-07 17:19:16 +0000826
827 class Point(namedtuple('Point', 'x y')):
828 'Point class with optimized _make() and _replace() without error-checking'
Christian Heimes25bb7832008-01-11 16:17:00 +0000829 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000830 _make = classmethod(tuple.__new__)
831 def _replace(self, _map=map, **kwds):
Christian Heimes2380ac72008-01-09 00:17:24 +0000832 return self._make(_map(kwds.get, ('x', 'y'), self))
Christian Heimes043d6f62008-01-07 17:19:16 +0000833
834 print(Point(11, 22)._replace(x=100))
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000835
Christian Heimes25bb7832008-01-11 16:17:00 +0000836 Point3D = namedtuple('Point3D', Point._fields + ('z',))
837 print(Point3D.__doc__)
838
Guido van Rossumd8faa362007-04-27 19:54:29 +0000839 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000840 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000841 print(TestResults(*doctest.testmod()))