Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1 | # 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 | |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 6 | Unit tests are in test_collections. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | from abc import ABCMeta, abstractmethod |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 10 | import sys |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11 | |
| 12 | __all__ = ["Hashable", "Iterable", "Iterator", |
| 13 | "Sized", "Container", "Callable", |
| 14 | "Set", "MutableSet", |
| 15 | "Mapping", "MutableMapping", |
| 16 | "MappingView", "KeysView", "ItemsView", "ValuesView", |
| 17 | "Sequence", "MutableSequence", |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 18 | "ByteString", |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 19 | ] |
| 20 | |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 21 | |
| 22 | ### collection related types which are not exposed through builtin ### |
| 23 | ## iterators ## |
| 24 | bytes_iterator = type(iter(b'')) |
| 25 | bytearray_iterator = type(iter(bytearray())) |
| 26 | #callable_iterator = ??? |
| 27 | dict_keyiterator = type(iter({}.keys())) |
| 28 | dict_valueiterator = type(iter({}.values())) |
| 29 | dict_itemiterator = type(iter({}.items())) |
| 30 | list_iterator = type(iter([])) |
| 31 | list_reverseiterator = type(iter(reversed([]))) |
| 32 | range_iterator = type(iter(range(0))) |
| 33 | set_iterator = type(iter(set())) |
| 34 | str_iterator = type(iter("")) |
| 35 | tuple_iterator = type(iter(())) |
| 36 | zip_iterator = type(iter(zip())) |
| 37 | ## views ## |
| 38 | dict_keys = type({}.keys()) |
| 39 | dict_values = type({}.values()) |
| 40 | dict_items = type({}.items()) |
Christian Heimes | 0db3853 | 2007-11-29 16:21:13 +0000 | [diff] [blame] | 41 | ## misc ## |
| 42 | dict_proxy = type(type.__dict__) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 45 | ### ONE-TRICK PONIES ### |
| 46 | |
| 47 | class Hashable(metaclass=ABCMeta): |
| 48 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 49 | __slots__ = () |
| 50 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 51 | @abstractmethod |
| 52 | def __hash__(self): |
| 53 | return 0 |
| 54 | |
| 55 | @classmethod |
| 56 | def __subclasshook__(cls, C): |
| 57 | if cls is Hashable: |
| 58 | for B in C.__mro__: |
| 59 | if "__hash__" in B.__dict__: |
| 60 | if B.__dict__["__hash__"]: |
| 61 | return True |
| 62 | break |
| 63 | return NotImplemented |
| 64 | |
| 65 | |
| 66 | class Iterable(metaclass=ABCMeta): |
| 67 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 68 | __slots__ = () |
| 69 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 70 | @abstractmethod |
| 71 | def __iter__(self): |
| 72 | while False: |
| 73 | yield None |
| 74 | |
| 75 | @classmethod |
| 76 | def __subclasshook__(cls, C): |
| 77 | if cls is Iterable: |
| 78 | if any("__iter__" in B.__dict__ for B in C.__mro__): |
| 79 | return True |
| 80 | return NotImplemented |
| 81 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 82 | |
Raymond Hettinger | 74b6495 | 2008-02-09 02:53:48 +0000 | [diff] [blame] | 83 | class Iterator(Iterable): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 84 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 85 | __slots__ = () |
| 86 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 87 | @abstractmethod |
| 88 | def __next__(self): |
| 89 | raise StopIteration |
| 90 | |
| 91 | def __iter__(self): |
| 92 | return self |
| 93 | |
| 94 | @classmethod |
| 95 | def __subclasshook__(cls, C): |
| 96 | if cls is Iterator: |
Raymond Hettinger | ead2222 | 2010-11-29 03:56:12 +0000 | [diff] [blame] | 97 | if (any("__next__" in B.__dict__ for B in C.__mro__) and |
| 98 | any("__iter__" in B.__dict__ for B in C.__mro__)): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 99 | return True |
| 100 | return NotImplemented |
| 101 | |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 102 | Iterator.register(bytes_iterator) |
| 103 | Iterator.register(bytearray_iterator) |
| 104 | #Iterator.register(callable_iterator) |
| 105 | Iterator.register(dict_keyiterator) |
| 106 | Iterator.register(dict_valueiterator) |
| 107 | Iterator.register(dict_itemiterator) |
| 108 | Iterator.register(list_iterator) |
| 109 | Iterator.register(list_reverseiterator) |
| 110 | Iterator.register(range_iterator) |
| 111 | Iterator.register(set_iterator) |
| 112 | Iterator.register(str_iterator) |
| 113 | Iterator.register(tuple_iterator) |
| 114 | Iterator.register(zip_iterator) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 115 | |
| 116 | class Sized(metaclass=ABCMeta): |
| 117 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 118 | __slots__ = () |
| 119 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 120 | @abstractmethod |
| 121 | def __len__(self): |
| 122 | return 0 |
| 123 | |
| 124 | @classmethod |
| 125 | def __subclasshook__(cls, C): |
| 126 | if cls is Sized: |
| 127 | if any("__len__" in B.__dict__ for B in C.__mro__): |
| 128 | return True |
| 129 | return NotImplemented |
| 130 | |
| 131 | |
| 132 | class Container(metaclass=ABCMeta): |
| 133 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 134 | __slots__ = () |
| 135 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 136 | @abstractmethod |
| 137 | def __contains__(self, x): |
| 138 | return False |
| 139 | |
| 140 | @classmethod |
| 141 | def __subclasshook__(cls, C): |
| 142 | if cls is Container: |
| 143 | if any("__contains__" in B.__dict__ for B in C.__mro__): |
| 144 | return True |
| 145 | return NotImplemented |
| 146 | |
| 147 | |
| 148 | class Callable(metaclass=ABCMeta): |
| 149 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 150 | __slots__ = () |
| 151 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 152 | @abstractmethod |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 153 | def __call__(self, *args, **kwds): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 154 | return False |
| 155 | |
| 156 | @classmethod |
| 157 | def __subclasshook__(cls, C): |
| 158 | if cls is Callable: |
| 159 | if any("__call__" in B.__dict__ for B in C.__mro__): |
| 160 | return True |
| 161 | return NotImplemented |
| 162 | |
| 163 | |
| 164 | ### SETS ### |
| 165 | |
| 166 | |
Raymond Hettinger | 74b6495 | 2008-02-09 02:53:48 +0000 | [diff] [blame] | 167 | class Set(Sized, Iterable, Container): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 168 | |
| 169 | """A set is a finite, iterable container. |
| 170 | |
| 171 | This class provides concrete generic implementations of all |
| 172 | methods except for __contains__, __iter__ and __len__. |
| 173 | |
| 174 | To override the comparisons (presumably for speed, as the |
| 175 | semantics are fixed), all you have to do is redefine __le__ and |
| 176 | then the other operations will automatically follow suit. |
| 177 | """ |
| 178 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 179 | __slots__ = () |
| 180 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 181 | def __le__(self, other): |
| 182 | if not isinstance(other, Set): |
| 183 | return NotImplemented |
| 184 | if len(self) > len(other): |
| 185 | return False |
| 186 | for elem in self: |
| 187 | if elem not in other: |
| 188 | return False |
| 189 | return True |
| 190 | |
| 191 | def __lt__(self, other): |
| 192 | if not isinstance(other, Set): |
| 193 | return NotImplemented |
| 194 | return len(self) < len(other) and self.__le__(other) |
| 195 | |
Raymond Hettinger | 7190942 | 2008-02-09 00:08:16 +0000 | [diff] [blame] | 196 | def __gt__(self, other): |
| 197 | if not isinstance(other, Set): |
| 198 | return NotImplemented |
| 199 | return other < self |
| 200 | |
| 201 | def __ge__(self, other): |
| 202 | if not isinstance(other, Set): |
| 203 | return NotImplemented |
| 204 | return other <= self |
| 205 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 206 | def __eq__(self, other): |
| 207 | if not isinstance(other, Set): |
| 208 | return NotImplemented |
| 209 | return len(self) == len(other) and self.__le__(other) |
| 210 | |
Raymond Hettinger | 7190942 | 2008-02-09 00:08:16 +0000 | [diff] [blame] | 211 | def __ne__(self, other): |
| 212 | return not (self == other) |
| 213 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 214 | @classmethod |
| 215 | def _from_iterable(cls, it): |
Raymond Hettinger | 8284c4a | 2008-02-06 20:47:09 +0000 | [diff] [blame] | 216 | '''Construct an instance of the class from any iterable input. |
| 217 | |
| 218 | Must override this method if the class constructor signature |
Raymond Hettinger | 7aebb64 | 2008-02-09 03:25:08 +0000 | [diff] [blame] | 219 | does not accept an iterable for an input. |
Raymond Hettinger | 8284c4a | 2008-02-06 20:47:09 +0000 | [diff] [blame] | 220 | ''' |
Raymond Hettinger | 7aebb64 | 2008-02-09 03:25:08 +0000 | [diff] [blame] | 221 | return cls(it) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 222 | |
| 223 | def __and__(self, other): |
| 224 | if not isinstance(other, Iterable): |
| 225 | return NotImplemented |
| 226 | return self._from_iterable(value for value in other if value in self) |
| 227 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 228 | def isdisjoint(self, other): |
| 229 | for value in other: |
| 230 | if value in self: |
| 231 | return False |
| 232 | return True |
| 233 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 234 | def __or__(self, other): |
| 235 | if not isinstance(other, Iterable): |
| 236 | return NotImplemented |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 237 | chain = (e for s in (self, other) for e in s) |
| 238 | return self._from_iterable(chain) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 239 | |
| 240 | def __sub__(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._from_iterable(value for value in self |
| 246 | if value not in other) |
| 247 | |
| 248 | def __xor__(self, other): |
| 249 | if not isinstance(other, Set): |
| 250 | if not isinstance(other, Iterable): |
| 251 | return NotImplemented |
| 252 | other = self._from_iterable(other) |
| 253 | return (self - other) | (other - self) |
| 254 | |
| 255 | def _hash(self): |
| 256 | """Compute the hash value of a set. |
| 257 | |
| 258 | Note that we don't define __hash__: not all sets are hashable. |
| 259 | But if you define a hashable set type, its __hash__ should |
| 260 | call this function. |
| 261 | |
| 262 | This must be compatible __eq__. |
| 263 | |
| 264 | All sets ought to compare equal if they contain the same |
| 265 | elements, regardless of how they are implemented, and |
| 266 | regardless of the order of the elements; so there's not much |
| 267 | freedom for __eq__ or __hash__. We match the algorithm used |
| 268 | by the built-in frozenset type. |
| 269 | """ |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 270 | MAX = sys.maxsize |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 271 | MASK = 2 * MAX + 1 |
| 272 | n = len(self) |
| 273 | h = 1927868237 * (n + 1) |
| 274 | h &= MASK |
| 275 | for x in self: |
| 276 | hx = hash(x) |
| 277 | h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167 |
| 278 | h &= MASK |
| 279 | h = h * 69069 + 907133923 |
| 280 | h &= MASK |
| 281 | if h > MAX: |
| 282 | h -= MASK + 1 |
| 283 | if h == -1: |
| 284 | h = 590923713 |
| 285 | return h |
| 286 | |
| 287 | Set.register(frozenset) |
| 288 | |
| 289 | |
| 290 | class MutableSet(Set): |
| 291 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 292 | __slots__ = () |
| 293 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 294 | @abstractmethod |
| 295 | def add(self, value): |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 296 | """Add an element.""" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 297 | raise NotImplementedError |
| 298 | |
| 299 | @abstractmethod |
| 300 | def discard(self, value): |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 301 | """Remove an element. Do not raise an exception if absent.""" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 302 | raise NotImplementedError |
| 303 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 304 | def remove(self, value): |
| 305 | """Remove an element. If not a member, raise a KeyError.""" |
| 306 | if value not in self: |
| 307 | raise KeyError(value) |
| 308 | self.discard(value) |
| 309 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 310 | def pop(self): |
| 311 | """Return the popped value. Raise KeyError if empty.""" |
| 312 | it = iter(self) |
| 313 | try: |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 314 | value = next(it) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 315 | except StopIteration: |
| 316 | raise KeyError |
| 317 | self.discard(value) |
| 318 | return value |
| 319 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 320 | def clear(self): |
| 321 | """This is slow (creates N new iterators!) but effective.""" |
| 322 | try: |
| 323 | while True: |
| 324 | self.pop() |
| 325 | except KeyError: |
| 326 | pass |
| 327 | |
Raymond Hettinger | b3d89a4 | 2011-01-12 20:37:47 +0000 | [diff] [blame] | 328 | def __ior__(self, it): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 329 | for value in it: |
| 330 | self.add(value) |
| 331 | return self |
| 332 | |
Raymond Hettinger | b3d89a4 | 2011-01-12 20:37:47 +0000 | [diff] [blame] | 333 | def __iand__(self, it): |
Raymond Hettinger | 3f10a95 | 2009-04-01 19:05:50 +0000 | [diff] [blame] | 334 | for value in (self - it): |
| 335 | self.discard(value) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 336 | return self |
| 337 | |
Raymond Hettinger | b3d89a4 | 2011-01-12 20:37:47 +0000 | [diff] [blame] | 338 | def __ixor__(self, it): |
Daniel Stutzbach | 31da5b2 | 2010-08-24 20:49:57 +0000 | [diff] [blame] | 339 | if it is self: |
| 340 | self.clear() |
| 341 | else: |
| 342 | if not isinstance(it, Set): |
| 343 | it = self._from_iterable(it) |
| 344 | for value in it: |
| 345 | if value in self: |
| 346 | self.discard(value) |
| 347 | else: |
| 348 | self.add(value) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 349 | return self |
| 350 | |
Raymond Hettinger | b3d89a4 | 2011-01-12 20:37:47 +0000 | [diff] [blame] | 351 | def __isub__(self, it): |
Daniel Stutzbach | 31da5b2 | 2010-08-24 20:49:57 +0000 | [diff] [blame] | 352 | if it is self: |
| 353 | self.clear() |
| 354 | else: |
| 355 | for value in it: |
| 356 | self.discard(value) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 357 | return self |
| 358 | |
| 359 | MutableSet.register(set) |
| 360 | |
| 361 | |
| 362 | ### MAPPINGS ### |
| 363 | |
| 364 | |
Raymond Hettinger | 74b6495 | 2008-02-09 02:53:48 +0000 | [diff] [blame] | 365 | class Mapping(Sized, Iterable, Container): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 366 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 367 | __slots__ = () |
| 368 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 369 | @abstractmethod |
| 370 | def __getitem__(self, key): |
| 371 | raise KeyError |
| 372 | |
| 373 | def get(self, key, default=None): |
| 374 | try: |
| 375 | return self[key] |
| 376 | except KeyError: |
| 377 | return default |
| 378 | |
| 379 | def __contains__(self, key): |
| 380 | try: |
| 381 | self[key] |
| 382 | except KeyError: |
| 383 | return False |
| 384 | else: |
| 385 | return True |
| 386 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 387 | def keys(self): |
| 388 | return KeysView(self) |
| 389 | |
| 390 | def items(self): |
| 391 | return ItemsView(self) |
| 392 | |
| 393 | def values(self): |
| 394 | return ValuesView(self) |
| 395 | |
Raymond Hettinger | b9da9bc | 2008-02-04 20:44:31 +0000 | [diff] [blame] | 396 | def __eq__(self, other): |
Benjamin Peterson | 4ad6bd5 | 2010-05-21 20:55:22 +0000 | [diff] [blame] | 397 | if not isinstance(other, Mapping): |
| 398 | return NotImplemented |
| 399 | return dict(self.items()) == dict(other.items()) |
Raymond Hettinger | b9da9bc | 2008-02-04 20:44:31 +0000 | [diff] [blame] | 400 | |
| 401 | def __ne__(self, other): |
Raymond Hettinger | 554c8b8 | 2008-02-05 22:54:43 +0000 | [diff] [blame] | 402 | return not (self == other) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 403 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 404 | |
Raymond Hettinger | bfd0612 | 2008-02-09 10:04:32 +0000 | [diff] [blame] | 405 | class MappingView(Sized): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 406 | |
| 407 | def __init__(self, mapping): |
| 408 | self._mapping = mapping |
| 409 | |
| 410 | def __len__(self): |
| 411 | return len(self._mapping) |
| 412 | |
Raymond Hettinger | 89fc2b7 | 2009-02-27 07:47:32 +0000 | [diff] [blame] | 413 | def __repr__(self): |
| 414 | return '{0.__class__.__name__}({0._mapping!r})'.format(self) |
| 415 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 416 | |
| 417 | class KeysView(MappingView, Set): |
| 418 | |
Raymond Hettinger | 9117c75 | 2010-08-22 07:44:24 +0000 | [diff] [blame] | 419 | @classmethod |
| 420 | def _from_iterable(self, it): |
| 421 | return set(it) |
| 422 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 423 | def __contains__(self, key): |
| 424 | return key in self._mapping |
| 425 | |
| 426 | def __iter__(self): |
| 427 | for key in self._mapping: |
| 428 | yield key |
| 429 | |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 430 | KeysView.register(dict_keys) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 431 | |
| 432 | |
| 433 | class ItemsView(MappingView, Set): |
| 434 | |
Raymond Hettinger | 9117c75 | 2010-08-22 07:44:24 +0000 | [diff] [blame] | 435 | @classmethod |
| 436 | def _from_iterable(self, it): |
| 437 | return set(it) |
| 438 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 439 | def __contains__(self, item): |
| 440 | key, value = item |
| 441 | try: |
| 442 | v = self._mapping[key] |
| 443 | except KeyError: |
| 444 | return False |
| 445 | else: |
| 446 | return v == value |
| 447 | |
| 448 | def __iter__(self): |
| 449 | for key in self._mapping: |
| 450 | yield (key, self._mapping[key]) |
| 451 | |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 452 | ItemsView.register(dict_items) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 453 | |
| 454 | |
| 455 | class ValuesView(MappingView): |
| 456 | |
| 457 | def __contains__(self, value): |
| 458 | for key in self._mapping: |
| 459 | if value == self._mapping[key]: |
| 460 | return True |
| 461 | return False |
| 462 | |
| 463 | def __iter__(self): |
| 464 | for key in self._mapping: |
| 465 | yield self._mapping[key] |
| 466 | |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 467 | ValuesView.register(dict_values) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 468 | |
| 469 | |
| 470 | class MutableMapping(Mapping): |
| 471 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 472 | __slots__ = () |
| 473 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 474 | @abstractmethod |
| 475 | def __setitem__(self, key, value): |
| 476 | raise KeyError |
| 477 | |
| 478 | @abstractmethod |
| 479 | def __delitem__(self, key): |
| 480 | raise KeyError |
| 481 | |
| 482 | __marker = object() |
| 483 | |
| 484 | def pop(self, key, default=__marker): |
| 485 | try: |
| 486 | value = self[key] |
| 487 | except KeyError: |
| 488 | if default is self.__marker: |
| 489 | raise |
| 490 | return default |
| 491 | else: |
| 492 | del self[key] |
| 493 | return value |
| 494 | |
| 495 | def popitem(self): |
| 496 | try: |
| 497 | key = next(iter(self)) |
| 498 | except StopIteration: |
| 499 | raise KeyError |
| 500 | value = self[key] |
| 501 | del self[key] |
| 502 | return key, value |
| 503 | |
| 504 | def clear(self): |
| 505 | try: |
| 506 | while True: |
| 507 | self.popitem() |
| 508 | except KeyError: |
| 509 | pass |
| 510 | |
Mark Dickinson | b214e90 | 2010-07-11 18:53:06 +0000 | [diff] [blame] | 511 | def update(*args, **kwds): |
| 512 | if len(args) > 2: |
| 513 | raise TypeError("update() takes at most 2 positional " |
| 514 | "arguments ({} given)".format(len(args))) |
| 515 | elif not args: |
| 516 | raise TypeError("update() takes at least 1 argument (0 given)") |
| 517 | self = args[0] |
| 518 | other = args[1] if len(args) >= 2 else () |
| 519 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 520 | if isinstance(other, Mapping): |
| 521 | for key in other: |
| 522 | self[key] = other[key] |
| 523 | elif hasattr(other, "keys"): |
| 524 | for key in other.keys(): |
| 525 | self[key] = other[key] |
| 526 | else: |
| 527 | for key, value in other: |
| 528 | self[key] = value |
| 529 | for key, value in kwds.items(): |
| 530 | self[key] = value |
| 531 | |
Raymond Hettinger | b9da9bc | 2008-02-04 20:44:31 +0000 | [diff] [blame] | 532 | def setdefault(self, key, default=None): |
| 533 | try: |
| 534 | return self[key] |
| 535 | except KeyError: |
| 536 | self[key] = default |
| 537 | return default |
| 538 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 539 | MutableMapping.register(dict) |
| 540 | |
| 541 | |
| 542 | ### SEQUENCES ### |
| 543 | |
| 544 | |
Raymond Hettinger | 74b6495 | 2008-02-09 02:53:48 +0000 | [diff] [blame] | 545 | class Sequence(Sized, Iterable, Container): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 546 | |
| 547 | """All the operations on a read-only sequence. |
| 548 | |
| 549 | Concrete subclasses must override __new__ or __init__, |
| 550 | __getitem__, and __len__. |
| 551 | """ |
| 552 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 553 | __slots__ = () |
| 554 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 555 | @abstractmethod |
| 556 | def __getitem__(self, index): |
| 557 | raise IndexError |
| 558 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 559 | def __iter__(self): |
| 560 | i = 0 |
Raymond Hettinger | 7190942 | 2008-02-09 00:08:16 +0000 | [diff] [blame] | 561 | try: |
| 562 | while True: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 563 | v = self[i] |
Raymond Hettinger | 7190942 | 2008-02-09 00:08:16 +0000 | [diff] [blame] | 564 | yield v |
| 565 | i += 1 |
| 566 | except IndexError: |
| 567 | return |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 568 | |
| 569 | def __contains__(self, value): |
| 570 | for v in self: |
| 571 | if v == value: |
| 572 | return True |
| 573 | return False |
| 574 | |
| 575 | def __reversed__(self): |
| 576 | for i in reversed(range(len(self))): |
| 577 | yield self[i] |
| 578 | |
| 579 | def index(self, value): |
| 580 | for i, v in enumerate(self): |
| 581 | if v == value: |
| 582 | return i |
| 583 | raise ValueError |
| 584 | |
| 585 | def count(self, value): |
| 586 | return sum(1 for v in self if v == value) |
| 587 | |
| 588 | Sequence.register(tuple) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 589 | Sequence.register(str) |
Raymond Hettinger | 9aa53c2 | 2009-02-24 11:25:35 +0000 | [diff] [blame] | 590 | Sequence.register(range) |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 591 | |
| 592 | |
| 593 | class ByteString(Sequence): |
| 594 | |
| 595 | """This unifies bytes and bytearray. |
| 596 | |
| 597 | XXX Should add all their methods. |
| 598 | """ |
| 599 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 600 | __slots__ = () |
| 601 | |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 602 | ByteString.register(bytes) |
| 603 | ByteString.register(bytearray) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 604 | |
| 605 | |
| 606 | class MutableSequence(Sequence): |
| 607 | |
Raymond Hettinger | c46759a | 2011-03-22 11:46:25 -0700 | [diff] [blame] | 608 | __slots__ = () |
| 609 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 610 | @abstractmethod |
| 611 | def __setitem__(self, index, value): |
| 612 | raise IndexError |
| 613 | |
| 614 | @abstractmethod |
| 615 | def __delitem__(self, index): |
| 616 | raise IndexError |
| 617 | |
| 618 | @abstractmethod |
| 619 | def insert(self, index, value): |
| 620 | raise IndexError |
| 621 | |
| 622 | def append(self, value): |
| 623 | self.insert(len(self), value) |
| 624 | |
Eli Bendersky | 9479d1a | 2011-03-04 05:34:58 +0000 | [diff] [blame] | 625 | def clear(self): |
| 626 | try: |
| 627 | while True: |
| 628 | self.pop() |
| 629 | except IndexError: |
| 630 | pass |
| 631 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 632 | def reverse(self): |
| 633 | n = len(self) |
| 634 | for i in range(n//2): |
| 635 | self[i], self[n-i-1] = self[n-i-1], self[i] |
| 636 | |
| 637 | def extend(self, values): |
| 638 | for v in values: |
| 639 | self.append(v) |
| 640 | |
| 641 | def pop(self, index=-1): |
| 642 | v = self[index] |
| 643 | del self[index] |
| 644 | return v |
| 645 | |
| 646 | def remove(self, value): |
| 647 | del self[self.index(value)] |
| 648 | |
| 649 | def __iadd__(self, values): |
| 650 | self.extend(values) |
Raymond Hettinger | c384b22 | 2009-05-18 15:35:26 +0000 | [diff] [blame] | 651 | return self |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 652 | |
| 653 | MutableSequence.register(list) |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 654 | MutableSequence.register(bytearray) # Multiply inheriting, see ByteString |