_collections_abc.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. # Copyright 2007 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Abstract Base Classes (ABCs) for collections, according to PEP 3119.
  4. Unit tests are in test_collections.
  5. """
  6. from abc import ABCMeta, abstractmethod
  7. import sys
  8. GenericAlias = type(list[int])
  9. EllipsisType = type(...)
  10. def _f(): pass
  11. FunctionType = type(_f)
  12. del _f
  13. __all__ = ["Awaitable", "Coroutine",
  14. "AsyncIterable", "AsyncIterator", "AsyncGenerator",
  15. "Hashable", "Iterable", "Iterator", "Generator", "Reversible",
  16. "Sized", "Container", "Callable", "Collection",
  17. "Set", "MutableSet",
  18. "Mapping", "MutableMapping",
  19. "MappingView", "KeysView", "ItemsView", "ValuesView",
  20. "Sequence", "MutableSequence",
  21. "ByteString",
  22. ]
  23. # This module has been renamed from collections.abc to _collections_abc to
  24. # speed up interpreter startup. Some of the types such as MutableMapping are
  25. # required early but collections module imports a lot of other modules.
  26. # See issue #19218
  27. __name__ = "collections.abc"
  28. # Private list of types that we want to register with the various ABCs
  29. # so that they will pass tests like:
  30. # it = iter(somebytearray)
  31. # assert isinstance(it, Iterable)
  32. # Note: in other implementations, these types might not be distinct
  33. # and they may have their own implementation specific types that
  34. # are not included on this list.
  35. bytes_iterator = type(iter(b''))
  36. bytearray_iterator = type(iter(bytearray()))
  37. #callable_iterator = ???
  38. dict_keyiterator = type(iter({}.keys()))
  39. dict_valueiterator = type(iter({}.values()))
  40. dict_itemiterator = type(iter({}.items()))
  41. list_iterator = type(iter([]))
  42. list_reverseiterator = type(iter(reversed([])))
  43. range_iterator = type(iter(range(0)))
  44. longrange_iterator = type(iter(range(1 << 1000)))
  45. set_iterator = type(iter(set()))
  46. str_iterator = type(iter(""))
  47. tuple_iterator = type(iter(()))
  48. zip_iterator = type(iter(zip()))
  49. ## views ##
  50. dict_keys = type({}.keys())
  51. dict_values = type({}.values())
  52. dict_items = type({}.items())
  53. ## misc ##
  54. mappingproxy = type(type.__dict__)
  55. generator = type((lambda: (yield))())
  56. ## coroutine ##
  57. async def _coro(): pass
  58. _coro = _coro()
  59. coroutine = type(_coro)
  60. _coro.close() # Prevent ResourceWarning
  61. del _coro
  62. ## asynchronous generator ##
  63. async def _ag(): yield
  64. _ag = _ag()
  65. async_generator = type(_ag)
  66. del _ag
  67. ### ONE-TRICK PONIES ###
  68. def _check_methods(C, *methods):
  69. mro = C.__mro__
  70. for method in methods:
  71. for B in mro:
  72. if method in B.__dict__:
  73. if B.__dict__[method] is None:
  74. return NotImplemented
  75. break
  76. else:
  77. return NotImplemented
  78. return True
  79. class Hashable(metaclass=ABCMeta):
  80. __slots__ = ()
  81. @abstractmethod
  82. def __hash__(self):
  83. return 0
  84. @classmethod
  85. def __subclasshook__(cls, C):
  86. if cls is Hashable:
  87. return _check_methods(C, "__hash__")
  88. return NotImplemented
  89. class Awaitable(metaclass=ABCMeta):
  90. __slots__ = ()
  91. @abstractmethod
  92. def __await__(self):
  93. yield
  94. @classmethod
  95. def __subclasshook__(cls, C):
  96. if cls is Awaitable:
  97. return _check_methods(C, "__await__")
  98. return NotImplemented
  99. __class_getitem__ = classmethod(GenericAlias)
  100. class Coroutine(Awaitable):
  101. __slots__ = ()
  102. @abstractmethod
  103. def send(self, value):
  104. """Send a value into the coroutine.
  105. Return next yielded value or raise StopIteration.
  106. """
  107. raise StopIteration
  108. @abstractmethod
  109. def throw(self, typ, val=None, tb=None):
  110. """Raise an exception in the coroutine.
  111. Return next yielded value or raise StopIteration.
  112. """
  113. if val is None:
  114. if tb is None:
  115. raise typ
  116. val = typ()
  117. if tb is not None:
  118. val = val.with_traceback(tb)
  119. raise val
  120. def close(self):
  121. """Raise GeneratorExit inside coroutine.
  122. """
  123. try:
  124. self.throw(GeneratorExit)
  125. except (GeneratorExit, StopIteration):
  126. pass
  127. else:
  128. raise RuntimeError("coroutine ignored GeneratorExit")
  129. @classmethod
  130. def __subclasshook__(cls, C):
  131. if cls is Coroutine:
  132. return _check_methods(C, '__await__', 'send', 'throw', 'close')
  133. return NotImplemented
  134. Coroutine.register(coroutine)
  135. class AsyncIterable(metaclass=ABCMeta):
  136. __slots__ = ()
  137. @abstractmethod
  138. def __aiter__(self):
  139. return AsyncIterator()
  140. @classmethod
  141. def __subclasshook__(cls, C):
  142. if cls is AsyncIterable:
  143. return _check_methods(C, "__aiter__")
  144. return NotImplemented
  145. __class_getitem__ = classmethod(GenericAlias)
  146. class AsyncIterator(AsyncIterable):
  147. __slots__ = ()
  148. @abstractmethod
  149. async def __anext__(self):
  150. """Return the next item or raise StopAsyncIteration when exhausted."""
  151. raise StopAsyncIteration
  152. def __aiter__(self):
  153. return self
  154. @classmethod
  155. def __subclasshook__(cls, C):
  156. if cls is AsyncIterator:
  157. return _check_methods(C, "__anext__", "__aiter__")
  158. return NotImplemented
  159. class AsyncGenerator(AsyncIterator):
  160. __slots__ = ()
  161. async def __anext__(self):
  162. """Return the next item from the asynchronous generator.
  163. When exhausted, raise StopAsyncIteration.
  164. """
  165. return await self.asend(None)
  166. @abstractmethod
  167. async def asend(self, value):
  168. """Send a value into the asynchronous generator.
  169. Return next yielded value or raise StopAsyncIteration.
  170. """
  171. raise StopAsyncIteration
  172. @abstractmethod
  173. async def athrow(self, typ, val=None, tb=None):
  174. """Raise an exception in the asynchronous generator.
  175. Return next yielded value or raise StopAsyncIteration.
  176. """
  177. if val is None:
  178. if tb is None:
  179. raise typ
  180. val = typ()
  181. if tb is not None:
  182. val = val.with_traceback(tb)
  183. raise val
  184. async def aclose(self):
  185. """Raise GeneratorExit inside coroutine.
  186. """
  187. try:
  188. await self.athrow(GeneratorExit)
  189. except (GeneratorExit, StopAsyncIteration):
  190. pass
  191. else:
  192. raise RuntimeError("asynchronous generator ignored GeneratorExit")
  193. @classmethod
  194. def __subclasshook__(cls, C):
  195. if cls is AsyncGenerator:
  196. return _check_methods(C, '__aiter__', '__anext__',
  197. 'asend', 'athrow', 'aclose')
  198. return NotImplemented
  199. AsyncGenerator.register(async_generator)
  200. class Iterable(metaclass=ABCMeta):
  201. __slots__ = ()
  202. @abstractmethod
  203. def __iter__(self):
  204. while False:
  205. yield None
  206. @classmethod
  207. def __subclasshook__(cls, C):
  208. if cls is Iterable:
  209. return _check_methods(C, "__iter__")
  210. return NotImplemented
  211. __class_getitem__ = classmethod(GenericAlias)
  212. class Iterator(Iterable):
  213. __slots__ = ()
  214. @abstractmethod
  215. def __next__(self):
  216. 'Return the next item from the iterator. When exhausted, raise StopIteration'
  217. raise StopIteration
  218. def __iter__(self):
  219. return self
  220. @classmethod
  221. def __subclasshook__(cls, C):
  222. if cls is Iterator:
  223. return _check_methods(C, '__iter__', '__next__')
  224. return NotImplemented
  225. Iterator.register(bytes_iterator)
  226. Iterator.register(bytearray_iterator)
  227. #Iterator.register(callable_iterator)
  228. Iterator.register(dict_keyiterator)
  229. Iterator.register(dict_valueiterator)
  230. Iterator.register(dict_itemiterator)
  231. Iterator.register(list_iterator)
  232. Iterator.register(list_reverseiterator)
  233. Iterator.register(range_iterator)
  234. Iterator.register(longrange_iterator)
  235. Iterator.register(set_iterator)
  236. Iterator.register(str_iterator)
  237. Iterator.register(tuple_iterator)
  238. Iterator.register(zip_iterator)
  239. class Reversible(Iterable):
  240. __slots__ = ()
  241. @abstractmethod
  242. def __reversed__(self):
  243. while False:
  244. yield None
  245. @classmethod
  246. def __subclasshook__(cls, C):
  247. if cls is Reversible:
  248. return _check_methods(C, "__reversed__", "__iter__")
  249. return NotImplemented
  250. class Generator(Iterator):
  251. __slots__ = ()
  252. def __next__(self):
  253. """Return the next item from the generator.
  254. When exhausted, raise StopIteration.
  255. """
  256. return self.send(None)
  257. @abstractmethod
  258. def send(self, value):
  259. """Send a value into the generator.
  260. Return next yielded value or raise StopIteration.
  261. """
  262. raise StopIteration
  263. @abstractmethod
  264. def throw(self, typ, val=None, tb=None):
  265. """Raise an exception in the generator.
  266. Return next yielded value or raise StopIteration.
  267. """
  268. if val is None:
  269. if tb is None:
  270. raise typ
  271. val = typ()
  272. if tb is not None:
  273. val = val.with_traceback(tb)
  274. raise val
  275. def close(self):
  276. """Raise GeneratorExit inside generator.
  277. """
  278. try:
  279. self.throw(GeneratorExit)
  280. except (GeneratorExit, StopIteration):
  281. pass
  282. else:
  283. raise RuntimeError("generator ignored GeneratorExit")
  284. @classmethod
  285. def __subclasshook__(cls, C):
  286. if cls is Generator:
  287. return _check_methods(C, '__iter__', '__next__',
  288. 'send', 'throw', 'close')
  289. return NotImplemented
  290. Generator.register(generator)
  291. class Sized(metaclass=ABCMeta):
  292. __slots__ = ()
  293. @abstractmethod
  294. def __len__(self):
  295. return 0
  296. @classmethod
  297. def __subclasshook__(cls, C):
  298. if cls is Sized:
  299. return _check_methods(C, "__len__")
  300. return NotImplemented
  301. class Container(metaclass=ABCMeta):
  302. __slots__ = ()
  303. @abstractmethod
  304. def __contains__(self, x):
  305. return False
  306. @classmethod
  307. def __subclasshook__(cls, C):
  308. if cls is Container:
  309. return _check_methods(C, "__contains__")
  310. return NotImplemented
  311. __class_getitem__ = classmethod(GenericAlias)
  312. class Collection(Sized, Iterable, Container):
  313. __slots__ = ()
  314. @classmethod
  315. def __subclasshook__(cls, C):
  316. if cls is Collection:
  317. return _check_methods(C, "__len__", "__iter__", "__contains__")
  318. return NotImplemented
  319. class _CallableGenericAlias(GenericAlias):
  320. """ Represent `Callable[argtypes, resulttype]`.
  321. This sets ``__args__`` to a tuple containing the flattened``argtypes``
  322. followed by ``resulttype``.
  323. Example: ``Callable[[int, str], float]`` sets ``__args__`` to
  324. ``(int, str, float)``.
  325. """
  326. __slots__ = ()
  327. def __new__(cls, origin, args):
  328. try:
  329. return cls.__create_ga(origin, args)
  330. except TypeError as exc:
  331. import warnings
  332. warnings.warn(f'{str(exc)} '
  333. f'(This will raise a TypeError in Python 3.10.)',
  334. DeprecationWarning)
  335. return GenericAlias(origin, args)
  336. @classmethod
  337. def __create_ga(cls, origin, args):
  338. if not isinstance(args, tuple) or len(args) != 2:
  339. raise TypeError(
  340. "Callable must be used as Callable[[arg, ...], result].")
  341. t_args, t_result = args
  342. if isinstance(t_args, (list, tuple)):
  343. ga_args = tuple(t_args) + (t_result,)
  344. # This relaxes what t_args can be on purpose to allow things like
  345. # PEP 612 ParamSpec. Responsibility for whether a user is using
  346. # Callable[...] properly is deferred to static type checkers.
  347. else:
  348. ga_args = args
  349. return super().__new__(cls, origin, ga_args)
  350. def __repr__(self):
  351. if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
  352. return super().__repr__()
  353. return (f'collections.abc.Callable'
  354. f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
  355. f'{_type_repr(self.__args__[-1])}]')
  356. def __reduce__(self):
  357. args = self.__args__
  358. if not (len(args) == 2 and args[0] is Ellipsis):
  359. args = list(args[:-1]), args[-1]
  360. return _CallableGenericAlias, (Callable, args)
  361. def __getitem__(self, item):
  362. # Called during TypeVar substitution, returns the custom subclass
  363. # rather than the default types.GenericAlias object.
  364. ga = super().__getitem__(item)
  365. args = ga.__args__
  366. t_result = args[-1]
  367. t_args = args[:-1]
  368. args = (t_args, t_result)
  369. return _CallableGenericAlias(Callable, args)
  370. def _type_repr(obj):
  371. """Return the repr() of an object, special-casing types (internal helper).
  372. Copied from :mod:`typing` since collections.abc
  373. shouldn't depend on that module.
  374. """
  375. if isinstance(obj, GenericAlias):
  376. return repr(obj)
  377. if isinstance(obj, type):
  378. if obj.__module__ == 'builtins':
  379. return obj.__qualname__
  380. return f'{obj.__module__}.{obj.__qualname__}'
  381. if obj is Ellipsis:
  382. return '...'
  383. if isinstance(obj, FunctionType):
  384. return obj.__name__
  385. return repr(obj)
  386. class Callable(metaclass=ABCMeta):
  387. __slots__ = ()
  388. @abstractmethod
  389. def __call__(self, *args, **kwds):
  390. return False
  391. @classmethod
  392. def __subclasshook__(cls, C):
  393. if cls is Callable:
  394. return _check_methods(C, "__call__")
  395. return NotImplemented
  396. __class_getitem__ = classmethod(_CallableGenericAlias)
  397. ### SETS ###
  398. class Set(Collection):
  399. """A set is a finite, iterable container.
  400. This class provides concrete generic implementations of all
  401. methods except for __contains__, __iter__ and __len__.
  402. To override the comparisons (presumably for speed, as the
  403. semantics are fixed), redefine __le__ and __ge__,
  404. then the other operations will automatically follow suit.
  405. """
  406. __slots__ = ()
  407. def __le__(self, other):
  408. if not isinstance(other, Set):
  409. return NotImplemented
  410. if len(self) > len(other):
  411. return False
  412. for elem in self:
  413. if elem not in other:
  414. return False
  415. return True
  416. def __lt__(self, other):
  417. if not isinstance(other, Set):
  418. return NotImplemented
  419. return len(self) < len(other) and self.__le__(other)
  420. def __gt__(self, other):
  421. if not isinstance(other, Set):
  422. return NotImplemented
  423. return len(self) > len(other) and self.__ge__(other)
  424. def __ge__(self, other):
  425. if not isinstance(other, Set):
  426. return NotImplemented
  427. if len(self) < len(other):
  428. return False
  429. for elem in other:
  430. if elem not in self:
  431. return False
  432. return True
  433. def __eq__(self, other):
  434. if not isinstance(other, Set):
  435. return NotImplemented
  436. return len(self) == len(other) and self.__le__(other)
  437. @classmethod
  438. def _from_iterable(cls, it):
  439. '''Construct an instance of the class from any iterable input.
  440. Must override this method if the class constructor signature
  441. does not accept an iterable for an input.
  442. '''
  443. return cls(it)
  444. def __and__(self, other):
  445. if not isinstance(other, Iterable):
  446. return NotImplemented
  447. return self._from_iterable(value for value in other if value in self)
  448. __rand__ = __and__
  449. def isdisjoint(self, other):
  450. 'Return True if two sets have a null intersection.'
  451. for value in other:
  452. if value in self:
  453. return False
  454. return True
  455. def __or__(self, other):
  456. if not isinstance(other, Iterable):
  457. return NotImplemented
  458. chain = (e for s in (self, other) for e in s)
  459. return self._from_iterable(chain)
  460. __ror__ = __or__
  461. def __sub__(self, other):
  462. if not isinstance(other, Set):
  463. if not isinstance(other, Iterable):
  464. return NotImplemented
  465. other = self._from_iterable(other)
  466. return self._from_iterable(value for value in self
  467. if value not in other)
  468. def __rsub__(self, other):
  469. if not isinstance(other, Set):
  470. if not isinstance(other, Iterable):
  471. return NotImplemented
  472. other = self._from_iterable(other)
  473. return self._from_iterable(value for value in other
  474. if value not in self)
  475. def __xor__(self, other):
  476. if not isinstance(other, Set):
  477. if not isinstance(other, Iterable):
  478. return NotImplemented
  479. other = self._from_iterable(other)
  480. return (self - other) | (other - self)
  481. __rxor__ = __xor__
  482. def _hash(self):
  483. """Compute the hash value of a set.
  484. Note that we don't define __hash__: not all sets are hashable.
  485. But if you define a hashable set type, its __hash__ should
  486. call this function.
  487. This must be compatible __eq__.
  488. All sets ought to compare equal if they contain the same
  489. elements, regardless of how they are implemented, and
  490. regardless of the order of the elements; so there's not much
  491. freedom for __eq__ or __hash__. We match the algorithm used
  492. by the built-in frozenset type.
  493. """
  494. MAX = sys.maxsize
  495. MASK = 2 * MAX + 1
  496. n = len(self)
  497. h = 1927868237 * (n + 1)
  498. h &= MASK
  499. for x in self:
  500. hx = hash(x)
  501. h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
  502. h &= MASK
  503. h ^= (h >> 11) ^ (h >> 25)
  504. h = h * 69069 + 907133923
  505. h &= MASK
  506. if h > MAX:
  507. h -= MASK + 1
  508. if h == -1:
  509. h = 590923713
  510. return h
  511. Set.register(frozenset)
  512. class MutableSet(Set):
  513. """A mutable set is a finite, iterable container.
  514. This class provides concrete generic implementations of all
  515. methods except for __contains__, __iter__, __len__,
  516. add(), and discard().
  517. To override the comparisons (presumably for speed, as the
  518. semantics are fixed), all you have to do is redefine __le__ and
  519. then the other operations will automatically follow suit.
  520. """
  521. __slots__ = ()
  522. @abstractmethod
  523. def add(self, value):
  524. """Add an element."""
  525. raise NotImplementedError
  526. @abstractmethod
  527. def discard(self, value):
  528. """Remove an element. Do not raise an exception if absent."""
  529. raise NotImplementedError
  530. def remove(self, value):
  531. """Remove an element. If not a member, raise a KeyError."""
  532. if value not in self:
  533. raise KeyError(value)
  534. self.discard(value)
  535. def pop(self):
  536. """Return the popped value. Raise KeyError if empty."""
  537. it = iter(self)
  538. try:
  539. value = next(it)
  540. except StopIteration:
  541. raise KeyError from None
  542. self.discard(value)
  543. return value
  544. def clear(self):
  545. """This is slow (creates N new iterators!) but effective."""
  546. try:
  547. while True:
  548. self.pop()
  549. except KeyError:
  550. pass
  551. def __ior__(self, it):
  552. for value in it:
  553. self.add(value)
  554. return self
  555. def __iand__(self, it):
  556. for value in (self - it):
  557. self.discard(value)
  558. return self
  559. def __ixor__(self, it):
  560. if it is self:
  561. self.clear()
  562. else:
  563. if not isinstance(it, Set):
  564. it = self._from_iterable(it)
  565. for value in it:
  566. if value in self:
  567. self.discard(value)
  568. else:
  569. self.add(value)
  570. return self
  571. def __isub__(self, it):
  572. if it is self:
  573. self.clear()
  574. else:
  575. for value in it:
  576. self.discard(value)
  577. return self
  578. MutableSet.register(set)
  579. ### MAPPINGS ###
  580. class Mapping(Collection):
  581. __slots__ = ()
  582. """A Mapping is a generic container for associating key/value
  583. pairs.
  584. This class provides concrete generic implementations of all
  585. methods except for __getitem__, __iter__, and __len__.
  586. """
  587. @abstractmethod
  588. def __getitem__(self, key):
  589. raise KeyError
  590. def get(self, key, default=None):
  591. 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
  592. try:
  593. return self[key]
  594. except KeyError:
  595. return default
  596. def __contains__(self, key):
  597. try:
  598. self[key]
  599. except KeyError:
  600. return False
  601. else:
  602. return True
  603. def keys(self):
  604. "D.keys() -> a set-like object providing a view on D's keys"
  605. return KeysView(self)
  606. def items(self):
  607. "D.items() -> a set-like object providing a view on D's items"
  608. return ItemsView(self)
  609. def values(self):
  610. "D.values() -> an object providing a view on D's values"
  611. return ValuesView(self)
  612. def __eq__(self, other):
  613. if not isinstance(other, Mapping):
  614. return NotImplemented
  615. return dict(self.items()) == dict(other.items())
  616. __reversed__ = None
  617. Mapping.register(mappingproxy)
  618. class MappingView(Sized):
  619. __slots__ = '_mapping',
  620. def __init__(self, mapping):
  621. self._mapping = mapping
  622. def __len__(self):
  623. return len(self._mapping)
  624. def __repr__(self):
  625. return '{0.__class__.__name__}({0._mapping!r})'.format(self)
  626. __class_getitem__ = classmethod(GenericAlias)
  627. class KeysView(MappingView, Set):
  628. __slots__ = ()
  629. @classmethod
  630. def _from_iterable(cls, it):
  631. return set(it)
  632. def __contains__(self, key):
  633. return key in self._mapping
  634. def __iter__(self):
  635. yield from self._mapping
  636. KeysView.register(dict_keys)
  637. class ItemsView(MappingView, Set):
  638. __slots__ = ()
  639. @classmethod
  640. def _from_iterable(cls, it):
  641. return set(it)
  642. def __contains__(self, item):
  643. key, value = item
  644. try:
  645. v = self._mapping[key]
  646. except KeyError:
  647. return False
  648. else:
  649. return v is value or v == value
  650. def __iter__(self):
  651. for key in self._mapping:
  652. yield (key, self._mapping[key])
  653. ItemsView.register(dict_items)
  654. class ValuesView(MappingView, Collection):
  655. __slots__ = ()
  656. def __contains__(self, value):
  657. for key in self._mapping:
  658. v = self._mapping[key]
  659. if v is value or v == value:
  660. return True
  661. return False
  662. def __iter__(self):
  663. for key in self._mapping:
  664. yield self._mapping[key]
  665. ValuesView.register(dict_values)
  666. class MutableMapping(Mapping):
  667. __slots__ = ()
  668. """A MutableMapping is a generic container for associating
  669. key/value pairs.
  670. This class provides concrete generic implementations of all
  671. methods except for __getitem__, __setitem__, __delitem__,
  672. __iter__, and __len__.
  673. """
  674. @abstractmethod
  675. def __setitem__(self, key, value):
  676. raise KeyError
  677. @abstractmethod
  678. def __delitem__(self, key):
  679. raise KeyError
  680. __marker = object()
  681. def pop(self, key, default=__marker):
  682. '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  683. If key is not found, d is returned if given, otherwise KeyError is raised.
  684. '''
  685. try:
  686. value = self[key]
  687. except KeyError:
  688. if default is self.__marker:
  689. raise
  690. return default
  691. else:
  692. del self[key]
  693. return value
  694. def popitem(self):
  695. '''D.popitem() -> (k, v), remove and return some (key, value) pair
  696. as a 2-tuple; but raise KeyError if D is empty.
  697. '''
  698. try:
  699. key = next(iter(self))
  700. except StopIteration:
  701. raise KeyError from None
  702. value = self[key]
  703. del self[key]
  704. return key, value
  705. def clear(self):
  706. 'D.clear() -> None. Remove all items from D.'
  707. try:
  708. while True:
  709. self.popitem()
  710. except KeyError:
  711. pass
  712. def update(self, other=(), /, **kwds):
  713. ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
  714. If E present and has a .keys() method, does: for k in E: D[k] = E[k]
  715. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
  716. In either case, this is followed by: for k, v in F.items(): D[k] = v
  717. '''
  718. if isinstance(other, Mapping):
  719. for key in other:
  720. self[key] = other[key]
  721. elif hasattr(other, "keys"):
  722. for key in other.keys():
  723. self[key] = other[key]
  724. else:
  725. for key, value in other:
  726. self[key] = value
  727. for key, value in kwds.items():
  728. self[key] = value
  729. def setdefault(self, key, default=None):
  730. 'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
  731. try:
  732. return self[key]
  733. except KeyError:
  734. self[key] = default
  735. return default
  736. MutableMapping.register(dict)
  737. ### SEQUENCES ###
  738. class Sequence(Reversible, Collection):
  739. """All the operations on a read-only sequence.
  740. Concrete subclasses must override __new__ or __init__,
  741. __getitem__, and __len__.
  742. """
  743. __slots__ = ()
  744. @abstractmethod
  745. def __getitem__(self, index):
  746. raise IndexError
  747. def __iter__(self):
  748. i = 0
  749. try:
  750. while True:
  751. v = self[i]
  752. yield v
  753. i += 1
  754. except IndexError:
  755. return
  756. def __contains__(self, value):
  757. for v in self:
  758. if v is value or v == value:
  759. return True
  760. return False
  761. def __reversed__(self):
  762. for i in reversed(range(len(self))):
  763. yield self[i]
  764. def index(self, value, start=0, stop=None):
  765. '''S.index(value, [start, [stop]]) -> integer -- return first index of value.
  766. Raises ValueError if the value is not present.
  767. Supporting start and stop arguments is optional, but
  768. recommended.
  769. '''
  770. if start is not None and start < 0:
  771. start = max(len(self) + start, 0)
  772. if stop is not None and stop < 0:
  773. stop += len(self)
  774. i = start
  775. while stop is None or i < stop:
  776. try:
  777. v = self[i]
  778. if v is value or v == value:
  779. return i
  780. except IndexError:
  781. break
  782. i += 1
  783. raise ValueError
  784. def count(self, value):
  785. 'S.count(value) -> integer -- return number of occurrences of value'
  786. return sum(1 for v in self if v is value or v == value)
  787. Sequence.register(tuple)
  788. Sequence.register(str)
  789. Sequence.register(range)
  790. Sequence.register(memoryview)
  791. class ByteString(Sequence):
  792. """This unifies bytes and bytearray.
  793. XXX Should add all their methods.
  794. """
  795. __slots__ = ()
  796. ByteString.register(bytes)
  797. ByteString.register(bytearray)
  798. class MutableSequence(Sequence):
  799. __slots__ = ()
  800. """All the operations on a read-write sequence.
  801. Concrete subclasses must provide __new__ or __init__,
  802. __getitem__, __setitem__, __delitem__, __len__, and insert().
  803. """
  804. @abstractmethod
  805. def __setitem__(self, index, value):
  806. raise IndexError
  807. @abstractmethod
  808. def __delitem__(self, index):
  809. raise IndexError
  810. @abstractmethod
  811. def insert(self, index, value):
  812. 'S.insert(index, value) -- insert value before index'
  813. raise IndexError
  814. def append(self, value):
  815. 'S.append(value) -- append value to the end of the sequence'
  816. self.insert(len(self), value)
  817. def clear(self):
  818. 'S.clear() -> None -- remove all items from S'
  819. try:
  820. while True:
  821. self.pop()
  822. except IndexError:
  823. pass
  824. def reverse(self):
  825. 'S.reverse() -- reverse *IN PLACE*'
  826. n = len(self)
  827. for i in range(n//2):
  828. self[i], self[n-i-1] = self[n-i-1], self[i]
  829. def extend(self, values):
  830. 'S.extend(iterable) -- extend sequence by appending elements from the iterable'
  831. if values is self:
  832. values = list(values)
  833. for v in values:
  834. self.append(v)
  835. def pop(self, index=-1):
  836. '''S.pop([index]) -> item -- remove and return item at index (default last).
  837. Raise IndexError if list is empty or index is out of range.
  838. '''
  839. v = self[index]
  840. del self[index]
  841. return v
  842. def remove(self, value):
  843. '''S.remove(value) -- remove first occurrence of value.
  844. Raise ValueError if the value is not present.
  845. '''
  846. del self[self.index(value)]
  847. def __iadd__(self, values):
  848. self.extend(values)
  849. return self
  850. MutableSequence.register(list)
  851. MutableSequence.register(bytearray) # Multiply inheriting, see ByteString