1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
19
20 """A simple set class.
21
22 This class was originally used to deal with sets being missing in
23 ancient versions of python, but dnspython will continue to use it
24 as these sets are based on lists and are thus indexable, and this
25 ability is widely used in dnspython applications.
26 """
27
28 __slots__ = ['items']
29
31 """Initialize the set.
32
33 *items*, an iterable or ``None``, the initial set of items.
34 """
35
36 self.items = []
37 if items is not None:
38 for item in items:
39 self.add(item)
40
42 return "dns.simpleset.Set(%s)" % repr(self.items)
43
44 - def add(self, item):
45 """Add an item to the set.
46 """
47
48 if item not in self.items:
49 self.items.append(item)
50
52 """Remove an item from the set.
53 """
54
55 self.items.remove(item)
56
58 """Remove an item from the set if present.
59 """
60
61 try:
62 self.items.remove(item)
63 except ValueError:
64 pass
65
67 """Make a (shallow) copy of the set.
68
69 There is a 'clone protocol' that subclasses of this class
70 should use. To make a copy, first call your super's _clone()
71 method, and use the object returned as the new instance. Then
72 make shallow copies of the attributes defined in the subclass.
73
74 This protocol allows us to write the set algorithms that
75 return new instances (e.g. union) once, and keep using them in
76 subclasses.
77 """
78
79 cls = self.__class__
80 obj = cls.__new__(cls)
81 obj.items = list(self.items)
82 return obj
83
85 """Make a (shallow) copy of the set.
86 """
87
88 return self._clone()
89
91 """Make a (shallow) copy of the set.
92 """
93
94 return self._clone()
95
97 """Update the set, adding any elements from other which are not
98 already in the set.
99 """
100
101 if not isinstance(other, Set):
102 raise ValueError('other must be a Set instance')
103 if self is other:
104 return
105 for item in other.items:
106 self.add(item)
107
109 """Update the set, removing any elements from other which are not
110 in both sets.
111 """
112
113 if not isinstance(other, Set):
114 raise ValueError('other must be a Set instance')
115 if self is other:
116 return
117
118
119 for item in list(self.items):
120 if item not in other.items:
121 self.items.remove(item)
122
124 """Update the set, removing any elements from other which are in
125 the set.
126 """
127
128 if not isinstance(other, Set):
129 raise ValueError('other must be a Set instance')
130 if self is other:
131 self.items = []
132 else:
133 for item in other.items:
134 self.discard(item)
135
137 """Return a new set which is the union of ``self`` and ``other``.
138
139 Returns the same Set type as this set.
140 """
141
142 obj = self._clone()
143 obj.union_update(other)
144 return obj
145
147 """Return a new set which is the intersection of ``self`` and
148 ``other``.
149
150 Returns the same Set type as this set.
151 """
152
153 obj = self._clone()
154 obj.intersection_update(other)
155 return obj
156
158 """Return a new set which ``self`` - ``other``, i.e. the items
159 in ``self`` which are not also in ``other``.
160
161 Returns the same Set type as this set.
162 """
163
164 obj = self._clone()
165 obj.difference_update(other)
166 return obj
167
169 return self.union(other)
170
173
175 return self.union(other)
176
179
183
187
191
195
197 """Update the set, adding any elements from other which are not
198 already in the set.
199
200 *other*, the collection of items with which to update the set, which
201 may be any iterable type.
202 """
203
204 for item in other:
205 self.add(item)
206
208 """Make the set empty."""
209 self.items = []
210
212
213
214 for item in self.items:
215 if item not in other.items:
216 return False
217 for item in other.items:
218 if item not in self.items:
219 return False
220 return True
221
223 return not self.__eq__(other)
224
226 return len(self.items)
227
229 return iter(self.items)
230
233
236
238 """Is this set a subset of *other*?
239
240 Returns a ``bool``.
241 """
242
243 if not isinstance(other, Set):
244 raise ValueError('other must be a Set instance')
245 for item in self.items:
246 if item not in other.items:
247 return False
248 return True
249
251 """Is this set a superset of *other*?
252
253 Returns a ``bool``.
254 """
255
256 if not isinstance(other, Set):
257 raise ValueError('other must be a Set instance')
258 for item in other.items:
259 if item not in self.items:
260 return False
261 return True
262