blob: a60d91e7cb8f15da2cbf3113e60502b8acee2dd2 [file] [log] [blame]
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001# Copyright 2007 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Abstract Base Classes (ABCs) for collections, according to PEP 3119.
5
6DON'T USE THIS MODULE DIRECTLY! The classes here should be imported
Guido van Rossum7eaf8222007-06-18 17:58:50 +00007via collections; they are defined here only to alleviate certain
Guido van Rossumcd16bf62007-06-13 18:07:49 +00008bootstrapping issues. Unit tests are in test_collections.
9"""
10
11from abc import ABCMeta, abstractmethod
Benjamin Peterson41181742008-07-02 20:22:54 +000012import sys
Guido van Rossumcd16bf62007-06-13 18:07:49 +000013
14__all__ = ["Hashable", "Iterable", "Iterator",
15 "Sized", "Container", "Callable",
16 "Set", "MutableSet",
17 "Mapping", "MutableMapping",
18 "MappingView", "KeysView", "ItemsView", "ValuesView",
19 "Sequence", "MutableSequence",
Guido van Rossumd05eb002007-11-21 22:26:24 +000020 "ByteString",
Christian Heimesf83be4e2007-11-28 09:44:38 +000021 "bytearray_iterator", "bytes_iterator", "dict_itemiterator",
Christian Heimes0db38532007-11-29 16:21:13 +000022 "dict_items", "dict_keyiterator", "dict_keys", "dict_proxy",
Christian Heimesf83be4e2007-11-28 09:44:38 +000023 "dict_valueiterator", "dict_values", "list_iterator",
24 "list_reverseiterator", "range_iterator", "set_iterator",
25 "str_iterator", "tuple_iterator", "zip_iterator",
Guido van Rossumcd16bf62007-06-13 18:07:49 +000026 ]
27
Christian Heimesf83be4e2007-11-28 09:44:38 +000028
29### collection related types which are not exposed through builtin ###
30## iterators ##
31bytes_iterator = type(iter(b''))
32bytearray_iterator = type(iter(bytearray()))
33#callable_iterator = ???
34dict_keyiterator = type(iter({}.keys()))
35dict_valueiterator = type(iter({}.values()))
36dict_itemiterator = type(iter({}.items()))
37list_iterator = type(iter([]))
38list_reverseiterator = type(iter(reversed([])))
39range_iterator = type(iter(range(0)))
40set_iterator = type(iter(set()))
41str_iterator = type(iter(""))
42tuple_iterator = type(iter(()))
43zip_iterator = type(iter(zip()))
44## views ##
45dict_keys = type({}.keys())
46dict_values = type({}.values())
47dict_items = type({}.items())
Christian Heimes0db38532007-11-29 16:21:13 +000048## misc ##
49dict_proxy = type(type.__dict__)
Christian Heimesf83be4e2007-11-28 09:44:38 +000050
51
Guido van Rossumcd16bf62007-06-13 18:07:49 +000052### ONE-TRICK PONIES ###
53
54class Hashable(metaclass=ABCMeta):
55
56 @abstractmethod
57 def __hash__(self):
58 return 0
59
60 @classmethod
61 def __subclasshook__(cls, C):
62 if cls is Hashable:
63 for B in C.__mro__:
64 if "__hash__" in B.__dict__:
65 if B.__dict__["__hash__"]:
66 return True
67 break
68 return NotImplemented
69
70
71class Iterable(metaclass=ABCMeta):
72
73 @abstractmethod
74 def __iter__(self):
75 while False:
76 yield None
77
78 @classmethod
79 def __subclasshook__(cls, C):
80 if cls is Iterable:
81 if any("__iter__" in B.__dict__ for B in C.__mro__):
82 return True
83 return NotImplemented
84
Guido van Rossumcd16bf62007-06-13 18:07:49 +000085
Raymond Hettinger74b64952008-02-09 02:53:48 +000086class Iterator(Iterable):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000087
88 @abstractmethod
89 def __next__(self):
90 raise StopIteration
91
92 def __iter__(self):
93 return self
94
95 @classmethod
96 def __subclasshook__(cls, C):
97 if cls is Iterator:
98 if any("__next__" in B.__dict__ for B in C.__mro__):
99 return True
100 return NotImplemented
101
Christian Heimesf83be4e2007-11-28 09:44:38 +0000102Iterator.register(bytes_iterator)
103Iterator.register(bytearray_iterator)
104#Iterator.register(callable_iterator)
105Iterator.register(dict_keyiterator)
106Iterator.register(dict_valueiterator)
107Iterator.register(dict_itemiterator)
108Iterator.register(list_iterator)
109Iterator.register(list_reverseiterator)
110Iterator.register(range_iterator)
111Iterator.register(set_iterator)
112Iterator.register(str_iterator)
113Iterator.register(tuple_iterator)
114Iterator.register(zip_iterator)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000115
116class Sized(metaclass=ABCMeta):
117
118 @abstractmethod
119 def __len__(self):
120 return 0
121
122 @classmethod
123 def __subclasshook__(cls, C):
124 if cls is Sized:
125 if any("__len__" in B.__dict__ for B in C.__mro__):
126 return True
127 return NotImplemented
128
129
130class Container(metaclass=ABCMeta):
131
132 @abstractmethod
133 def __contains__(self, x):
134 return False
135
136 @classmethod
137 def __subclasshook__(cls, C):
138 if cls is Container:
139 if any("__contains__" in B.__dict__ for B in C.__mro__):
140 return True
141 return NotImplemented
142
143
144class Callable(metaclass=ABCMeta):
145
146 @abstractmethod
Christian Heimes78644762008-03-04 23:39:23 +0000147 def __call__(self, *args, **kwds):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000148 return False
149
150 @classmethod
151 def __subclasshook__(cls, C):
152 if cls is Callable:
153 if any("__call__" in B.__dict__ for B in C.__mro__):
154 return True
155 return NotImplemented
156
157
158### SETS ###
159
160
Raymond Hettinger74b64952008-02-09 02:53:48 +0000161class Set(Sized, Iterable, Container):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000162
163 """A set is a finite, iterable container.
164
165 This class provides concrete generic implementations of all
166 methods except for __contains__, __iter__ and __len__.
167
168 To override the comparisons (presumably for speed, as the
169 semantics are fixed), all you have to do is redefine __le__ and
170 then the other operations will automatically follow suit.
171 """
172
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000173 def __le__(self, other):
174 if not isinstance(other, Set):
175 return NotImplemented
176 if len(self) > len(other):
177 return False
178 for elem in self:
179 if elem not in other:
180 return False
181 return True
182
183 def __lt__(self, other):
184 if not isinstance(other, Set):
185 return NotImplemented
186 return len(self) < len(other) and self.__le__(other)
187
Raymond Hettinger71909422008-02-09 00:08:16 +0000188 def __gt__(self, other):
189 if not isinstance(other, Set):
190 return NotImplemented
191 return other < self
192
193 def __ge__(self, other):
194 if not isinstance(other, Set):
195 return NotImplemented
196 return other <= self
197
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000198 def __eq__(self, other):
199 if not isinstance(other, Set):
200 return NotImplemented
201 return len(self) == len(other) and self.__le__(other)
202
Raymond Hettinger71909422008-02-09 00:08:16 +0000203 def __ne__(self, other):
204 return not (self == other)
205
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000206 @classmethod
207 def _from_iterable(cls, it):
Raymond Hettinger8284c4a2008-02-06 20:47:09 +0000208 '''Construct an instance of the class from any iterable input.
209
210 Must override this method if the class constructor signature
Raymond Hettinger7aebb642008-02-09 03:25:08 +0000211 does not accept an iterable for an input.
Raymond Hettinger8284c4a2008-02-06 20:47:09 +0000212 '''
Raymond Hettinger7aebb642008-02-09 03:25:08 +0000213 return cls(it)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000214
215 def __and__(self, other):
216 if not isinstance(other, Iterable):
217 return NotImplemented
218 return self._from_iterable(value for value in other if value in self)
219
Christian Heimes190d79e2008-01-30 11:58:22 +0000220 def isdisjoint(self, other):
221 for value in other:
222 if value in self:
223 return False
224 return True
225
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000226 def __or__(self, other):
227 if not isinstance(other, Iterable):
228 return NotImplemented
Christian Heimes78644762008-03-04 23:39:23 +0000229 chain = (e for s in (self, other) for e in s)
230 return self._from_iterable(chain)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000231
232 def __sub__(self, other):
233 if not isinstance(other, Set):
234 if not isinstance(other, Iterable):
235 return NotImplemented
236 other = self._from_iterable(other)
237 return self._from_iterable(value for value in self
238 if value not in other)
239
240 def __xor__(self, other):
241 if not isinstance(other, Set):
242 if not isinstance(other, Iterable):
243 return NotImplemented
244 other = self._from_iterable(other)
245 return (self - other) | (other - self)
246
247 def _hash(self):
248 """Compute the hash value of a set.
249
250 Note that we don't define __hash__: not all sets are hashable.
251 But if you define a hashable set type, its __hash__ should
252 call this function.
253
254 This must be compatible __eq__.
255
256 All sets ought to compare equal if they contain the same
257 elements, regardless of how they are implemented, and
258 regardless of the order of the elements; so there's not much
259 freedom for __eq__ or __hash__. We match the algorithm used
260 by the built-in frozenset type.
261 """
Christian Heimesa37d4c62007-12-04 23:02:19 +0000262 MAX = sys.maxsize
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000263 MASK = 2 * MAX + 1
264 n = len(self)
265 h = 1927868237 * (n + 1)
266 h &= MASK
267 for x in self:
268 hx = hash(x)
269 h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
270 h &= MASK
271 h = h * 69069 + 907133923
272 h &= MASK
273 if h > MAX:
274 h -= MASK + 1
275 if h == -1:
276 h = 590923713
277 return h
278
279Set.register(frozenset)
280
281
282class MutableSet(Set):
283
284 @abstractmethod
285 def add(self, value):
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000286 """Add an element."""
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000287 raise NotImplementedError
288
289 @abstractmethod
290 def discard(self, value):
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000291 """Remove an element. Do not raise an exception if absent."""
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000292 raise NotImplementedError
293
Christian Heimes190d79e2008-01-30 11:58:22 +0000294 def remove(self, value):
295 """Remove an element. If not a member, raise a KeyError."""
296 if value not in self:
297 raise KeyError(value)
298 self.discard(value)
299
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000300 def pop(self):
301 """Return the popped value. Raise KeyError if empty."""
302 it = iter(self)
303 try:
Raymond Hettingerae650182009-01-28 23:33:59 +0000304 value = next(it)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000305 except StopIteration:
306 raise KeyError
307 self.discard(value)
308 return value
309
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000310 def clear(self):
311 """This is slow (creates N new iterators!) but effective."""
312 try:
313 while True:
314 self.pop()
315 except KeyError:
316 pass
317
318 def __ior__(self, it: Iterable):
319 for value in it:
320 self.add(value)
321 return self
322
Raymond Hettinger3f10a952009-04-01 19:05:50 +0000323 def __iand__(self, it: Iterable):
324 for value in (self - it):
325 self.discard(value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000326 return self
327
328 def __ixor__(self, it: Iterable):
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000329 if not isinstance(it, Set):
330 it = self._from_iterable(it)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000331 for value in it:
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000332 if value in self:
333 self.discard(value)
334 else:
335 self.add(value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000336 return self
337
338 def __isub__(self, it: Iterable):
339 for value in it:
340 self.discard(value)
341 return self
342
343MutableSet.register(set)
344
345
346### MAPPINGS ###
347
348
Raymond Hettinger74b64952008-02-09 02:53:48 +0000349class Mapping(Sized, Iterable, Container):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000350
351 @abstractmethod
352 def __getitem__(self, key):
353 raise KeyError
354
355 def get(self, key, default=None):
356 try:
357 return self[key]
358 except KeyError:
359 return default
360
361 def __contains__(self, key):
362 try:
363 self[key]
364 except KeyError:
365 return False
366 else:
367 return True
368
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000369 def keys(self):
370 return KeysView(self)
371
372 def items(self):
373 return ItemsView(self)
374
375 def values(self):
376 return ValuesView(self)
377
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000378 def __eq__(self, other):
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000379 return isinstance(other, Mapping) and \
380 dict(self.items()) == dict(other.items())
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000381
382 def __ne__(self, other):
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000383 return not (self == other)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000384
Christian Heimes2202f872008-02-06 14:31:34 +0000385
Raymond Hettingerbfd06122008-02-09 10:04:32 +0000386class MappingView(Sized):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000387
388 def __init__(self, mapping):
389 self._mapping = mapping
390
391 def __len__(self):
392 return len(self._mapping)
393
Raymond Hettinger89fc2b72009-02-27 07:47:32 +0000394 def __repr__(self):
395 return '{0.__class__.__name__}({0._mapping!r})'.format(self)
396
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000397
398class KeysView(MappingView, Set):
399
400 def __contains__(self, key):
401 return key in self._mapping
402
403 def __iter__(self):
404 for key in self._mapping:
405 yield key
406
Christian Heimesf83be4e2007-11-28 09:44:38 +0000407KeysView.register(dict_keys)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000408
409
410class ItemsView(MappingView, Set):
411
412 def __contains__(self, item):
413 key, value = item
414 try:
415 v = self._mapping[key]
416 except KeyError:
417 return False
418 else:
419 return v == value
420
421 def __iter__(self):
422 for key in self._mapping:
423 yield (key, self._mapping[key])
424
Christian Heimesf83be4e2007-11-28 09:44:38 +0000425ItemsView.register(dict_items)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000426
427
428class ValuesView(MappingView):
429
430 def __contains__(self, value):
431 for key in self._mapping:
432 if value == self._mapping[key]:
433 return True
434 return False
435
436 def __iter__(self):
437 for key in self._mapping:
438 yield self._mapping[key]
439
Christian Heimesf83be4e2007-11-28 09:44:38 +0000440ValuesView.register(dict_values)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000441
442
443class MutableMapping(Mapping):
444
445 @abstractmethod
446 def __setitem__(self, key, value):
447 raise KeyError
448
449 @abstractmethod
450 def __delitem__(self, key):
451 raise KeyError
452
453 __marker = object()
454
455 def pop(self, key, default=__marker):
456 try:
457 value = self[key]
458 except KeyError:
459 if default is self.__marker:
460 raise
461 return default
462 else:
463 del self[key]
464 return value
465
466 def popitem(self):
467 try:
468 key = next(iter(self))
469 except StopIteration:
470 raise KeyError
471 value = self[key]
472 del self[key]
473 return key, value
474
475 def clear(self):
476 try:
477 while True:
478 self.popitem()
479 except KeyError:
480 pass
481
482 def update(self, other=(), **kwds):
483 if isinstance(other, Mapping):
484 for key in other:
485 self[key] = other[key]
486 elif hasattr(other, "keys"):
487 for key in other.keys():
488 self[key] = other[key]
489 else:
490 for key, value in other:
491 self[key] = value
492 for key, value in kwds.items():
493 self[key] = value
494
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000495 def setdefault(self, key, default=None):
496 try:
497 return self[key]
498 except KeyError:
499 self[key] = default
500 return default
501
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000502MutableMapping.register(dict)
503
504
505### SEQUENCES ###
506
507
Raymond Hettinger74b64952008-02-09 02:53:48 +0000508class Sequence(Sized, Iterable, Container):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000509
510 """All the operations on a read-only sequence.
511
512 Concrete subclasses must override __new__ or __init__,
513 __getitem__, and __len__.
514 """
515
516 @abstractmethod
517 def __getitem__(self, index):
518 raise IndexError
519
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000520 def __iter__(self):
521 i = 0
Raymond Hettinger71909422008-02-09 00:08:16 +0000522 try:
523 while True:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000524 v = self[i]
Raymond Hettinger71909422008-02-09 00:08:16 +0000525 yield v
526 i += 1
527 except IndexError:
528 return
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000529
530 def __contains__(self, value):
531 for v in self:
532 if v == value:
533 return True
534 return False
535
536 def __reversed__(self):
537 for i in reversed(range(len(self))):
538 yield self[i]
539
540 def index(self, value):
541 for i, v in enumerate(self):
542 if v == value:
543 return i
544 raise ValueError
545
546 def count(self, value):
547 return sum(1 for v in self if v == value)
548
549Sequence.register(tuple)
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000550Sequence.register(str)
Raymond Hettinger9aa53c22009-02-24 11:25:35 +0000551Sequence.register(range)
Guido van Rossumd05eb002007-11-21 22:26:24 +0000552
553
554class ByteString(Sequence):
555
556 """This unifies bytes and bytearray.
557
558 XXX Should add all their methods.
559 """
560
561ByteString.register(bytes)
562ByteString.register(bytearray)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000563
564
565class MutableSequence(Sequence):
566
567 @abstractmethod
568 def __setitem__(self, index, value):
569 raise IndexError
570
571 @abstractmethod
572 def __delitem__(self, index):
573 raise IndexError
574
575 @abstractmethod
576 def insert(self, index, value):
577 raise IndexError
578
579 def append(self, value):
580 self.insert(len(self), value)
581
582 def reverse(self):
583 n = len(self)
584 for i in range(n//2):
585 self[i], self[n-i-1] = self[n-i-1], self[i]
586
587 def extend(self, values):
588 for v in values:
589 self.append(v)
590
591 def pop(self, index=-1):
592 v = self[index]
593 del self[index]
594 return v
595
596 def remove(self, value):
597 del self[self.index(value)]
598
599 def __iadd__(self, values):
600 self.extend(values)
Raymond Hettingerc384b222009-05-18 15:35:26 +0000601 return self
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000602
603MutableSequence.register(list)
Guido van Rossumd05eb002007-11-21 22:26:24 +0000604MutableSequence.register(bytearray) # Multiply inheriting, see ByteString