blob: 85b4c3c19acc39008b893db4a31c463f34dd1710 [file] [log] [blame]
Raymond Hettingerc9c3dd82015-11-23 20:43:28 -08001'''This module implements specialized container datatypes providing
Raymond Hettingerc3f7d172015-11-23 21:03:09 -08002alternatives to Python's general purpose built-in containers, dict,
Raymond Hettingerc9c3dd82015-11-23 20:43:28 -08003list, set, and tuple.
4
5* namedtuple factory function for creating tuple subclasses with named fields
6* deque list-like container with fast appends and pops on either end
7* ChainMap dict-like class for creating a single view of multiple mappings
8* Counter dict subclass for counting hashable objects
9* OrderedDict dict subclass that remembers the order entries were added
10* defaultdict dict subclass that calls a factory function to supply missing values
11* UserDict wrapper around dictionary objects for easier dict subclassing
12* UserList wrapper around list objects for easier list subclassing
13* UserString wrapper around string objects for easier string subclassing
14
15'''
16
Brett Cannon23a4a7b2008-05-12 00:56:28 +000017__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
Benjamin Petersonc92f6222011-09-11 12:55:34 -040018 'UserString', 'Counter', 'OrderedDict', 'ChainMap']
Raymond Hettinger158c9c22011-02-22 00:41:50 +000019
Ezio Melotti37308922011-03-15 06:03:08 +020020# For backwards compatibility, continue to make the collections ABCs
Raymond Hettinger158c9c22011-02-22 00:41:50 +000021# available through the collections module.
Christian Heimesf1dc3ee2013-10-13 02:04:20 +020022from _collections_abc import *
23import _collections_abc
24__all__ += _collections_abc.__all__
Guido van Rossumcd16bf62007-06-13 18:07:49 +000025
Raymond Hettinger1c2018c2012-06-09 22:51:39 -070026from operator import itemgetter as _itemgetter, eq as _eq
Christian Heimes99170a52007-12-19 02:07:34 +000027from keyword import iskeyword as _iskeyword
28import sys as _sys
Raymond Hettingerb8baf632009-01-14 02:20:07 +000029import heapq as _heapq
Richard Oudkerk7a3dae02013-05-05 23:05:00 +010030from _weakref import proxy as _proxy
Raymond Hettingerea9f8db2009-03-02 21:28:41 +000031from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000032from reprlib import recursive_repr as _recursive_repr
Raymond Hettinger2d32f632009-03-02 21:24:57 +000033
Eric Snow96c6af92015-05-29 22:21:39 -060034try:
35 from _collections import deque
36except ImportError:
37 pass
38else:
39 MutableSequence.register(deque)
40
41try:
42 from _collections import defaultdict
43except ImportError:
44 pass
45
Raymond Hettinger32ea1652015-03-21 01:37:37 -070046
Raymond Hettinger2d32f632009-03-02 21:24:57 +000047################################################################################
48### OrderedDict
49################################################################################
50
Serhiy Storchaka578c9212014-04-04 15:19:36 +030051class _OrderedDictKeysView(KeysView):
52
53 def __reversed__(self):
54 yield from reversed(self._mapping)
55
56class _OrderedDictItemsView(ItemsView):
57
58 def __reversed__(self):
59 for key in reversed(self._mapping):
60 yield (key, self._mapping[key])
61
62class _OrderedDictValuesView(ValuesView):
63
64 def __reversed__(self):
65 for key in reversed(self._mapping):
66 yield self._mapping[key]
67
Raymond Hettingerfa11db02010-09-12 04:12:42 +000068class _Link(object):
69 __slots__ = 'prev', 'next', 'key', '__weakref__'
70
Raymond Hettinger345c49b2011-01-01 23:51:55 +000071class OrderedDict(dict):
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +000072 'Dictionary that remembers insertion order'
Raymond Hettingerf1736542009-03-23 05:19:21 +000073 # An inherited dict maps keys to values.
Raymond Hettinger18ed2cb2009-03-19 23:14:39 +000074 # The inherited dict provides __getitem__, __len__, __contains__, and get.
75 # The remaining methods are order-aware.
Raymond Hettingera82aa552011-04-24 14:34:26 -070076 # Big-O running times for all methods are the same as regular dictionaries.
Raymond Hettingerf1736542009-03-23 05:19:21 +000077
Raymond Hettingera82aa552011-04-24 14:34:26 -070078 # The internal self.__map dict maps keys to links in a doubly linked list.
Raymond Hettingerf1736542009-03-23 05:19:21 +000079 # The circular doubly linked list starts and ends with a sentinel element.
80 # The sentinel element never gets deleted (this simplifies the algorithm).
Raymond Hettingera82aa552011-04-24 14:34:26 -070081 # The sentinel is in self.__hardroot with a weakref proxy in self.__root.
Raymond Hettingereaac4f02012-01-26 00:14:16 -080082 # The prev links are weakref proxies (to prevent circular references).
Raymond Hettingerc5c29c02010-09-12 18:13:46 +000083 # Individual links are kept alive by the hard reference in self.__map.
84 # Those hard references disappear when a key is deleted from an OrderedDict.
Raymond Hettinger2d32f632009-03-02 21:24:57 +000085
Serhiy Storchakaae5cb212014-11-27 16:25:51 +020086 def __init__(*args, **kwds):
Raymond Hettingera82aa552011-04-24 14:34:26 -070087 '''Initialize an ordered dictionary. The signature is the same as
88 regular dictionaries, but keyword arguments are not recommended because
89 their insertion order is arbitrary.
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +000090
91 '''
Serhiy Storchakaae5cb212014-11-27 16:25:51 +020092 if not args:
93 raise TypeError("descriptor '__init__' of 'OrderedDict' object "
94 "needs an argument")
95 self, *args = args
Raymond Hettinger2d32f632009-03-02 21:24:57 +000096 if len(args) > 1:
97 raise TypeError('expected at most 1 arguments, got %d' % len(args))
Raymond Hettinger08c70cf2009-03-03 20:47:29 +000098 try:
Raymond Hettingerf1736542009-03-23 05:19:21 +000099 self.__root
Raymond Hettinger08c70cf2009-03-03 20:47:29 +0000100 except AttributeError:
Raymond Hettingerf7328d02010-09-14 19:40:15 +0000101 self.__hardroot = _Link()
102 self.__root = root = _proxy(self.__hardroot)
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000103 root.prev = root.next = root
Raymond Hettinger52dc06b2009-03-25 22:45:22 +0000104 self.__map = {}
Raymond Hettinger32062e92011-01-01 22:38:00 +0000105 self.__update(*args, **kwds)
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000106
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000107 def __setitem__(self, key, value,
108 dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000109 'od.__setitem__(i, y) <==> od[i]=y'
Raymond Hettingera82aa552011-04-24 14:34:26 -0700110 # Setting a new item creates a new link at the end of the linked list,
111 # and the inherited dictionary is updated with the new key/value pair.
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000112 if key not in self:
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000113 self.__map[key] = link = Link()
Raymond Hettingerf1736542009-03-23 05:19:21 +0000114 root = self.__root
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000115 last = root.prev
116 link.prev, link.next, link.key = last, root, key
Raymond Hettingerf7328d02010-09-14 19:40:15 +0000117 last.next = link
118 root.prev = proxy(link)
119 dict_setitem(self, key, value)
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000120
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000121 def __delitem__(self, key, dict_delitem=dict.__delitem__):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000122 'od.__delitem__(y) <==> del od[y]'
Raymond Hettingera82aa552011-04-24 14:34:26 -0700123 # Deleting an existing item uses self.__map to find the link which gets
124 # removed by updating the links in the predecessor and successor nodes.
Raymond Hettinger5be21b72010-08-01 22:10:57 +0000125 dict_delitem(self, key)
Raymond Hettingerf1736542009-03-23 05:19:21 +0000126 link = self.__map.pop(key)
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000127 link_prev = link.prev
128 link_next = link.next
129 link_prev.next = link_next
130 link_next.prev = link_prev
Raymond Hettinger53d2c412014-05-03 21:58:45 -0700131 link.prev = None
132 link.next = None
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000133
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000134 def __iter__(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000135 'od.__iter__() <==> iter(od)'
Raymond Hettingerf1736542009-03-23 05:19:21 +0000136 # Traverse the linked list in order.
137 root = self.__root
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000138 curr = root.next
Raymond Hettingerf1736542009-03-23 05:19:21 +0000139 while curr is not root:
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000140 yield curr.key
141 curr = curr.next
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000142
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000143 def __reversed__(self):
Raymond Hettinger2352cf32009-04-08 01:16:27 +0000144 'od.__reversed__() <==> reversed(od)'
Raymond Hettingerf1736542009-03-23 05:19:21 +0000145 # Traverse the linked list in reverse order.
146 root = self.__root
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000147 curr = root.prev
Raymond Hettingerf1736542009-03-23 05:19:21 +0000148 while curr is not root:
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000149 yield curr.key
150 curr = curr.prev
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000151
Raymond Hettinger5be21b72010-08-01 22:10:57 +0000152 def clear(self):
153 'od.clear() -> None. Remove all items from od.'
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000154 root = self.__root
155 root.prev = root.next = root
156 self.__map.clear()
Raymond Hettinger5be21b72010-08-01 22:10:57 +0000157 dict.clear(self)
158
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000159 def popitem(self, last=True):
Raymond Hettinger331722d2010-09-02 18:44:16 +0000160 '''od.popitem() -> (k, v), return and remove a (key, value) pair.
161 Pairs are returned in LIFO order if last is true or FIFO order if false.
162
163 '''
164 if not self:
165 raise KeyError('dictionary is empty')
166 root = self.__root
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000167 if last:
168 link = root.prev
169 link_prev = link.prev
170 link_prev.next = root
171 root.prev = link_prev
172 else:
173 link = root.next
174 link_next = link.next
175 root.next = link_next
176 link_next.prev = root
177 key = link.key
Raymond Hettinger331722d2010-09-02 18:44:16 +0000178 del self.__map[key]
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000179 value = dict.pop(self, key)
Raymond Hettinger331722d2010-09-02 18:44:16 +0000180 return key, value
181
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000182 def move_to_end(self, key, last=True):
183 '''Move an existing element to the end (or beginning if last==False).
184
185 Raises KeyError if the element does not exist.
186 When last=True, acts like a fast version of self[key]=self.pop(key).
187
188 '''
189 link = self.__map[key]
190 link_prev = link.prev
191 link_next = link.next
Raymond Hettingerb46ea902016-12-31 12:01:59 -0700192 soft_link = link_next.prev
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000193 link_prev.next = link_next
194 link_next.prev = link_prev
195 root = self.__root
196 if last:
197 last = root.prev
198 link.prev = last
199 link.next = root
Raymond Hettingerb46ea902016-12-31 12:01:59 -0700200 root.prev = soft_link
201 last.next = link
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000202 else:
203 first = root.next
204 link.prev = root
205 link.next = first
Raymond Hettingerb46ea902016-12-31 12:01:59 -0700206 first.prev = soft_link
207 root.next = link
Raymond Hettingerfa11db02010-09-12 04:12:42 +0000208
Raymond Hettinger35c87f22010-09-16 19:10:17 +0000209 def __sizeof__(self):
210 sizeof = _sys.getsizeof
211 n = len(self) + 1 # number of links including root
212 size = sizeof(self.__dict__) # instance dictionary
213 size += sizeof(self.__map) * 2 # internal dict and inherited dict
214 size += sizeof(self.__hardroot) * n # link objects
215 size += sizeof(self.__root) * n # proxy objects
216 return size
217
Raymond Hettinger32062e92011-01-01 22:38:00 +0000218 update = __update = MutableMapping.update
Serhiy Storchaka578c9212014-04-04 15:19:36 +0300219
220 def keys(self):
221 "D.keys() -> a set-like object providing a view on D's keys"
222 return _OrderedDictKeysView(self)
223
224 def items(self):
225 "D.items() -> a set-like object providing a view on D's items"
226 return _OrderedDictItemsView(self)
227
228 def values(self):
229 "D.values() -> an object providing a view on D's values"
230 return _OrderedDictValuesView(self)
231
Raymond Hettinger5be21b72010-08-01 22:10:57 +0000232 __ne__ = MutableMapping.__ne__
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000233
Raymond Hettinger345c49b2011-01-01 23:51:55 +0000234 __marker = object()
235
236 def pop(self, key, default=__marker):
Raymond Hettingera82aa552011-04-24 14:34:26 -0700237 '''od.pop(k[,d]) -> v, remove specified key and return the corresponding
238 value. If key is not found, d is returned if given, otherwise KeyError
239 is raised.
240
241 '''
Raymond Hettinger345c49b2011-01-01 23:51:55 +0000242 if key in self:
243 result = self[key]
244 del self[key]
245 return result
246 if default is self.__marker:
247 raise KeyError(key)
248 return default
249
Raymond Hettingera673b1f2010-12-31 23:16:17 +0000250 def setdefault(self, key, default=None):
Raymond Hettingera82aa552011-04-24 14:34:26 -0700251 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
Raymond Hettingera673b1f2010-12-31 23:16:17 +0000252 if key in self:
253 return self[key]
254 self[key] = default
255 return default
256
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000257 @_recursive_repr()
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000258 def __repr__(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000259 'od.__repr__() <==> repr(od)'
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000260 if not self:
261 return '%s()' % (self.__class__.__name__,)
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000262 return '%s(%r)' % (self.__class__.__name__, list(self.items()))
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000263
Raymond Hettingerfc330ae2011-04-20 13:03:49 -0700264 def __reduce__(self):
265 'Return state information for pickling'
Raymond Hettingerfc330ae2011-04-20 13:03:49 -0700266 inst_dict = vars(self).copy()
267 for k in vars(OrderedDict()):
268 inst_dict.pop(k, None)
Serhiy Storchaka3ee6dab2013-05-21 12:47:57 +0300269 return self.__class__, (), inst_dict or None, None, iter(self.items())
Raymond Hettingerfc330ae2011-04-20 13:03:49 -0700270
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000271 def copy(self):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000272 'od.copy() -> a shallow copy of od'
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000273 return self.__class__(self)
274
275 @classmethod
276 def fromkeys(cls, iterable, value=None):
Raymond Hettingera82aa552011-04-24 14:34:26 -0700277 '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
278 If not specified, the value defaults to None.
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000279
280 '''
Raymond Hettingera82aa552011-04-24 14:34:26 -0700281 self = cls()
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000282 for key in iterable:
Raymond Hettingera82aa552011-04-24 14:34:26 -0700283 self[key] = value
284 return self
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000285
286 def __eq__(self, other):
Raymond Hettingerf04fa1b2009-04-08 01:15:02 +0000287 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
288 while comparison to a regular mapping is order-insensitive.
289
290 '''
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000291 if isinstance(other, OrderedDict):
Raymond Hettinger527507d2012-12-07 10:18:22 -0800292 return dict.__eq__(self, other) and all(map(_eq, self, other))
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000293 return dict.__eq__(self, other)
294
Christian Heimes99170a52007-12-19 02:07:34 +0000295
Eric Snow96c6af92015-05-29 22:21:39 -0600296try:
297 from _collections import OrderedDict
298except ImportError:
299 # Leave the pure Python version in place.
300 pass
301
302
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000303################################################################################
304### namedtuple
305################################################################################
306
Raymond Hettinger587d3bf2013-05-17 16:43:14 -0700307_class_template = """\
Raymond Hettinger843a7512011-03-23 11:49:56 -0700308from builtins import property as _property, tuple as _tuple
309from operator import itemgetter as _itemgetter
310from collections import OrderedDict
311
Raymond Hettingerfef85462011-03-22 21:14:41 -0700312class {typename}(tuple):
313 '{typename}({arg_list})'
314
315 __slots__ = ()
316
317 _fields = {field_names!r}
318
319 def __new__(_cls, {arg_list}):
320 'Create new instance of {typename}({arg_list})'
321 return _tuple.__new__(_cls, ({arg_list}))
322
323 @classmethod
324 def _make(cls, iterable, new=tuple.__new__, len=len):
325 'Make a new {typename} object from a sequence or iterable'
326 result = new(cls, iterable)
327 if len(result) != {num_fields:d}:
328 raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
329 return result
330
Raymond Hettingerfef85462011-03-22 21:14:41 -0700331 def _replace(_self, **kwds):
332 'Return a new {typename} object replacing specified fields with new values'
333 result = _self._make(map(kwds.pop, {field_names!r}, _self))
334 if kwds:
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700335 raise ValueError('Got unexpected field names: %r' % list(kwds))
Raymond Hettingerfef85462011-03-22 21:14:41 -0700336 return result
337
Raymond Hettinger587d3bf2013-05-17 16:43:14 -0700338 def __repr__(self):
339 'Return a nicely formatted representation string'
340 return self.__class__.__name__ + '({repr_fmt})' % self
341
Raymond Hettinger587d3bf2013-05-17 16:43:14 -0700342 def _asdict(self):
Raymond Hettingerd852e992014-03-20 06:42:31 -0700343 'Return a new OrderedDict which maps field names to their values.'
Raymond Hettinger7a3602e2015-08-30 09:13:48 -0700344 return OrderedDict(zip(self._fields, self))
Raymond Hettinger587d3bf2013-05-17 16:43:14 -0700345
Raymond Hettingerfef85462011-03-22 21:14:41 -0700346 def __getnewargs__(self):
347 'Return self as a plain tuple. Used by copy and pickle.'
348 return tuple(self)
349
350{field_defs}
Raymond Hettinger587d3bf2013-05-17 16:43:14 -0700351"""
Raymond Hettingerfef85462011-03-22 21:14:41 -0700352
353_repr_template = '{name}=%r'
354
355_field_template = '''\
356 {name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
357'''
358
Raymond Hettinger0d5048c2016-09-12 00:18:31 -0700359def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000360 """Returns a new subclass of tuple with named fields.
361
Raymond Hettingerfef85462011-03-22 21:14:41 -0700362 >>> Point = namedtuple('Point', ['x', 'y'])
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000363 >>> Point.__doc__ # docstring for the new class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000364 'Point(x, y)'
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000365 >>> p = Point(11, y=22) # instantiate with positional args or keywords
Christian Heimes99170a52007-12-19 02:07:34 +0000366 >>> p[0] + p[1] # indexable like a plain tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000367 33
Christian Heimes99170a52007-12-19 02:07:34 +0000368 >>> x, y = p # unpack like a regular tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000369 >>> x, y
370 (11, 22)
Martin Panter46f50722016-05-26 05:35:26 +0000371 >>> p.x + p.y # fields also accessible by name
Guido van Rossumd8faa362007-04-27 19:54:29 +0000372 33
Christian Heimes0449f632007-12-15 01:27:15 +0000373 >>> d = p._asdict() # convert to a dictionary
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000374 >>> d['x']
375 11
376 >>> Point(**d) # convert from a dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +0000377 Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000378 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000379 Point(x=100, y=22)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000380
381 """
382
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700383 # Validate the field names. At the user's option, either generate an error
384 # message or automatically replace the field name with a valid name.
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000385 if isinstance(field_names, str):
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700386 field_names = field_names.replace(',', ' ').split()
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700387 field_names = list(map(str, field_names))
Raymond Hettingerbc000502014-06-24 15:20:55 -0700388 typename = str(typename)
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000389 if rename:
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000390 seen = set()
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700391 for index, name in enumerate(field_names):
Raymond Hettinger4f4ba162013-03-01 23:43:48 -0800392 if (not name.isidentifier()
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700393 or _iskeyword(name)
Raymond Hettinger15d0c1d2011-03-23 14:38:39 -0700394 or name.startswith('_')
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000395 or name in seen):
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700396 field_names[index] = '_%d' % index
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000397 seen.add(name)
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700398 for name in [typename] + field_names:
Raymond Hettinger8579a8f2016-08-17 00:46:48 -0700399 if type(name) is not str:
Raymond Hettingerbc000502014-06-24 15:20:55 -0700400 raise TypeError('Type names and field names must be strings')
Raymond Hettinger4f4ba162013-03-01 23:43:48 -0800401 if not name.isidentifier():
402 raise ValueError('Type names and field names must be valid '
403 'identifiers: %r' % name)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000404 if _iskeyword(name):
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700405 raise ValueError('Type names and field names cannot be a '
406 'keyword: %r' % name)
Raymond Hettinger2ebea412011-03-23 12:52:23 -0700407 seen = set()
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000408 for name in field_names:
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000409 if name.startswith('_') and not rename:
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700410 raise ValueError('Field names cannot start with an underscore: '
411 '%r' % name)
Raymond Hettinger2ebea412011-03-23 12:52:23 -0700412 if name in seen:
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000413 raise ValueError('Encountered duplicate field name: %r' % name)
Raymond Hettinger2ebea412011-03-23 12:52:23 -0700414 seen.add(name)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000415
Raymond Hettingerfef85462011-03-22 21:14:41 -0700416 # Fill-in the class template
417 class_definition = _class_template.format(
Raymond Hettinger3e82ae02011-03-22 14:21:38 -0700418 typename = typename,
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700419 field_names = tuple(field_names),
Raymond Hettingerfef85462011-03-22 21:14:41 -0700420 num_fields = len(field_names),
Raymond Hettingerb2d09452011-03-22 22:36:21 -0700421 arg_list = repr(tuple(field_names)).replace("'", "")[1:-1],
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700422 repr_fmt = ', '.join(_repr_template.format(name=name)
423 for name in field_names),
Raymond Hettingerfef85462011-03-22 21:14:41 -0700424 field_defs = '\n'.join(_field_template.format(index=index, name=name)
425 for index, name in enumerate(field_names))
Raymond Hettinger3e82ae02011-03-22 14:21:38 -0700426 )
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000427
Raymond Hettinger80ed4d42012-06-09 18:46:45 -0700428 # Execute the template string in a temporary namespace and support
429 # tracing utilities by setting a value for frame.f_globals['__name__']
Raymond Hettingerf6d3e8e2011-03-23 20:33:30 -0700430 namespace = dict(__name__='namedtuple_%s' % typename)
Raymond Hettingerb37706f2013-05-17 02:28:33 -0700431 exec(class_definition, namespace)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000432 result = namespace[typename]
Raymond Hettinger2ebea412011-03-23 12:52:23 -0700433 result._source = class_definition
434 if verbose:
435 print(result._source)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000436
437 # For pickling to work, the __module__ variable needs to be set to the frame
Ezio Melotti85a86292013-08-17 16:57:41 +0300438 # where the named tuple is created. Bypass this step in environments where
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000439 # sys._getframe is not defined (Jython for example) or sys._getframe is not
Raymond Hettinger0d5048c2016-09-12 00:18:31 -0700440 # defined for arguments greater than 0 (IronPython), or where the user has
441 # specified a particular module.
442 if module is None:
443 try:
444 module = _sys._getframe(1).f_globals.get('__name__', '__main__')
445 except (AttributeError, ValueError):
446 pass
447 if module is not None:
448 result.__module__ = module
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000449
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000450 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +0000451
Guido van Rossumd8faa362007-04-27 19:54:29 +0000452
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000453########################################################################
454### Counter
455########################################################################
456
Raymond Hettinger96f34102010-12-15 16:30:37 +0000457def _count_elements(mapping, iterable):
458 'Tally elements from the iterable.'
459 mapping_get = mapping.get
460 for elem in iterable:
461 mapping[elem] = mapping_get(elem, 0) + 1
462
463try: # Load C helper function if available
464 from _collections import _count_elements
Brett Cannoncd171c82013-07-04 17:43:24 -0400465except ImportError:
Raymond Hettinger96f34102010-12-15 16:30:37 +0000466 pass
467
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000468class Counter(dict):
469 '''Dict subclass for counting hashable items. Sometimes called a bag
470 or multiset. Elements are stored as dictionary keys and their counts
471 are stored as dictionary values.
472
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000473 >>> c = Counter('abcdeabcdabcaba') # count elements from a string
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000474
475 >>> c.most_common(3) # three most common elements
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000476 [('a', 5), ('b', 4), ('c', 3)]
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000477 >>> sorted(c) # list all unique elements
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000478 ['a', 'b', 'c', 'd', 'e']
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000479 >>> ''.join(sorted(c.elements())) # list elements with repetitions
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000480 'aaaaabbbbcccdde'
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000481 >>> sum(c.values()) # total of all counts
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000482 15
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000483
484 >>> c['a'] # count of letter 'a'
485 5
486 >>> for elem in 'shazam': # update counts from an iterable
487 ... c[elem] += 1 # by adding 1 to each element's count
488 >>> c['a'] # now there are seven 'a'
489 7
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000490 >>> del c['b'] # remove all 'b'
491 >>> c['b'] # now there are zero 'b'
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000492 0
493
494 >>> d = Counter('simsalabim') # make another counter
495 >>> c.update(d) # add in the second counter
496 >>> c['a'] # now there are nine 'a'
497 9
498
499 >>> c.clear() # empty the counter
500 >>> c
501 Counter()
502
503 Note: If a count is set to zero or reduced to zero, it will remain
504 in the counter until the entry is deleted or the counter is cleared:
505
506 >>> c = Counter('aaabbc')
507 >>> c['b'] -= 2 # reduce the count of 'b' by two
508 >>> c.most_common() # 'b' is still in, but its count is zero
509 [('a', 3), ('c', 1), ('b', 0)]
510
511 '''
512 # References:
513 # http://en.wikipedia.org/wiki/Multiset
514 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
515 # http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
516 # http://code.activestate.com/recipes/259174/
517 # Knuth, TAOCP Vol. II section 4.6.3
518
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200519 def __init__(*args, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000520 '''Create a new, empty Counter object. And if given, count elements
521 from an input iterable. Or, initialize the count from another mapping
522 of elements to their counts.
523
524 >>> c = Counter() # a new, empty counter
525 >>> c = Counter('gallahad') # a new counter from an iterable
526 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000527 >>> c = Counter(a=4, b=2) # a new counter from keyword args
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000528
529 '''
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200530 if not args:
531 raise TypeError("descriptor '__init__' of 'Counter' object "
532 "needs an argument")
533 self, *args = args
534 if len(args) > 1:
535 raise TypeError('expected at most 1 arguments, got %d' % len(args))
536 super(Counter, self).__init__()
537 self.update(*args, **kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000538
539 def __missing__(self, key):
540 'The count of elements not in the Counter is zero.'
541 # Needed so that self[missing_item] does not raise KeyError
542 return 0
543
544 def most_common(self, n=None):
545 '''List the n most common elements and their counts from the most
546 common to the least. If n is None, then list all element counts.
547
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000548 >>> Counter('abcdeabcdabcaba').most_common(3)
549 [('a', 5), ('b', 4), ('c', 3)]
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000550
551 '''
552 # Emulate Bag.sortedByCount from Smalltalk
553 if n is None:
554 return sorted(self.items(), key=_itemgetter(1), reverse=True)
555 return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
556
557 def elements(self):
558 '''Iterator over elements repeating each as many times as its count.
559
560 >>> c = Counter('ABCABC')
561 >>> sorted(c.elements())
562 ['A', 'A', 'B', 'B', 'C', 'C']
563
564 # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
565 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
566 >>> product = 1
567 >>> for factor in prime_factors.elements(): # loop over factors
568 ... product *= factor # and multiply them
569 >>> product
570 1836
571
572 Note, if an element's count has been set to zero or is a negative
573 number, elements() will ignore it.
574
575 '''
576 # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
577 return _chain.from_iterable(_starmap(_repeat, self.items()))
578
579 # Override dict methods where necessary
580
581 @classmethod
582 def fromkeys(cls, iterable, v=None):
583 # There is no equivalent method for counters because setting v=1
584 # means that no element can have a count greater than one.
585 raise NotImplementedError(
586 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
587
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200588 def update(*args, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000589 '''Like dict.update() but add counts instead of replacing them.
590
591 Source can be an iterable, a dictionary, or another Counter instance.
592
593 >>> c = Counter('which')
594 >>> c.update('witch') # add elements from another iterable
595 >>> d = Counter('watch')
596 >>> c.update(d) # add elements from another counter
597 >>> c['h'] # four 'h' in which, witch, and watch
598 4
599
600 '''
601 # The regular dict.update() operation makes no sense here because the
602 # replace behavior results in the some of original untouched counts
603 # being mixed-in with all of the other counts for a mismash that
604 # doesn't have a straight-forward interpretation in most counting
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000605 # contexts. Instead, we implement straight-addition. Both the inputs
606 # and outputs are allowed to contain zero and negative counts.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000607
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200608 if not args:
609 raise TypeError("descriptor 'update' of 'Counter' object "
610 "needs an argument")
611 self, *args = args
612 if len(args) > 1:
613 raise TypeError('expected at most 1 arguments, got %d' % len(args))
614 iterable = args[0] if args else None
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000615 if iterable is not None:
616 if isinstance(iterable, Mapping):
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000617 if self:
Raymond Hettingerf9092022009-06-29 18:30:43 +0000618 self_get = self.get
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000619 for elem, count in iterable.items():
Raymond Hettingerf9092022009-06-29 18:30:43 +0000620 self[elem] = count + self_get(elem, 0)
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000621 else:
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200622 super(Counter, self).update(iterable) # fast path when counter is empty
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000623 else:
Raymond Hettinger96f34102010-12-15 16:30:37 +0000624 _count_elements(self, iterable)
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000625 if kwds:
626 self.update(kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000627
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200628 def subtract(*args, **kwds):
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000629 '''Like dict.update() but subtracts counts instead of replacing them.
630 Counts can be reduced below zero. Both the inputs and outputs are
631 allowed to contain zero and negative counts.
632
633 Source can be an iterable, a dictionary, or another Counter instance.
634
635 >>> c = Counter('which')
636 >>> c.subtract('witch') # subtract elements from another iterable
637 >>> c.subtract(Counter('watch')) # subtract elements from another counter
638 >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
639 0
640 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
641 -1
642
643 '''
Serhiy Storchakaae5cb212014-11-27 16:25:51 +0200644 if not args:
645 raise TypeError("descriptor 'subtract' of 'Counter' object "
646 "needs an argument")
647 self, *args = args
648 if len(args) > 1:
649 raise TypeError('expected at most 1 arguments, got %d' % len(args))
650 iterable = args[0] if args else None
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000651 if iterable is not None:
Raymond Hettingerfc3c9cd2010-04-11 20:41:56 +0000652 self_get = self.get
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000653 if isinstance(iterable, Mapping):
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000654 for elem, count in iterable.items():
655 self[elem] = self_get(elem, 0) - count
656 else:
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000657 for elem in iterable:
658 self[elem] = self_get(elem, 0) - 1
659 if kwds:
660 self.subtract(kwds)
661
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000662 def copy(self):
Raymond Hettinger1c746c22011-04-15 13:16:46 -0700663 'Return a shallow copy.'
664 return self.__class__(self)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000665
Raymond Hettingerff728162011-01-03 02:44:14 +0000666 def __reduce__(self):
667 return self.__class__, (dict(self),)
668
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000669 def __delitem__(self, elem):
670 'Like dict.__delitem__() but does not raise KeyError for missing values.'
671 if elem in self:
Raymond Hettinger00d43fd2011-01-02 08:03:33 +0000672 super().__delitem__(elem)
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000673
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000674 def __repr__(self):
675 if not self:
676 return '%s()' % self.__class__.__name__
Raymond Hettinger4e6bf412011-11-05 13:35:26 -0700677 try:
678 items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
679 return '%s({%s})' % (self.__class__.__name__, items)
680 except TypeError:
681 # handle case where values are not orderable
682 return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000683
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000684 # Multiset-style mathematical operations discussed in:
685 # Knuth TAOCP Volume II section 4.6.3 exercise 19
686 # and at http://en.wikipedia.org/wiki/Multiset
687 #
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000688 # Outputs guaranteed to only include positive counts.
689 #
690 # To strip negative and zero counts, add-in an empty counter:
691 # c += Counter()
692
693 def __add__(self, other):
694 '''Add counts from two counters.
695
696 >>> Counter('abbb') + Counter('bcc')
697 Counter({'b': 4, 'c': 2, 'a': 1})
698
699 '''
700 if not isinstance(other, Counter):
701 return NotImplemented
702 result = Counter()
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700703 for elem, count in self.items():
704 newcount = count + other[elem]
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000705 if newcount > 0:
706 result[elem] = newcount
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700707 for elem, count in other.items():
708 if elem not in self and count > 0:
709 result[elem] = count
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000710 return result
711
712 def __sub__(self, other):
713 ''' Subtract count, but keep only results with positive counts.
714
715 >>> Counter('abbbc') - Counter('bccd')
716 Counter({'b': 2, 'a': 1})
717
718 '''
719 if not isinstance(other, Counter):
720 return NotImplemented
721 result = Counter()
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700722 for elem, count in self.items():
723 newcount = count - other[elem]
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000724 if newcount > 0:
725 result[elem] = newcount
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700726 for elem, count in other.items():
727 if elem not in self and count < 0:
728 result[elem] = 0 - count
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000729 return result
730
731 def __or__(self, other):
732 '''Union is the maximum of value in either of the input counters.
733
734 >>> Counter('abbb') | Counter('bcc')
735 Counter({'b': 3, 'c': 2, 'a': 1})
736
737 '''
738 if not isinstance(other, Counter):
739 return NotImplemented
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000740 result = Counter()
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700741 for elem, count in self.items():
742 other_count = other[elem]
743 newcount = other_count if count < other_count else count
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000744 if newcount > 0:
745 result[elem] = newcount
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700746 for elem, count in other.items():
747 if elem not in self and count > 0:
748 result[elem] = count
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000749 return result
750
751 def __and__(self, other):
752 ''' Intersection is the minimum of corresponding counts.
753
754 >>> Counter('abbb') & Counter('bcc')
755 Counter({'b': 1})
756
757 '''
758 if not isinstance(other, Counter):
759 return NotImplemented
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000760 result = Counter()
Raymond Hettinger90375bc2011-04-17 19:49:29 -0700761 for elem, count in self.items():
762 other_count = other[elem]
763 newcount = count if count < other_count else other_count
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000764 if newcount > 0:
765 result[elem] = newcount
766 return result
767
Raymond Hettingerfcb393c2011-08-09 13:00:40 -0700768 def __pos__(self):
769 'Adds an empty counter, effectively stripping negative and zero counts'
Raymond Hettinger4c97a622015-05-29 22:14:07 -0700770 result = Counter()
771 for elem, count in self.items():
772 if count > 0:
773 result[elem] = count
774 return result
Raymond Hettingerfcb393c2011-08-09 13:00:40 -0700775
776 def __neg__(self):
777 '''Subtracts from an empty counter. Strips positive and zero counts,
778 and flips the sign on negative counts.
779
780 '''
Raymond Hettinger4c97a622015-05-29 22:14:07 -0700781 result = Counter()
782 for elem, count in self.items():
783 if count < 0:
784 result[elem] = 0 - count
785 return result
Raymond Hettingerfcb393c2011-08-09 13:00:40 -0700786
Raymond Hettingerbecd5682011-10-19 13:40:37 -0700787 def _keep_positive(self):
788 '''Internal method to strip elements with a negative or zero count'''
789 nonpositive = [elem for elem, count in self.items() if not count > 0]
790 for elem in nonpositive:
791 del self[elem]
792 return self
793
794 def __iadd__(self, other):
795 '''Inplace add from another counter, keeping only positive counts.
796
797 >>> c = Counter('abbb')
798 >>> c += Counter('bcc')
799 >>> c
800 Counter({'b': 4, 'c': 2, 'a': 1})
801
802 '''
803 for elem, count in other.items():
804 self[elem] += count
805 return self._keep_positive()
806
807 def __isub__(self, other):
808 '''Inplace subtract counter, but keep only results with positive counts.
809
810 >>> c = Counter('abbbc')
811 >>> c -= Counter('bccd')
812 >>> c
813 Counter({'b': 2, 'a': 1})
814
815 '''
816 for elem, count in other.items():
817 self[elem] -= count
818 return self._keep_positive()
819
820 def __ior__(self, other):
821 '''Inplace union is the maximum of value from either counter.
822
823 >>> c = Counter('abbb')
824 >>> c |= Counter('bcc')
825 >>> c
826 Counter({'b': 3, 'c': 2, 'a': 1})
827
828 '''
829 for elem, other_count in other.items():
830 count = self[elem]
831 if other_count > count:
832 self[elem] = other_count
833 return self._keep_positive()
834
835 def __iand__(self, other):
836 '''Inplace intersection is the minimum of corresponding counts.
837
838 >>> c = Counter('abbb')
839 >>> c &= Counter('bcc')
840 >>> c
841 Counter({'b': 1})
842
843 '''
844 for elem, count in self.items():
845 other_count = other[elem]
846 if other_count < count:
847 self[elem] = other_count
848 return self._keep_positive()
849
Guido van Rossumd8faa362007-04-27 19:54:29 +0000850
Raymond Hettingerddb52402011-02-21 19:42:11 +0000851########################################################################
Raymond Hettinger62bc3212016-02-25 00:25:45 -0800852### ChainMap
Raymond Hettingerddb52402011-02-21 19:42:11 +0000853########################################################################
854
Raymond Hettinger9fe1ccf2011-02-26 01:02:51 +0000855class ChainMap(MutableMapping):
Raymond Hettingerddb52402011-02-21 19:42:11 +0000856 ''' A ChainMap groups multiple dicts (or other mappings) together
857 to create a single, updateable view.
858
859 The underlying mappings are stored in a list. That list is public and can
Martin Panter8d56c022016-05-29 04:13:35 +0000860 be accessed or updated using the *maps* attribute. There is no other
861 state.
Raymond Hettingerddb52402011-02-21 19:42:11 +0000862
863 Lookups search the underlying mappings successively until a key is found.
864 In contrast, writes, updates, and deletions only operate on the first
865 mapping.
866
867 '''
868
869 def __init__(self, *maps):
870 '''Initialize a ChainMap by setting *maps* to the given mappings.
871 If no mappings are provided, a single empty dictionary is used.
872
873 '''
874 self.maps = list(maps) or [{}] # always at least one map
875
876 def __missing__(self, key):
877 raise KeyError(key)
878
879 def __getitem__(self, key):
880 for mapping in self.maps:
881 try:
882 return mapping[key] # can't use 'key in mapping' with defaultdict
883 except KeyError:
884 pass
885 return self.__missing__(key) # support subclasses that define __missing__
886
887 def get(self, key, default=None):
888 return self[key] if key in self else default
889
890 def __len__(self):
891 return len(set().union(*self.maps)) # reuses stored hash values if possible
892
893 def __iter__(self):
894 return iter(set().union(*self.maps))
895
896 def __contains__(self, key):
897 return any(key in m for m in self.maps)
898
Raymond Hettingerd0321312011-02-26 06:53:58 +0000899 def __bool__(self):
900 return any(self.maps)
901
Raymond Hettingerddb52402011-02-21 19:42:11 +0000902 @_recursive_repr()
903 def __repr__(self):
904 return '{0.__class__.__name__}({1})'.format(
905 self, ', '.join(map(repr, self.maps)))
906
907 @classmethod
908 def fromkeys(cls, iterable, *args):
909 'Create a ChainMap with a single dict created from the iterable.'
910 return cls(dict.fromkeys(iterable, *args))
911
912 def copy(self):
913 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
914 return self.__class__(self.maps[0].copy(), *self.maps[1:])
915
916 __copy__ = copy
917
Vinay Sajip1ba81ee2013-01-11 23:39:53 +0000918 def new_child(self, m=None): # like Django's Context.push()
Raymond Hettinger8ba03cf2015-09-08 00:36:29 -0400919 '''New ChainMap with a new map followed by all previous maps.
920 If no map is provided, an empty dict is used.
Vinay Sajip1ba81ee2013-01-11 23:39:53 +0000921 '''
922 if m is None:
923 m = {}
924 return self.__class__(m, *self.maps)
Raymond Hettinger499e1932011-02-23 07:56:53 +0000925
926 @property
927 def parents(self): # like Django's Context.pop()
928 'New ChainMap from maps[1:].'
929 return self.__class__(*self.maps[1:])
930
Raymond Hettingerddb52402011-02-21 19:42:11 +0000931 def __setitem__(self, key, value):
932 self.maps[0][key] = value
933
934 def __delitem__(self, key):
935 try:
936 del self.maps[0][key]
937 except KeyError:
938 raise KeyError('Key not found in the first mapping: {!r}'.format(key))
939
940 def popitem(self):
941 'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
942 try:
943 return self.maps[0].popitem()
944 except KeyError:
945 raise KeyError('No keys found in the first mapping.')
946
947 def pop(self, key, *args):
948 'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
949 try:
950 return self.maps[0].pop(key, *args)
951 except KeyError:
952 raise KeyError('Key not found in the first mapping: {!r}'.format(key))
953
954 def clear(self):
955 'Clear maps[0], leaving maps[1:] intact.'
956 self.maps[0].clear()
957
958
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000959################################################################################
960### UserDict
961################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000962
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000963class UserDict(MutableMapping):
964
965 # Start by filling-out the abstract methods
Serhiy Storchaka68f5ef22015-09-29 23:36:06 +0300966 def __init__(*args, **kwargs):
967 if not args:
968 raise TypeError("descriptor '__init__' of 'UserDict' object "
969 "needs an argument")
970 self, *args = args
971 if len(args) > 1:
972 raise TypeError('expected at most 1 arguments, got %d' % len(args))
973 if args:
974 dict = args[0]
975 elif 'dict' in kwargs:
976 dict = kwargs.pop('dict')
977 import warnings
978 warnings.warn("Passing 'dict' as keyword argument is deprecated",
Serhiy Storchaka5527cf12015-09-29 23:38:34 +0300979 DeprecationWarning, stacklevel=2)
Serhiy Storchaka68f5ef22015-09-29 23:36:06 +0300980 else:
981 dict = None
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000982 self.data = {}
983 if dict is not None:
984 self.update(dict)
985 if len(kwargs):
986 self.update(kwargs)
987 def __len__(self): return len(self.data)
988 def __getitem__(self, key):
989 if key in self.data:
990 return self.data[key]
991 if hasattr(self.__class__, "__missing__"):
992 return self.__class__.__missing__(self, key)
993 raise KeyError(key)
994 def __setitem__(self, key, item): self.data[key] = item
995 def __delitem__(self, key): del self.data[key]
996 def __iter__(self):
997 return iter(self.data)
998
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000999 # Modify __contains__ to work correctly when __missing__ is present
1000 def __contains__(self, key):
1001 return key in self.data
Raymond Hettinger48b8b662008-02-05 01:53:00 +00001002
1003 # Now, add the methods in dicts but not in MutableMapping
1004 def __repr__(self): return repr(self.data)
1005 def copy(self):
1006 if self.__class__ is UserDict:
1007 return UserDict(self.data.copy())
1008 import copy
1009 data = self.data
1010 try:
1011 self.data = {}
1012 c = copy.copy(self)
1013 finally:
1014 self.data = data
1015 c.update(self)
1016 return c
1017 @classmethod
1018 def fromkeys(cls, iterable, value=None):
1019 d = cls()
1020 for key in iterable:
1021 d[key] = value
1022 return d
1023
Raymond Hettinger48b8b662008-02-05 01:53:00 +00001024
1025
1026################################################################################
Raymond Hettinger53dbe392008-02-12 20:03:09 +00001027### UserList
1028################################################################################
1029
1030class UserList(MutableSequence):
1031 """A more or less complete user-defined wrapper around list objects."""
1032 def __init__(self, initlist=None):
1033 self.data = []
1034 if initlist is not None:
1035 # XXX should this accept an arbitrary sequence?
1036 if type(initlist) == type(self.data):
1037 self.data[:] = initlist
1038 elif isinstance(initlist, UserList):
1039 self.data[:] = initlist.data[:]
1040 else:
1041 self.data = list(initlist)
1042 def __repr__(self): return repr(self.data)
1043 def __lt__(self, other): return self.data < self.__cast(other)
1044 def __le__(self, other): return self.data <= self.__cast(other)
1045 def __eq__(self, other): return self.data == self.__cast(other)
Raymond Hettinger53dbe392008-02-12 20:03:09 +00001046 def __gt__(self, other): return self.data > self.__cast(other)
1047 def __ge__(self, other): return self.data >= self.__cast(other)
1048 def __cast(self, other):
1049 return other.data if isinstance(other, UserList) else other
Raymond Hettinger53dbe392008-02-12 20:03:09 +00001050 def __contains__(self, item): return item in self.data
1051 def __len__(self): return len(self.data)
1052 def __getitem__(self, i): return self.data[i]
1053 def __setitem__(self, i, item): self.data[i] = item
1054 def __delitem__(self, i): del self.data[i]
1055 def __add__(self, other):
1056 if isinstance(other, UserList):
1057 return self.__class__(self.data + other.data)
1058 elif isinstance(other, type(self.data)):
1059 return self.__class__(self.data + other)
1060 return self.__class__(self.data + list(other))
1061 def __radd__(self, other):
1062 if isinstance(other, UserList):
1063 return self.__class__(other.data + self.data)
1064 elif isinstance(other, type(self.data)):
1065 return self.__class__(other + self.data)
1066 return self.__class__(list(other) + self.data)
1067 def __iadd__(self, other):
1068 if isinstance(other, UserList):
1069 self.data += other.data
1070 elif isinstance(other, type(self.data)):
1071 self.data += other
1072 else:
1073 self.data += list(other)
1074 return self
1075 def __mul__(self, n):
1076 return self.__class__(self.data*n)
1077 __rmul__ = __mul__
1078 def __imul__(self, n):
1079 self.data *= n
1080 return self
1081 def append(self, item): self.data.append(item)
1082 def insert(self, i, item): self.data.insert(i, item)
1083 def pop(self, i=-1): return self.data.pop(i)
1084 def remove(self, item): self.data.remove(item)
Eli Benderskycbbaa962011-02-25 05:47:53 +00001085 def clear(self): self.data.clear()
Raymond Hettinger1c7b7f72011-05-05 14:34:35 -07001086 def copy(self): return self.__class__(self)
Raymond Hettinger53dbe392008-02-12 20:03:09 +00001087 def count(self, item): return self.data.count(item)
1088 def index(self, item, *args): return self.data.index(item, *args)
1089 def reverse(self): self.data.reverse()
1090 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
1091 def extend(self, other):
1092 if isinstance(other, UserList):
1093 self.data.extend(other.data)
1094 else:
1095 self.data.extend(other)
1096
1097
1098
1099################################################################################
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001100### UserString
1101################################################################################
1102
1103class UserString(Sequence):
1104 def __init__(self, seq):
1105 if isinstance(seq, str):
1106 self.data = seq
1107 elif isinstance(seq, UserString):
1108 self.data = seq.data[:]
1109 else:
1110 self.data = str(seq)
1111 def __str__(self): return str(self.data)
1112 def __repr__(self): return repr(self.data)
1113 def __int__(self): return int(self.data)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001114 def __float__(self): return float(self.data)
1115 def __complex__(self): return complex(self.data)
1116 def __hash__(self): return hash(self.data)
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001117 def __getnewargs__(self):
1118 return (self.data[:],)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001119
1120 def __eq__(self, string):
1121 if isinstance(string, UserString):
1122 return self.data == string.data
1123 return self.data == string
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001124 def __lt__(self, string):
1125 if isinstance(string, UserString):
1126 return self.data < string.data
1127 return self.data < string
1128 def __le__(self, string):
1129 if isinstance(string, UserString):
1130 return self.data <= string.data
1131 return self.data <= string
1132 def __gt__(self, string):
1133 if isinstance(string, UserString):
1134 return self.data > string.data
1135 return self.data > string
1136 def __ge__(self, string):
1137 if isinstance(string, UserString):
1138 return self.data >= string.data
1139 return self.data >= string
1140
1141 def __contains__(self, char):
1142 if isinstance(char, UserString):
1143 char = char.data
1144 return char in self.data
1145
1146 def __len__(self): return len(self.data)
1147 def __getitem__(self, index): return self.__class__(self.data[index])
1148 def __add__(self, other):
1149 if isinstance(other, UserString):
1150 return self.__class__(self.data + other.data)
1151 elif isinstance(other, str):
1152 return self.__class__(self.data + other)
1153 return self.__class__(self.data + str(other))
1154 def __radd__(self, other):
1155 if isinstance(other, str):
1156 return self.__class__(other + self.data)
1157 return self.__class__(str(other) + self.data)
1158 def __mul__(self, n):
1159 return self.__class__(self.data*n)
1160 __rmul__ = __mul__
1161 def __mod__(self, args):
1162 return self.__class__(self.data % args)
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001163 def __rmod__(self, format):
1164 return self.__class__(format % args)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001165
1166 # the following methods are defined in alphabetical order:
1167 def capitalize(self): return self.__class__(self.data.capitalize())
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001168 def casefold(self):
1169 return self.__class__(self.data.casefold())
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001170 def center(self, width, *args):
1171 return self.__class__(self.data.center(width, *args))
1172 def count(self, sub, start=0, end=_sys.maxsize):
1173 if isinstance(sub, UserString):
1174 sub = sub.data
1175 return self.data.count(sub, start, end)
1176 def encode(self, encoding=None, errors=None): # XXX improve this?
1177 if encoding:
1178 if errors:
1179 return self.__class__(self.data.encode(encoding, errors))
1180 return self.__class__(self.data.encode(encoding))
1181 return self.__class__(self.data.encode())
1182 def endswith(self, suffix, start=0, end=_sys.maxsize):
1183 return self.data.endswith(suffix, start, end)
1184 def expandtabs(self, tabsize=8):
1185 return self.__class__(self.data.expandtabs(tabsize))
1186 def find(self, sub, start=0, end=_sys.maxsize):
1187 if isinstance(sub, UserString):
1188 sub = sub.data
1189 return self.data.find(sub, start, end)
1190 def format(self, *args, **kwds):
1191 return self.data.format(*args, **kwds)
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001192 def format_map(self, mapping):
1193 return self.data.format_map(mapping)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001194 def index(self, sub, start=0, end=_sys.maxsize):
1195 return self.data.index(sub, start, end)
1196 def isalpha(self): return self.data.isalpha()
1197 def isalnum(self): return self.data.isalnum()
1198 def isdecimal(self): return self.data.isdecimal()
1199 def isdigit(self): return self.data.isdigit()
1200 def isidentifier(self): return self.data.isidentifier()
1201 def islower(self): return self.data.islower()
1202 def isnumeric(self): return self.data.isnumeric()
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001203 def isprintable(self): return self.data.isprintable()
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001204 def isspace(self): return self.data.isspace()
1205 def istitle(self): return self.data.istitle()
1206 def isupper(self): return self.data.isupper()
1207 def join(self, seq): return self.data.join(seq)
1208 def ljust(self, width, *args):
1209 return self.__class__(self.data.ljust(width, *args))
1210 def lower(self): return self.__class__(self.data.lower())
1211 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
Raymond Hettinger573b44c2015-05-22 16:56:32 -07001212 maketrans = str.maketrans
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001213 def partition(self, sep):
1214 return self.data.partition(sep)
1215 def replace(self, old, new, maxsplit=-1):
1216 if isinstance(old, UserString):
1217 old = old.data
1218 if isinstance(new, UserString):
1219 new = new.data
1220 return self.__class__(self.data.replace(old, new, maxsplit))
1221 def rfind(self, sub, start=0, end=_sys.maxsize):
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +00001222 if isinstance(sub, UserString):
1223 sub = sub.data
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001224 return self.data.rfind(sub, start, end)
1225 def rindex(self, sub, start=0, end=_sys.maxsize):
1226 return self.data.rindex(sub, start, end)
1227 def rjust(self, width, *args):
1228 return self.__class__(self.data.rjust(width, *args))
1229 def rpartition(self, sep):
1230 return self.data.rpartition(sep)
1231 def rstrip(self, chars=None):
1232 return self.__class__(self.data.rstrip(chars))
1233 def split(self, sep=None, maxsplit=-1):
1234 return self.data.split(sep, maxsplit)
1235 def rsplit(self, sep=None, maxsplit=-1):
1236 return self.data.rsplit(sep, maxsplit)
Ezio Melottid8b509b2011-09-28 17:37:55 +03001237 def splitlines(self, keepends=False): return self.data.splitlines(keepends)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +00001238 def startswith(self, prefix, start=0, end=_sys.maxsize):
1239 return self.data.startswith(prefix, start, end)
1240 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
1241 def swapcase(self): return self.__class__(self.data.swapcase())
1242 def title(self): return self.__class__(self.data.title())
1243 def translate(self, *args):
1244 return self.__class__(self.data.translate(*args))
1245 def upper(self): return self.__class__(self.data.upper())
1246 def zfill(self, width): return self.__class__(self.data.zfill(width))