annotate Lib/_abcoll.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children 6fc9103d55f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
24 def _hasattr(C, attr):
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
25 try:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
26 return any(attr in B.__dict__ for B in C.__mro__)
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
27 except AttributeError:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
28 # Old-style class
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
29 return hasattr(C, attr)
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
30
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
31
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 class Hashable:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 __metaclass__ = ABCMeta
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 def __hash__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 return 0
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 if cls is Hashable:
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
42 try:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
43 for B in C.__mro__:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
44 if "__hash__" in B.__dict__:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
45 if B.__dict__["__hash__"]:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
46 return True
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
47 break
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
48 except AttributeError:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
49 # Old-style class
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
50 if getattr(C, "__hash__", None):
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
51 return True
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 class Iterable:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 __metaclass__ = ABCMeta
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 while False:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 yield None
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if cls is Iterable:
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
66 if _hasattr(C, "__iter__"):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 Iterable.register(str)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
45242
97b420b2d8d1 Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents: 45238
diff changeset
73 class Iterator(Iterable):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 @abstractmethod
51148
9c5aef9f1fa7 Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents: 50788
diff changeset
76 def next(self):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 raise StopIteration
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 def __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return self
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 if cls is Iterator:
66545
de6e79c5b1ed Merged revisions 86857 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64269
diff changeset
85 if _hasattr(C, "next") and _hasattr(C, "__iter__"):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 class Sized:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 __metaclass__ = ABCMeta
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 def __len__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return 0
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if cls is Sized:
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
100 if _hasattr(C, "__len__"):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 class Container:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 __metaclass__ = ABCMeta
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def __contains__(self, x):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 if cls is Container:
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
115 if _hasattr(C, "__contains__"):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 class Callable:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 __metaclass__ = ABCMeta
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 @abstractmethod
45644
4d361f01641c Issue 2226: Callable checked for the wrong abstract method.
Raymond Hettinger <python@rcn.com>
parents: 45643
diff changeset
124 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
125 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def __subclasshook__(cls, C):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if cls is Callable:
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 54198
diff changeset
130 if _hasattr(C, "__call__"):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
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 ### SETS ###
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
45242
97b420b2d8d1 Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents: 45238
diff changeset
138 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
139 """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
140
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142 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
143
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 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
145 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
146 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
147 """
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 def __le__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if not isinstance(other, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 if len(self) > len(other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 for elem in self:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if elem not in other:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 def __lt__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 if not isinstance(other, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 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
163
45238
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
164 def __gt__(self, other):
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
165 if not isinstance(other, Set):
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
166 return NotImplemented
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
167 return other < self
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
168
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
169 def __ge__(self, other):
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
170 if not isinstance(other, Set):
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
171 return NotImplemented
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
172 return other <= self
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
173
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 def __eq__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if not isinstance(other, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 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
178
45238
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
179 def __ne__(self, other):
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
180 return not (self == other)
77c5791b7c21 Fill-in missing Set comparisons
Raymond Hettinger <python@rcn.com>
parents: 45237
diff changeset
181
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 @classmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 def _from_iterable(cls, it):
45211
8cbb8aa45ba9 Merge 60627.
Raymond Hettinger <python@rcn.com>
parents: 45188
diff changeset
184 '''Construct an instance of the class from any iterable input.
8cbb8aa45ba9 Merge 60627.
Raymond Hettinger <python@rcn.com>
parents: 45188
diff changeset
185
8cbb8aa45ba9 Merge 60627.
Raymond Hettinger <python@rcn.com>
parents: 45188
diff changeset
186 Must override this method if the class constructor signature
45247
645938e0eb52 Merge with r60683.
Raymond Hettinger <python@rcn.com>
parents: 45242
diff changeset
187 does not accept an iterable for an input.
45211
8cbb8aa45ba9 Merge 60627.
Raymond Hettinger <python@rcn.com>
parents: 45188
diff changeset
188 '''
45247
645938e0eb52 Merge with r60683.
Raymond Hettinger <python@rcn.com>
parents: 45242
diff changeset
189 return cls(it)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 def __and__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if not isinstance(other, Iterable):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 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
195
45046
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
196 def isdisjoint(self, other):
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
197 for value in other:
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
198 if value in self:
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
199 return False
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
200 return True
96d0249164fc Add isdisjoint() to the Set/MutableSet ABCs.
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
201
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 def __or__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 if not isinstance(other, Iterable):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 return NotImplemented
45643
2d551d046022 Remove dependency on itertools -- a simple genexp suffices.
Raymond Hettinger <python@rcn.com>
parents: 45641
diff changeset
205 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
206 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
207
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 def __sub__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if not isinstance(other, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if not isinstance(other, Iterable):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 other = self._from_iterable(other)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 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
214 if value not in other)
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 def __xor__(self, other):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 if not isinstance(other, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 if not isinstance(other, Iterable):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 return NotImplemented
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 other = self._from_iterable(other)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 return (self - other) | (other - self)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
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
223 # 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
224 __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
225
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 def _hash(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 """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
228
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 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
230 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
231 call this function.
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 This must be compatible __eq__.
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 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
236 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
237 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
238 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
239 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
240 """
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 MAX = sys.maxint
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 MASK = 2 * MAX + 1
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 n = len(self)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 h = 1927868237 * (n + 1)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 h &= MASK
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 for x in self:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 hx = hash(x)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 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
249 h &= MASK
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 h = h * 69069 + 907133923
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 h &= MASK
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 if h > MAX:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 h -= MASK + 1
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 if h == -1:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 h = 590923713
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 return h
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 Set.register(frozenset)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 class MutableSet(Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 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
265 """Add an element."""
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 raise NotImplementedError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 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
270 """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
271 raise NotImplementedError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
45047
b7aa8bde7bcf MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents: 45046
diff changeset
273 def remove(self, value):
b7aa8bde7bcf MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents: 45046
diff changeset
274 """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
275 if value not in self:
b7aa8bde7bcf MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents: 45046
diff changeset
276 raise KeyError(value)
b7aa8bde7bcf MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents: 45046
diff changeset
277 self.discard(value)
b7aa8bde7bcf MutableSets support a remove() method.
Raymond Hettinger <python@rcn.com>
parents: 45046
diff changeset
278
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 def pop(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 """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
281 it = iter(self)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 try:
51148
9c5aef9f1fa7 Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents: 50788
diff changeset
283 value = next(it)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 except StopIteration:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 raise KeyError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 self.discard(value)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 return value
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 def clear(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 """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
291 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 while True:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.pop()
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 pass
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 def __ior__(self, it):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 for value in it:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 self.add(value)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 return self
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
52662
c337f2028d8c Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents: 51837
diff changeset
302 def __iand__(self, it):
c337f2028d8c Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents: 51837
diff changeset
303 for value in (self - it):
c337f2028d8c Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents: 51837
diff changeset
304 self.discard(value)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 return self
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 def __ixor__(self, it):
64269
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
308 if it is self:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
309 self.clear()
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
310 else:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
311 if not isinstance(it, Set):
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
312 it = self._from_iterable(it)
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
313 for value in it:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
314 if value in self:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
315 self.discard(value)
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
316 else:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
317 self.add(value)
45069
e77b82aed1a8 Missing return
Raymond Hettinger <python@rcn.com>
parents: 45068
diff changeset
318 return self
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 def __isub__(self, it):
64269
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
321 if it is self:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
322 self.clear()
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
323 else:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
324 for value in it:
6180f4564714 Merged revisions 84301 via svnmerge from
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64221
diff changeset
325 self.discard(value)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 return self
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 MutableSet.register(set)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 ### MAPPINGS ###
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
45242
97b420b2d8d1 Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents: 45238
diff changeset
334 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
335
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 def __getitem__(self, key):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 raise KeyError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 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
341 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 return self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return default
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 def __contains__(self, key):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 else:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
47509
7201c0193ade #3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents: 45644
diff changeset
354 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
355 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
356
7201c0193ade #3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents: 45644
diff changeset
357 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
358 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
359 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
360
7201c0193ade #3057: Fix the MutableMapping ABC to use the 2.6 dict interface.
Georg Brandl <georg@python.org>
parents: 45644
diff changeset
361 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
362 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
363 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
364
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 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
366 return list(self)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 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
369 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
370
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 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
372 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
373
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
374 # 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
375 __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
376
45188
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
377 def __eq__(self, other):
61815
23e38192af96 Merged revisions 81414 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59948
diff changeset
378 if not isinstance(other, Mapping):
23e38192af96 Merged revisions 81414 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59948
diff changeset
379 return NotImplemented
23e38192af96 Merged revisions 81414 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59948
diff changeset
380 return dict(self.items()) == dict(other.items())
45188
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
381
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
382 def __ne__(self, other):
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
383 return not (self == other)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
45242
97b420b2d8d1 Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents: 45238
diff changeset
385 class MappingView(Sized):
43958
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 def __init__(self, mapping):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 self._mapping = mapping
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 def __len__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 return len(self._mapping)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
51889
105a5317e68e Give mapping views a usable repr.
Raymond Hettinger <python@rcn.com>
parents: 51837
diff changeset
393 def __repr__(self):
105a5317e68e Give mapping views a usable repr.
Raymond Hettinger <python@rcn.com>
parents: 51837
diff changeset
394 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
395
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 class KeysView(MappingView, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
64221
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
399 @classmethod
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
400 def _from_iterable(self, it):
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
401 return set(it)
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
402
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 def __contains__(self, key):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 return key in self._mapping
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 def __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 for key in self._mapping:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 yield key
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
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 class ItemsView(MappingView, Set):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
64221
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
413 @classmethod
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
414 def _from_iterable(self, it):
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
415 return set(it)
9c4e85785d4e Issue #9214: Fix set operations on KeysView and ItemsView.
Raymond Hettinger <python@rcn.com>
parents: 62973
diff changeset
416
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 def __contains__(self, item):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 key, value = item
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 v = self._mapping[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 return False
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 else:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 return v == value
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 def __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 for key in self._mapping:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 yield (key, self._mapping[key])
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 class ValuesView(MappingView):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 def __contains__(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 for key in self._mapping:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 if value == self._mapping[key]:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 return False
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 __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 for key in self._mapping:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 yield self._mapping[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 class MutableMapping(Mapping):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 def __setitem__(self, key, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 raise KeyError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 def __delitem__(self, key):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 raise KeyError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 __marker = object()
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 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
457 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 value = self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 if default is self.__marker:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 raise
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 return default
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 else:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 del self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 return value
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 def popitem(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 key = next(iter(self))
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 except StopIteration:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 raise KeyError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 value = self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 del self[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 return key, value
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 def clear(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 try:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 while True:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 self.popitem()
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 except KeyError:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 pass
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
62973
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
483 def update(*args, **kwds):
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
484 if len(args) > 2:
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
485 raise TypeError("update() takes at most 2 positional "
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
486 "arguments ({} given)".format(len(args)))
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
487 elif not args:
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
488 raise TypeError("update() takes at least 1 argument (0 given)")
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
489 self = args[0]
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
490 other = args[1] if len(args) >= 2 else ()
1129d4dfeed6 Merged revisions 82821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61804
diff changeset
491
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 if isinstance(other, Mapping):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 for key in other:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 self[key] = other[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 elif hasattr(other, "keys"):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 for key in other.keys():
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 self[key] = other[key]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 else:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 for key, value in other:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 self[key] = value
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 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
502 self[key] = value
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
45188
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
504 def setdefault(self, key, default=None):
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
505 try:
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
506 return self[key]
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
507 except KeyError:
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
508 self[key] = default
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
509 return default
1653f5ff3c00 Sync-up with Py3k work.
Raymond Hettinger <python@rcn.com>
parents: 45069
diff changeset
510
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 MutableMapping.register(dict)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 ### SEQUENCES ###
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516
45242
97b420b2d8d1 Make ABC containers inherit as documented.
Raymond Hettinger <python@rcn.com>
parents: 45238
diff changeset
517 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
518 """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
519
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 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
521 __getitem__, and __len__.
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 """
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 def __getitem__(self, index):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 raise IndexError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 def __iter__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 i = 0
45237
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
530 try:
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
531 while True:
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 v = self[i]
45237
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
533 yield v
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
534 i += 1
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
535 except IndexError:
82d44280c135 Speed-up __iter__() mixin method.
Raymond Hettinger <python@rcn.com>
parents: 45211
diff changeset
536 return
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 def __contains__(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 for v in self:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 if v == value:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 return True
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 return False
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 __reversed__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 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
546 yield self[i]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 def index(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 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
550 if v == value:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 return i
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 raise ValueError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 def count(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 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
556
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 Sequence.register(tuple)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 Sequence.register(basestring)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 Sequence.register(buffer)
51837
faf968212f31 Backport 69934: Register xrange() as a Sequence.
Raymond Hettinger <python@rcn.com>
parents: 51148
diff changeset
560 Sequence.register(xrange)
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 class MutableSequence(Sequence):
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 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 def __setitem__(self, index, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 raise IndexError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 def __delitem__(self, index):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 raise IndexError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 @abstractmethod
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 def insert(self, index, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 raise IndexError
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 def append(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 self.insert(len(self), value)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 def reverse(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 n = len(self)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 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
583 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
584
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 def extend(self, values):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 for v in values:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 self.append(v)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 def pop(self, index=-1):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 v = self[index]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 del self[index]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 return v
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 def remove(self, value):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 del self[self.index(value)]
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 def __iadd__(self, values):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 self.extend(values)
54198
c136887db7ef Issue 6037: MutableSequence.__iadd__ should return self.
Raymond Hettinger <python@rcn.com>
parents: 52662
diff changeset
599 return self
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 MutableSequence.register(list)