_abcoll.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. DON'T USE THIS MODULE DIRECTLY! The classes here should be imported
  5. via collections; they are defined here only to alleviate certain
  6. bootstrapping issues. Unit tests are in test_collections.
  7. """
  8. from abc import ABCMeta, abstractmethod
  9. import sys
  10. __all__ = ["Hashable", "Iterable", "Iterator",
  11. "Sized", "Container", "Callable",
  12. "Set", "MutableSet",
  13. "Mapping", "MutableMapping",
  14. "MappingView", "KeysView", "ItemsView", "ValuesView",
  15. "Sequence", "MutableSequence",
  16. ]
  17. ### ONE-TRICK PONIES ###
  18. def _hasattr(C, attr):
  19. try:
  20. return any(attr in B.__dict__ for B in C.__mro__)
  21. except AttributeError:
  22. # Old-style class
  23. return hasattr(C, attr)
  24. class Hashable:
  25. __metaclass__ = ABCMeta
  26. @abstractmethod
  27. def __hash__(self):
  28. return 0
  29. @classmethod
  30. def __subclasshook__(cls, C):
  31. if cls is Hashable:
  32. try:
  33. for B in C.__mro__:
  34. if "__hash__" in B.__dict__:
  35. if B.__dict__["__hash__"]:
  36. return True
  37. break
  38. except AttributeError:
  39. # Old-style class
  40. if getattr(C, "__hash__", None):
  41. return True
  42. return NotImplemented
  43. class Iterable:
  44. __metaclass__ = ABCMeta
  45. @abstractmethod
  46. def __iter__(self):
  47. while False:
  48. yield None
  49. @classmethod
  50. def __subclasshook__(cls, C):
  51. if cls is Iterable:
  52. if _hasattr(C, "__iter__"):
  53. return True
  54. return NotImplemented
  55. Iterable.register(str)
  56. class Iterator(Iterable):
  57. @abstractmethod
  58. def next(self):
  59. 'Return the next item from the iterator. When exhausted, raise StopIteration'
  60. raise StopIteration
  61. def __iter__(self):
  62. return self
  63. @classmethod
  64. def __subclasshook__(cls, C):
  65. if cls is Iterator:
  66. if _hasattr(C, "next") and _hasattr(C, "__iter__"):
  67. return True
  68. return NotImplemented
  69. class Sized:
  70. __metaclass__ = ABCMeta
  71. @abstractmethod
  72. def __len__(self):
  73. return 0
  74. @classmethod
  75. def __subclasshook__(cls, C):
  76. if cls is Sized:
  77. if _hasattr(C, "__len__"):
  78. return True
  79. return NotImplemented
  80. class Container:
  81. __metaclass__ = ABCMeta
  82. @abstractmethod
  83. def __contains__(self, x):
  84. return False
  85. @classmethod
  86. def __subclasshook__(cls, C):
  87. if cls is Container:
  88. if _hasattr(C, "__contains__"):
  89. return True
  90. return NotImplemented
  91. class Callable:
  92. __metaclass__ = ABCMeta
  93. @abstractmethod
  94. def __call__(self, *args, **kwds):
  95. return False
  96. @classmethod
  97. def __subclasshook__(cls, C):
  98. if cls is Callable:
  99. if _hasattr(C, "__call__"):
  100. return True
  101. return NotImplemented
  102. ### SETS ###
  103. class Set(Sized, Iterable, Container):
  104. """A set is a finite, iterable container.
  105. This class provides concrete generic implementations of all
  106. methods except for __contains__, __iter__ and __len__.
  107. To override the comparisons (presumably for speed, as the
  108. semantics are fixed), redefine __le__ and __ge__,
  109. then the other operations will automatically follow suit.
  110. """
  111. def __le__(self, other):
  112. if not isinstance(other, Set):
  113. return NotImplemented
  114. if len(self) > len(other):
  115. return False
  116. for elem in self:
  117. if elem not in other:
  118. return False
  119. return True
  120. def __lt__(self, other):
  121. if not isinstance(other, Set):
  122. return NotImplemented
  123. return len(self) < len(other) and self.__le__(other)
  124. def __gt__(self, other):
  125. if not isinstance(other, Set):
  126. return NotImplemented
  127. return len(self) > len(other) and self.__ge__(other)
  128. def __ge__(self, other):
  129. if not isinstance(other, Set):
  130. return NotImplemented
  131. if len(self) < len(other):
  132. return False
  133. for elem in other:
  134. if elem not in self:
  135. return False
  136. return True
  137. def __eq__(self, other):
  138. if not isinstance(other, Set):
  139. return NotImplemented
  140. return len(self) == len(other) and self.__le__(other)
  141. def __ne__(self, other):
  142. return not (self == other)
  143. @classmethod
  144. def _from_iterable(cls, it):
  145. '''Construct an instance of the class from any iterable input.
  146. Must override this method if the class constructor signature
  147. does not accept an iterable for an input.
  148. '''
  149. return cls(it)
  150. def __and__(self, other):
  151. if not isinstance(other, Iterable):
  152. return NotImplemented
  153. return self._from_iterable(value for value in other if value in self)
  154. __rand__ = __and__
  155. def isdisjoint(self, other):
  156. 'Return True if two sets have a null intersection.'
  157. for value in other:
  158. if value in self:
  159. return False
  160. return True
  161. def __or__(self, other):
  162. if not isinstance(other, Iterable):
  163. return NotImplemented
  164. chain = (e for s in (self, other) for e in s)
  165. return self._from_iterable(chain)
  166. __ror__ = __or__
  167. def __sub__(self, other):
  168. if not isinstance(other, Set):
  169. if not isinstance(other, Iterable):
  170. return NotImplemented
  171. other = self._from_iterable(other)
  172. return self._from_iterable(value for value in self
  173. if value not in other)
  174. def __rsub__(self, other):
  175. if not isinstance(other, Set):
  176. if not isinstance(other, Iterable):
  177. return NotImplemented
  178. other = self._from_iterable(other)
  179. return self._from_iterable(value for value in other
  180. if value not in self)
  181. def __xor__(self, other):
  182. if not isinstance(other, Set):
  183. if not isinstance(other, Iterable):
  184. return NotImplemented
  185. other = self._from_iterable(other)
  186. return (self - other) | (other - self)
  187. __rxor__ = __xor__
  188. # Sets are not hashable by default, but subclasses can change this
  189. __hash__ = None
  190. def _hash(self):
  191. """Compute the hash value of a set.
  192. Note that we don't define __hash__: not all sets are hashable.
  193. But if you define a hashable set type, its __hash__ should
  194. call this function.
  195. This must be compatible __eq__.
  196. All sets ought to compare equal if they contain the same
  197. elements, regardless of how they are implemented, and
  198. regardless of the order of the elements; so there's not much
  199. freedom for __eq__ or __hash__. We match the algorithm used
  200. by the built-in frozenset type.
  201. """
  202. MAX = sys.maxint
  203. MASK = 2 * MAX + 1
  204. n = len(self)
  205. h = 1927868237 * (n + 1)
  206. h &= MASK
  207. for x in self:
  208. hx = hash(x)
  209. h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
  210. h &= MASK
  211. h = h * 69069 + 907133923
  212. h &= MASK
  213. if h > MAX:
  214. h -= MASK + 1
  215. if h == -1:
  216. h = 590923713
  217. return h
  218. Set.register(frozenset)
  219. class MutableSet(Set):
  220. """A mutable set is a finite, iterable container.
  221. This class provides concrete generic implementations of all
  222. methods except for __contains__, __iter__, __len__,
  223. add(), and discard().
  224. To override the comparisons (presumably for speed, as the
  225. semantics are fixed), all you have to do is redefine __le__ and
  226. then the other operations will automatically follow suit.
  227. """
  228. @abstractmethod
  229. def add(self, value):
  230. """Add an element."""
  231. raise NotImplementedError
  232. @abstractmethod
  233. def discard(self, value):
  234. """Remove an element. Do not raise an exception if absent."""
  235. raise NotImplementedError
  236. def remove(self, value):
  237. """Remove an element. If not a member, raise a KeyError."""
  238. if value not in self:
  239. raise KeyError(value)
  240. self.discard(value)
  241. def pop(self):
  242. """Return the popped value. Raise KeyError if empty."""
  243. it = iter(self)
  244. try:
  245. value = next(it)
  246. except StopIteration:
  247. raise KeyError
  248. self.discard(value)
  249. return value
  250. def clear(self):
  251. """This is slow (creates N new iterators!) but effective."""
  252. try:
  253. while True:
  254. self.pop()
  255. except KeyError:
  256. pass
  257. def __ior__(self, it):
  258. for value in it:
  259. self.add(value)
  260. return self
  261. def __iand__(self, it):
  262. for value in (self - it):
  263. self.discard(value)
  264. return self
  265. def __ixor__(self, it):
  266. if it is self:
  267. self.clear()
  268. else:
  269. if not isinstance(it, Set):
  270. it = self._from_iterable(it)
  271. for value in it:
  272. if value in self:
  273. self.discard(value)
  274. else:
  275. self.add(value)
  276. return self
  277. def __isub__(self, it):
  278. if it is self:
  279. self.clear()
  280. else:
  281. for value in it:
  282. self.discard(value)
  283. return self
  284. MutableSet.register(set)
  285. ### MAPPINGS ###
  286. class Mapping(Sized, Iterable, Container):
  287. """A Mapping is a generic container for associating key/value
  288. pairs.
  289. This class provides concrete generic implementations of all
  290. methods except for __getitem__, __iter__, and __len__.
  291. """
  292. @abstractmethod
  293. def __getitem__(self, key):
  294. raise KeyError
  295. def get(self, key, default=None):
  296. 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
  297. try:
  298. return self[key]
  299. except KeyError:
  300. return default
  301. def __contains__(self, key):
  302. try:
  303. self[key]
  304. except KeyError:
  305. return False
  306. else:
  307. return True
  308. def iterkeys(self):
  309. 'D.iterkeys() -> an iterator over the keys of D'
  310. return iter(self)
  311. def itervalues(self):
  312. 'D.itervalues() -> an iterator over the values of D'
  313. for key in self:
  314. yield self[key]
  315. def iteritems(self):
  316. 'D.iteritems() -> an iterator over the (key, value) items of D'
  317. for key in self:
  318. yield (key, self[key])
  319. def keys(self):
  320. "D.keys() -> list of D's keys"
  321. return list(self)
  322. def items(self):
  323. "D.items() -> list of D's (key, value) pairs, as 2-tuples"
  324. return [(key, self[key]) for key in self]
  325. def values(self):
  326. "D.values() -> list of D's values"
  327. return [self[key] for key in self]
  328. # Mappings are not hashable by default, but subclasses can change this
  329. __hash__ = None
  330. def __eq__(self, other):
  331. if not isinstance(other, Mapping):
  332. return NotImplemented
  333. return dict(self.items()) == dict(other.items())
  334. def __ne__(self, other):
  335. return not (self == other)
  336. class MappingView(Sized):
  337. def __init__(self, mapping):
  338. self._mapping = mapping
  339. def __len__(self):
  340. return len(self._mapping)
  341. def __repr__(self):
  342. return '{0.__class__.__name__}({0._mapping!r})'.format(self)
  343. class KeysView(MappingView, Set):
  344. @classmethod
  345. def _from_iterable(self, it):
  346. return set(it)
  347. def __contains__(self, key):
  348. return key in self._mapping
  349. def __iter__(self):
  350. for key in self._mapping:
  351. yield key
  352. KeysView.register(type({}.viewkeys()))
  353. class ItemsView(MappingView, Set):
  354. @classmethod
  355. def _from_iterable(self, it):
  356. return set(it)
  357. def __contains__(self, item):
  358. key, value = item
  359. try:
  360. v = self._mapping[key]
  361. except KeyError:
  362. return False
  363. else:
  364. return v == value
  365. def __iter__(self):
  366. for key in self._mapping:
  367. yield (key, self._mapping[key])
  368. ItemsView.register(type({}.viewitems()))
  369. class ValuesView(MappingView):
  370. def __contains__(self, value):
  371. for key in self._mapping:
  372. if value == self._mapping[key]:
  373. return True
  374. return False
  375. def __iter__(self):
  376. for key in self._mapping:
  377. yield self._mapping[key]
  378. ValuesView.register(type({}.viewvalues()))
  379. class MutableMapping(Mapping):
  380. """A MutableMapping is a generic container for associating
  381. key/value pairs.
  382. This class provides concrete generic implementations of all
  383. methods except for __getitem__, __setitem__, __delitem__,
  384. __iter__, and __len__.
  385. """
  386. @abstractmethod
  387. def __setitem__(self, key, value):
  388. raise KeyError
  389. @abstractmethod
  390. def __delitem__(self, key):
  391. raise KeyError
  392. __marker = object()
  393. def pop(self, key, default=__marker):
  394. '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  395. If key is not found, d is returned if given, otherwise KeyError is raised.
  396. '''
  397. try:
  398. value = self[key]
  399. except KeyError:
  400. if default is self.__marker:
  401. raise
  402. return default
  403. else:
  404. del self[key]
  405. return value
  406. def popitem(self):
  407. '''D.popitem() -> (k, v), remove and return some (key, value) pair
  408. as a 2-tuple; but raise KeyError if D is empty.
  409. '''
  410. try:
  411. key = next(iter(self))
  412. except StopIteration:
  413. raise KeyError
  414. value = self[key]
  415. del self[key]
  416. return key, value
  417. def clear(self):
  418. 'D.clear() -> None. Remove all items from D.'
  419. try:
  420. while True:
  421. self.popitem()
  422. except KeyError:
  423. pass
  424. def update(*args, **kwds):
  425. ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
  426. If E present and has a .keys() method, does: for k in E: D[k] = E[k]
  427. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
  428. In either case, this is followed by: for k, v in F.items(): D[k] = v
  429. '''
  430. if not args:
  431. raise TypeError("descriptor 'update' of 'MutableMapping' object "
  432. "needs an argument")
  433. self = args[0]
  434. args = args[1:]
  435. if len(args) > 1:
  436. raise TypeError('update expected at most 1 arguments, got %d' %
  437. len(args))
  438. if args:
  439. other = args[0]
  440. if isinstance(other, Mapping):
  441. for key in other:
  442. self[key] = other[key]
  443. elif hasattr(other, "keys"):
  444. for key in other.keys():
  445. self[key] = other[key]
  446. else:
  447. for key, value in other:
  448. self[key] = value
  449. for key, value in kwds.items():
  450. self[key] = value
  451. def setdefault(self, key, default=None):
  452. 'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
  453. try:
  454. return self[key]
  455. except KeyError:
  456. self[key] = default
  457. return default
  458. MutableMapping.register(dict)
  459. ### SEQUENCES ###
  460. class Sequence(Sized, Iterable, Container):
  461. """All the operations on a read-only sequence.
  462. Concrete subclasses must override __new__ or __init__,
  463. __getitem__, and __len__.
  464. """
  465. @abstractmethod
  466. def __getitem__(self, index):
  467. raise IndexError
  468. def __iter__(self):
  469. i = 0
  470. try:
  471. while True:
  472. v = self[i]
  473. yield v
  474. i += 1
  475. except IndexError:
  476. return
  477. def __contains__(self, value):
  478. for v in self:
  479. if v == value:
  480. return True
  481. return False
  482. def __reversed__(self):
  483. for i in reversed(range(len(self))):
  484. yield self[i]
  485. def index(self, value):
  486. '''S.index(value) -> integer -- return first index of value.
  487. Raises ValueError if the value is not present.
  488. '''
  489. for i, v in enumerate(self):
  490. if v == value:
  491. return i
  492. raise ValueError
  493. def count(self, value):
  494. 'S.count(value) -> integer -- return number of occurrences of value'
  495. return sum(1 for v in self if v == value)
  496. Sequence.register(tuple)
  497. Sequence.register(basestring)
  498. Sequence.register(buffer)
  499. Sequence.register(xrange)
  500. class MutableSequence(Sequence):
  501. """All the operations on a read-only sequence.
  502. Concrete subclasses must provide __new__ or __init__,
  503. __getitem__, __setitem__, __delitem__, __len__, and insert().
  504. """
  505. @abstractmethod
  506. def __setitem__(self, index, value):
  507. raise IndexError
  508. @abstractmethod
  509. def __delitem__(self, index):
  510. raise IndexError
  511. @abstractmethod
  512. def insert(self, index, value):
  513. 'S.insert(index, object) -- insert object before index'
  514. raise IndexError
  515. def append(self, value):
  516. 'S.append(object) -- append object to the end of the sequence'
  517. self.insert(len(self), value)
  518. def reverse(self):
  519. 'S.reverse() -- reverse *IN PLACE*'
  520. n = len(self)
  521. for i in range(n//2):
  522. self[i], self[n-i-1] = self[n-i-1], self[i]
  523. def extend(self, values):
  524. 'S.extend(iterable) -- extend sequence by appending elements from the iterable'
  525. for v in values:
  526. self.append(v)
  527. def pop(self, index=-1):
  528. '''S.pop([index]) -> item -- remove and return item at index (default last).
  529. Raise IndexError if list is empty or index is out of range.
  530. '''
  531. v = self[index]
  532. del self[index]
  533. return v
  534. def remove(self, value):
  535. '''S.remove(value) -- remove first occurrence of value.
  536. Raise ValueError if the value is not present.
  537. '''
  538. del self[self.index(value)]
  539. def __iadd__(self, values):
  540. self.extend(values)
  541. return self
  542. MutableSequence.register(list)