Mercurial > cpython-lru_cache-weakref
annotate Lib/_abcoll.py @ 57303:4aa4c19dfaed legacy-trunk
#1757126: fix typo with the cyrillic_asian alias
| author | Philip Jenvey <pjenvey@underboss.org> |
|---|---|
| date | Tue, 17 Nov 2009 02:42:26 +0000 |
| parents | dacad42836dd |
| children | d92878bba7bb |
| rev | line source |
|---|---|
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Copyright 2007 Google, Inc. All Rights Reserved. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 # Licensed to PSF under a Contributor Agreement. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 """Abstract Base Classes (ABCs) for collections, according to PEP 3119. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 DON'T USE THIS MODULE DIRECTLY! The classes here should be imported |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 via collections; they are defined here only to alleviate certain |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 bootstrapping issues. Unit tests are in test_collections. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 """ |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 from abc import ABCMeta, abstractmethod |
|
47812
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47509
diff
changeset
|
12 import sys |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 __all__ = ["Hashable", "Iterable", "Iterator", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 "Sized", "Container", "Callable", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 "Set", "MutableSet", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 "Mapping", "MutableMapping", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 "MappingView", "KeysView", "ItemsView", "ValuesView", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 "Sequence", "MutableSequence", |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 ### ONE-TRICK PONIES ### |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 class Hashable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 __metaclass__ = ABCMeta |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 def __hash__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 return 0 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 if cls is Hashable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 for B in C.__mro__: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 if "__hash__" in B.__dict__: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 if B.__dict__["__hash__"]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 break |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 class Iterable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 __metaclass__ = ABCMeta |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 while False: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 yield None |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 if cls is Iterable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 if any("__iter__" in B.__dict__ for B in C.__mro__): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 Iterable.register(str) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
45242
97b420b2d8d1
Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents:
45238
diff
changeset
|
60 class Iterator(Iterable): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 @abstractmethod |
|
51148
9c5aef9f1fa7
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50788
diff
changeset
|
63 def next(self): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 raise StopIteration |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 return self |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 if cls is Iterator: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 if any("next" in B.__dict__ for B in C.__mro__): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 class Sized: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 __metaclass__ = ABCMeta |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 def __len__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 return 0 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 if cls is Sized: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 if any("__len__" in B.__dict__ for B in C.__mro__): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 class Container: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 __metaclass__ = ABCMeta |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 def __contains__(self, x): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 if cls is Container: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 if any("__contains__" in B.__dict__ for B in C.__mro__): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 class Callable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 __metaclass__ = ABCMeta |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 @abstractmethod |
|
45644
4d361f01641c
Issue 2226: Callable checked for the wrong abstract method.
Raymond Hettinger <python@rcn.com>
parents:
45643
diff
changeset
|
111 def __call__(self, *args, **kwds): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 def __subclasshook__(cls, C): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 if cls is Callable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 if any("__call__" in B.__dict__ for B in C.__mro__): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 ### SETS ### |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
45242
97b420b2d8d1
Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents:
45238
diff
changeset
|
125 class Set(Sized, Iterable, Container): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 """A set is a finite, iterable container. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 This class provides concrete generic implementations of all |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 methods except for __contains__, __iter__ and __len__. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 To override the comparisons (presumably for speed, as the |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 semantics are fixed), all you have to do is redefine __le__ and |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 then the other operations will automatically follow suit. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 """ |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 def __le__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 if not isinstance(other, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 if len(self) > len(other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 for elem in self: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 if elem not in other: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 def __lt__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 if not isinstance(other, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 return len(self) < len(other) and self.__le__(other) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 |
|
45238
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
151 def __gt__(self, other): |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
152 if not isinstance(other, Set): |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
153 return NotImplemented |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
154 return other < self |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
155 |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
156 def __ge__(self, other): |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
157 if not isinstance(other, Set): |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
158 return NotImplemented |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
159 return other <= self |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
160 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 def __eq__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 if not isinstance(other, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 return len(self) == len(other) and self.__le__(other) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 |
|
45238
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
166 def __ne__(self, other): |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
167 return not (self == other) |
|
77c5791b7c21
Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents:
45237
diff
changeset
|
168 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 @classmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 def _from_iterable(cls, it): |
| 45211 | 171 '''Construct an instance of the class from any iterable input. |
| 172 | |
| 173 Must override this method if the class constructor signature | |
| 45247 | 174 does not accept an iterable for an input. |
| 45211 | 175 ''' |
| 45247 | 176 return cls(it) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 def __and__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 if not isinstance(other, Iterable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 return self._from_iterable(value for value in other if value in self) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 |
|
45046
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
183 def isdisjoint(self, other): |
|
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
184 for value in other: |
|
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
185 if value in self: |
|
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
186 return False |
|
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
187 return True |
|
96d0249164fc
Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents:
43958
diff
changeset
|
188 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 def __or__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 if not isinstance(other, Iterable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 return NotImplemented |
|
45643
2d551d046022
Remove dependency on itertools -- a simple genexp suffices.
Raymond Hettinger <python@rcn.com>
parents:
45641
diff
changeset
|
192 chain = (e for s in (self, other) for e in s) |
|
2d551d046022
Remove dependency on itertools -- a simple genexp suffices.
Raymond Hettinger <python@rcn.com>
parents:
45641
diff
changeset
|
193 return self._from_iterable(chain) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 def __sub__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 if not isinstance(other, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 if not isinstance(other, Iterable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 other = self._from_iterable(other) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return self._from_iterable(value for value in self |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 if value not in other) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 def __xor__(self, other): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 if not isinstance(other, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 if not isinstance(other, Iterable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 return NotImplemented |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 other = self._from_iterable(other) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 return (self - other) | (other - self) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
210 # Sets are not hashable by default, but subclasses can change this |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
211 __hash__ = None |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
212 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 def _hash(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 """Compute the hash value of a set. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 Note that we don't define __hash__: not all sets are hashable. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 But if you define a hashable set type, its __hash__ should |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 call this function. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 This must be compatible __eq__. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 All sets ought to compare equal if they contain the same |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 elements, regardless of how they are implemented, and |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 regardless of the order of the elements; so there's not much |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 freedom for __eq__ or __hash__. We match the algorithm used |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 by the built-in frozenset type. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 """ |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 MAX = sys.maxint |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 MASK = 2 * MAX + 1 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 n = len(self) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 h = 1927868237 * (n + 1) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 h &= MASK |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 for x in self: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 hx = hash(x) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 h &= MASK |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 h = h * 69069 + 907133923 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 h &= MASK |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 if h > MAX: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 h -= MASK + 1 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 if h == -1: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 h = 590923713 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 return h |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 Set.register(frozenset) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 class MutableSet(Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 def add(self, value): |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48213
diff
changeset
|
252 """Add an element.""" |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 raise NotImplementedError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 def discard(self, value): |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48213
diff
changeset
|
257 """Remove an element. Do not raise an exception if absent.""" |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 raise NotImplementedError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
45047
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
260 def remove(self, value): |
|
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
261 """Remove an element. If not a member, raise a KeyError.""" |
|
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
262 if value not in self: |
|
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
263 raise KeyError(value) |
|
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
264 self.discard(value) |
|
b7aa8bde7bcf
MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents:
45046
diff
changeset
|
265 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 def pop(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 """Return the popped value. Raise KeyError if empty.""" |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 it = iter(self) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 try: |
|
51148
9c5aef9f1fa7
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50788
diff
changeset
|
270 value = next(it) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 except StopIteration: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 raise KeyError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 self.discard(value) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 return value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 def clear(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 """This is slow (creates N new iterators!) but effective.""" |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 while True: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 self.pop() |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 pass |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 def __ior__(self, it): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 for value in it: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 self.add(value) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 return self |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
52661
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
51889
diff
changeset
|
289 def __iand__(self, it): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
51889
diff
changeset
|
290 for value in (self - it): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
51889
diff
changeset
|
291 self.discard(value) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 return self |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 def __ixor__(self, it): |
|
45068
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
295 if not isinstance(it, Set): |
|
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
296 it = self._from_iterable(it) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 for value in it: |
|
45068
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
298 if value in self: |
|
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
299 self.discard(value) |
|
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
300 else: |
|
de7c3097b2c1
Fix defect in __ixor__ which would get the wrong
Raymond Hettinger <python@rcn.com>
parents:
45047
diff
changeset
|
301 self.add(value) |
| 45069 | 302 return self |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 def __isub__(self, it): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 for value in it: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 self.discard(value) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 return self |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 MutableSet.register(set) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 ### MAPPINGS ### |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
45242
97b420b2d8d1
Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents:
45238
diff
changeset
|
315 class Mapping(Sized, Iterable, Container): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 def __getitem__(self, key): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 raise KeyError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 def get(self, key, default=None): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 return self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 return default |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 def __contains__(self, key): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 else: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 |
|
47509
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
335 def iterkeys(self): |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
336 return iter(self) |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
337 |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
338 def itervalues(self): |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
339 for key in self: |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
340 yield self[key] |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
341 |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
342 def iteritems(self): |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
343 for key in self: |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
344 yield (key, self[key]) |
|
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
345 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 def keys(self): |
|
47509
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
347 return list(self) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 def items(self): |
|
47509
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
350 return [(key, self[key]) for key in self] |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 def values(self): |
|
47509
7201c0193ade
#3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents:
45644
diff
changeset
|
353 return [self[key] for key in self] |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
355 # Mappings are not hashable by default, but subclasses can change this |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
356 __hash__ = None |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
357 |
|
45188
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
358 def __eq__(self, other): |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
359 return isinstance(other, Mapping) and \ |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
360 dict(self.items()) == dict(other.items()) |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
361 |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
362 def __ne__(self, other): |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
363 return not (self == other) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 |
|
45242
97b420b2d8d1
Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents:
45238
diff
changeset
|
365 class MappingView(Sized): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 def __init__(self, mapping): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 self._mapping = mapping |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 def __len__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 return len(self._mapping) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
51889
105a5317e68e
Give mapping views a usable repr.
Raymond Hettinger <python@rcn.com>
parents:
51837
diff
changeset
|
373 def __repr__(self): |
|
105a5317e68e
Give mapping views a usable repr.
Raymond Hettinger <python@rcn.com>
parents:
51837
diff
changeset
|
374 return '{0.__class__.__name__}({0._mapping!r})'.format(self) |
|
105a5317e68e
Give mapping views a usable repr.
Raymond Hettinger <python@rcn.com>
parents:
51837
diff
changeset
|
375 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 class KeysView(MappingView, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 def __contains__(self, key): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 return key in self._mapping |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 for key in self._mapping: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 yield key |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 class ItemsView(MappingView, Set): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 def __contains__(self, item): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 key, value = item |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 v = self._mapping[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 else: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 return v == value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 for key in self._mapping: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 yield (key, self._mapping[key]) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 class ValuesView(MappingView): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
404 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 def __contains__(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 for key in self._mapping: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 if value == self._mapping[key]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 for key in self._mapping: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 yield self._mapping[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 class MutableMapping(Mapping): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 def __setitem__(self, key, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 raise KeyError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 def __delitem__(self, key): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 raise KeyError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 __marker = object() |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 def pop(self, key, default=__marker): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 value = self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 if default is self.__marker: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 raise |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 return default |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 else: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 del self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 return value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 def popitem(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 key = next(iter(self)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 except StopIteration: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 raise KeyError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 value = self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 del self[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 return key, value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
447 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 def clear(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 try: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 while True: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 self.popitem() |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 except KeyError: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 pass |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 def update(self, other=(), **kwds): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 if isinstance(other, Mapping): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 for key in other: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 self[key] = other[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 elif hasattr(other, "keys"): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 for key in other.keys(): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 self[key] = other[key] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 else: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 for key, value in other: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 self[key] = value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 for key, value in kwds.items(): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 self[key] = value |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 |
|
45188
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
468 def setdefault(self, key, default=None): |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
469 try: |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
470 return self[key] |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
471 except KeyError: |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
472 self[key] = default |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
473 return default |
|
1653f5ff3c00
Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents:
45069
diff
changeset
|
474 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 MutableMapping.register(dict) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 ### SEQUENCES ### |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 |
|
45242
97b420b2d8d1
Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents:
45238
diff
changeset
|
481 class Sequence(Sized, Iterable, Container): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 """All the operations on a read-only sequence. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 Concrete subclasses must override __new__ or __init__, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 __getitem__, and __len__. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 """ |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 def __getitem__(self, index): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 raise IndexError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 i = 0 |
|
45237
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
494 try: |
|
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
495 while True: |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 v = self[i] |
|
45237
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
497 yield v |
|
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
498 i += 1 |
|
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
499 except IndexError: |
|
82d44280c135
Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents:
45211
diff
changeset
|
500 return |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 def __contains__(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 for v in self: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 if v == value: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 return True |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 return False |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 def __reversed__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 for i in reversed(range(len(self))): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 yield self[i] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 def index(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 for i, v in enumerate(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 if v == value: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 return i |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 raise ValueError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 def count(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 return sum(1 for v in self if v == value) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 Sequence.register(tuple) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 Sequence.register(basestring) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 Sequence.register(buffer) |
|
51837
faf968212f31
Backport 69934: Register xrange() as a Sequence.
Raymond Hettinger <python@rcn.com>
parents:
51148
diff
changeset
|
524 Sequence.register(xrange) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 class MutableSequence(Sequence): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 def __setitem__(self, index, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 raise IndexError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 def __delitem__(self, index): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 raise IndexError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 @abstractmethod |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 def insert(self, index, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 raise IndexError |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 def append(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 self.insert(len(self), value) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 def reverse(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 n = len(self) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 for i in range(n//2): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 self[i], self[n-i-1] = self[n-i-1], self[i] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
549 def extend(self, values): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 for v in values: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 self.append(v) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 def pop(self, index=-1): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 v = self[index] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 del self[index] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 return v |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 def remove(self, value): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 del self[self.index(value)] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
560 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 def __iadd__(self, values): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
562 self.extend(values) |
|
54197
dacad42836dd
Issue 6037: MutableSequence.__iadd__ should return self.
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
563 return self |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
565 MutableSequence.register(list) |
