Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: sets.py
"""Classes to represent arbitrary sets (including sets of sets).
[0] Fix | Delete
[1] Fix | Delete
This module implements sets using dictionaries whose values are
[2] Fix | Delete
ignored. The usual operations (union, intersection, deletion, etc.)
[3] Fix | Delete
are provided as both methods and operators.
[4] Fix | Delete
[5] Fix | Delete
Important: sets are not sequences! While they support 'x in s',
[6] Fix | Delete
'len(s)', and 'for x in s', none of those operations are unique for
[7] Fix | Delete
sequences; for example, mappings support all three as well. The
[8] Fix | Delete
characteristic operation for sequences is subscripting with small
[9] Fix | Delete
integers: s[i], for i in range(len(s)). Sets don't support
[10] Fix | Delete
subscripting at all. Also, sequences allow multiple occurrences and
[11] Fix | Delete
their elements have a definite order; sets on the other hand don't
[12] Fix | Delete
record multiple occurrences and don't remember the order of element
[13] Fix | Delete
insertion (which is why they don't support s[i]).
[14] Fix | Delete
[15] Fix | Delete
The following classes are provided:
[16] Fix | Delete
[17] Fix | Delete
BaseSet -- All the operations common to both mutable and immutable
[18] Fix | Delete
sets. This is an abstract class, not meant to be directly
[19] Fix | Delete
instantiated.
[20] Fix | Delete
[21] Fix | Delete
Set -- Mutable sets, subclass of BaseSet; not hashable.
[22] Fix | Delete
[23] Fix | Delete
ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
[24] Fix | Delete
An iterable argument is mandatory to create an ImmutableSet.
[25] Fix | Delete
[26] Fix | Delete
_TemporarilyImmutableSet -- A wrapper around a Set, hashable,
[27] Fix | Delete
giving the same hash value as the immutable set equivalent
[28] Fix | Delete
would have. Do not use this class directly.
[29] Fix | Delete
[30] Fix | Delete
Only hashable objects can be added to a Set. In particular, you cannot
[31] Fix | Delete
really add a Set as an element to another Set; if you try, what is
[32] Fix | Delete
actually added is an ImmutableSet built from it (it compares equal to
[33] Fix | Delete
the one you tried adding).
[34] Fix | Delete
[35] Fix | Delete
When you ask if `x in y' where x is a Set and y is a Set or
[36] Fix | Delete
ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
[37] Fix | Delete
what's tested is actually `z in y'.
[38] Fix | Delete
[39] Fix | Delete
"""
[40] Fix | Delete
[41] Fix | Delete
# Code history:
[42] Fix | Delete
#
[43] Fix | Delete
# - Greg V. Wilson wrote the first version, using a different approach
[44] Fix | Delete
# to the mutable/immutable problem, and inheriting from dict.
[45] Fix | Delete
#
[46] Fix | Delete
# - Alex Martelli modified Greg's version to implement the current
[47] Fix | Delete
# Set/ImmutableSet approach, and make the data an attribute.
[48] Fix | Delete
#
[49] Fix | Delete
# - Guido van Rossum rewrote much of the code, made some API changes,
[50] Fix | Delete
# and cleaned up the docstrings.
[51] Fix | Delete
#
[52] Fix | Delete
# - Raymond Hettinger added a number of speedups and other
[53] Fix | Delete
# improvements.
[54] Fix | Delete
[55] Fix | Delete
from itertools import ifilter, ifilterfalse
[56] Fix | Delete
[57] Fix | Delete
__all__ = ['BaseSet', 'Set', 'ImmutableSet']
[58] Fix | Delete
[59] Fix | Delete
import warnings
[60] Fix | Delete
warnings.warn("the sets module is deprecated", DeprecationWarning,
[61] Fix | Delete
stacklevel=2)
[62] Fix | Delete
[63] Fix | Delete
class BaseSet(object):
[64] Fix | Delete
"""Common base class for mutable and immutable sets."""
[65] Fix | Delete
[66] Fix | Delete
__slots__ = ['_data']
[67] Fix | Delete
[68] Fix | Delete
# Constructor
[69] Fix | Delete
[70] Fix | Delete
def __init__(self):
[71] Fix | Delete
"""This is an abstract class."""
[72] Fix | Delete
# Don't call this from a concrete subclass!
[73] Fix | Delete
if self.__class__ is BaseSet:
[74] Fix | Delete
raise TypeError, ("BaseSet is an abstract class. "
[75] Fix | Delete
"Use Set or ImmutableSet.")
[76] Fix | Delete
[77] Fix | Delete
# Standard protocols: __len__, __repr__, __str__, __iter__
[78] Fix | Delete
[79] Fix | Delete
def __len__(self):
[80] Fix | Delete
"""Return the number of elements of a set."""
[81] Fix | Delete
return len(self._data)
[82] Fix | Delete
[83] Fix | Delete
def __repr__(self):
[84] Fix | Delete
"""Return string representation of a set.
[85] Fix | Delete
[86] Fix | Delete
This looks like 'Set([<list of elements>])'.
[87] Fix | Delete
"""
[88] Fix | Delete
return self._repr()
[89] Fix | Delete
[90] Fix | Delete
# __str__ is the same as __repr__
[91] Fix | Delete
__str__ = __repr__
[92] Fix | Delete
[93] Fix | Delete
def _repr(self, sorted=False):
[94] Fix | Delete
elements = self._data.keys()
[95] Fix | Delete
if sorted:
[96] Fix | Delete
elements.sort()
[97] Fix | Delete
return '%s(%r)' % (self.__class__.__name__, elements)
[98] Fix | Delete
[99] Fix | Delete
def __iter__(self):
[100] Fix | Delete
"""Return an iterator over the elements or a set.
[101] Fix | Delete
[102] Fix | Delete
This is the keys iterator for the underlying dict.
[103] Fix | Delete
"""
[104] Fix | Delete
return self._data.iterkeys()
[105] Fix | Delete
[106] Fix | Delete
# Three-way comparison is not supported. However, because __eq__ is
[107] Fix | Delete
# tried before __cmp__, if Set x == Set y, x.__eq__(y) returns True and
[108] Fix | Delete
# then cmp(x, y) returns 0 (Python doesn't actually call __cmp__ in this
[109] Fix | Delete
# case).
[110] Fix | Delete
[111] Fix | Delete
def __cmp__(self, other):
[112] Fix | Delete
raise TypeError, "can't compare sets using cmp()"
[113] Fix | Delete
[114] Fix | Delete
# Equality comparisons using the underlying dicts. Mixed-type comparisons
[115] Fix | Delete
# are allowed here, where Set == z for non-Set z always returns False,
[116] Fix | Delete
# and Set != z always True. This allows expressions like "x in y" to
[117] Fix | Delete
# give the expected result when y is a sequence of mixed types, not
[118] Fix | Delete
# raising a pointless TypeError just because y contains a Set, or x is
[119] Fix | Delete
# a Set and y contain's a non-set ("in" invokes only __eq__).
[120] Fix | Delete
# Subtle: it would be nicer if __eq__ and __ne__ could return
[121] Fix | Delete
# NotImplemented instead of True or False. Then the other comparand
[122] Fix | Delete
# would get a chance to determine the result, and if the other comparand
[123] Fix | Delete
# also returned NotImplemented then it would fall back to object address
[124] Fix | Delete
# comparison (which would always return False for __eq__ and always
[125] Fix | Delete
# True for __ne__). However, that doesn't work, because this type
[126] Fix | Delete
# *also* implements __cmp__: if, e.g., __eq__ returns NotImplemented,
[127] Fix | Delete
# Python tries __cmp__ next, and the __cmp__ here then raises TypeError.
[128] Fix | Delete
[129] Fix | Delete
def __eq__(self, other):
[130] Fix | Delete
if isinstance(other, BaseSet):
[131] Fix | Delete
return self._data == other._data
[132] Fix | Delete
else:
[133] Fix | Delete
return False
[134] Fix | Delete
[135] Fix | Delete
def __ne__(self, other):
[136] Fix | Delete
if isinstance(other, BaseSet):
[137] Fix | Delete
return self._data != other._data
[138] Fix | Delete
else:
[139] Fix | Delete
return True
[140] Fix | Delete
[141] Fix | Delete
# Copying operations
[142] Fix | Delete
[143] Fix | Delete
def copy(self):
[144] Fix | Delete
"""Return a shallow copy of a set."""
[145] Fix | Delete
result = self.__class__()
[146] Fix | Delete
result._data.update(self._data)
[147] Fix | Delete
return result
[148] Fix | Delete
[149] Fix | Delete
__copy__ = copy # For the copy module
[150] Fix | Delete
[151] Fix | Delete
def __deepcopy__(self, memo):
[152] Fix | Delete
"""Return a deep copy of a set; used by copy module."""
[153] Fix | Delete
# This pre-creates the result and inserts it in the memo
[154] Fix | Delete
# early, in case the deep copy recurses into another reference
[155] Fix | Delete
# to this same set. A set can't be an element of itself, but
[156] Fix | Delete
# it can certainly contain an object that has a reference to
[157] Fix | Delete
# itself.
[158] Fix | Delete
from copy import deepcopy
[159] Fix | Delete
result = self.__class__()
[160] Fix | Delete
memo[id(self)] = result
[161] Fix | Delete
data = result._data
[162] Fix | Delete
value = True
[163] Fix | Delete
for elt in self:
[164] Fix | Delete
data[deepcopy(elt, memo)] = value
[165] Fix | Delete
return result
[166] Fix | Delete
[167] Fix | Delete
# Standard set operations: union, intersection, both differences.
[168] Fix | Delete
# Each has an operator version (e.g. __or__, invoked with |) and a
[169] Fix | Delete
# method version (e.g. union).
[170] Fix | Delete
# Subtle: Each pair requires distinct code so that the outcome is
[171] Fix | Delete
# correct when the type of other isn't suitable. For example, if
[172] Fix | Delete
# we did "union = __or__" instead, then Set().union(3) would return
[173] Fix | Delete
# NotImplemented instead of raising TypeError (albeit that *why* it
[174] Fix | Delete
# raises TypeError as-is is also a bit subtle).
[175] Fix | Delete
[176] Fix | Delete
def __or__(self, other):
[177] Fix | Delete
"""Return the union of two sets as a new set.
[178] Fix | Delete
[179] Fix | Delete
(I.e. all elements that are in either set.)
[180] Fix | Delete
"""
[181] Fix | Delete
if not isinstance(other, BaseSet):
[182] Fix | Delete
return NotImplemented
[183] Fix | Delete
return self.union(other)
[184] Fix | Delete
[185] Fix | Delete
def union(self, other):
[186] Fix | Delete
"""Return the union of two sets as a new set.
[187] Fix | Delete
[188] Fix | Delete
(I.e. all elements that are in either set.)
[189] Fix | Delete
"""
[190] Fix | Delete
result = self.__class__(self)
[191] Fix | Delete
result._update(other)
[192] Fix | Delete
return result
[193] Fix | Delete
[194] Fix | Delete
def __and__(self, other):
[195] Fix | Delete
"""Return the intersection of two sets as a new set.
[196] Fix | Delete
[197] Fix | Delete
(I.e. all elements that are in both sets.)
[198] Fix | Delete
"""
[199] Fix | Delete
if not isinstance(other, BaseSet):
[200] Fix | Delete
return NotImplemented
[201] Fix | Delete
return self.intersection(other)
[202] Fix | Delete
[203] Fix | Delete
def intersection(self, other):
[204] Fix | Delete
"""Return the intersection of two sets as a new set.
[205] Fix | Delete
[206] Fix | Delete
(I.e. all elements that are in both sets.)
[207] Fix | Delete
"""
[208] Fix | Delete
if not isinstance(other, BaseSet):
[209] Fix | Delete
other = Set(other)
[210] Fix | Delete
if len(self) <= len(other):
[211] Fix | Delete
little, big = self, other
[212] Fix | Delete
else:
[213] Fix | Delete
little, big = other, self
[214] Fix | Delete
common = ifilter(big._data.__contains__, little)
[215] Fix | Delete
return self.__class__(common)
[216] Fix | Delete
[217] Fix | Delete
def __xor__(self, other):
[218] Fix | Delete
"""Return the symmetric difference of two sets as a new set.
[219] Fix | Delete
[220] Fix | Delete
(I.e. all elements that are in exactly one of the sets.)
[221] Fix | Delete
"""
[222] Fix | Delete
if not isinstance(other, BaseSet):
[223] Fix | Delete
return NotImplemented
[224] Fix | Delete
return self.symmetric_difference(other)
[225] Fix | Delete
[226] Fix | Delete
def symmetric_difference(self, other):
[227] Fix | Delete
"""Return the symmetric difference of two sets as a new set.
[228] Fix | Delete
[229] Fix | Delete
(I.e. all elements that are in exactly one of the sets.)
[230] Fix | Delete
"""
[231] Fix | Delete
result = self.__class__()
[232] Fix | Delete
data = result._data
[233] Fix | Delete
value = True
[234] Fix | Delete
selfdata = self._data
[235] Fix | Delete
try:
[236] Fix | Delete
otherdata = other._data
[237] Fix | Delete
except AttributeError:
[238] Fix | Delete
otherdata = Set(other)._data
[239] Fix | Delete
for elt in ifilterfalse(otherdata.__contains__, selfdata):
[240] Fix | Delete
data[elt] = value
[241] Fix | Delete
for elt in ifilterfalse(selfdata.__contains__, otherdata):
[242] Fix | Delete
data[elt] = value
[243] Fix | Delete
return result
[244] Fix | Delete
[245] Fix | Delete
def __sub__(self, other):
[246] Fix | Delete
"""Return the difference of two sets as a new Set.
[247] Fix | Delete
[248] Fix | Delete
(I.e. all elements that are in this set and not in the other.)
[249] Fix | Delete
"""
[250] Fix | Delete
if not isinstance(other, BaseSet):
[251] Fix | Delete
return NotImplemented
[252] Fix | Delete
return self.difference(other)
[253] Fix | Delete
[254] Fix | Delete
def difference(self, other):
[255] Fix | Delete
"""Return the difference of two sets as a new Set.
[256] Fix | Delete
[257] Fix | Delete
(I.e. all elements that are in this set and not in the other.)
[258] Fix | Delete
"""
[259] Fix | Delete
result = self.__class__()
[260] Fix | Delete
data = result._data
[261] Fix | Delete
try:
[262] Fix | Delete
otherdata = other._data
[263] Fix | Delete
except AttributeError:
[264] Fix | Delete
otherdata = Set(other)._data
[265] Fix | Delete
value = True
[266] Fix | Delete
for elt in ifilterfalse(otherdata.__contains__, self):
[267] Fix | Delete
data[elt] = value
[268] Fix | Delete
return result
[269] Fix | Delete
[270] Fix | Delete
# Membership test
[271] Fix | Delete
[272] Fix | Delete
def __contains__(self, element):
[273] Fix | Delete
"""Report whether an element is a member of a set.
[274] Fix | Delete
[275] Fix | Delete
(Called in response to the expression `element in self'.)
[276] Fix | Delete
"""
[277] Fix | Delete
try:
[278] Fix | Delete
return element in self._data
[279] Fix | Delete
except TypeError:
[280] Fix | Delete
transform = getattr(element, "__as_temporarily_immutable__", None)
[281] Fix | Delete
if transform is None:
[282] Fix | Delete
raise # re-raise the TypeError exception we caught
[283] Fix | Delete
return transform() in self._data
[284] Fix | Delete
[285] Fix | Delete
# Subset and superset test
[286] Fix | Delete
[287] Fix | Delete
def issubset(self, other):
[288] Fix | Delete
"""Report whether another set contains this set."""
[289] Fix | Delete
self._binary_sanity_check(other)
[290] Fix | Delete
if len(self) > len(other): # Fast check for obvious cases
[291] Fix | Delete
return False
[292] Fix | Delete
for elt in ifilterfalse(other._data.__contains__, self):
[293] Fix | Delete
return False
[294] Fix | Delete
return True
[295] Fix | Delete
[296] Fix | Delete
def issuperset(self, other):
[297] Fix | Delete
"""Report whether this set contains another set."""
[298] Fix | Delete
self._binary_sanity_check(other)
[299] Fix | Delete
if len(self) < len(other): # Fast check for obvious cases
[300] Fix | Delete
return False
[301] Fix | Delete
for elt in ifilterfalse(self._data.__contains__, other):
[302] Fix | Delete
return False
[303] Fix | Delete
return True
[304] Fix | Delete
[305] Fix | Delete
# Inequality comparisons using the is-subset relation.
[306] Fix | Delete
__le__ = issubset
[307] Fix | Delete
__ge__ = issuperset
[308] Fix | Delete
[309] Fix | Delete
def __lt__(self, other):
[310] Fix | Delete
self._binary_sanity_check(other)
[311] Fix | Delete
return len(self) < len(other) and self.issubset(other)
[312] Fix | Delete
[313] Fix | Delete
def __gt__(self, other):
[314] Fix | Delete
self._binary_sanity_check(other)
[315] Fix | Delete
return len(self) > len(other) and self.issuperset(other)
[316] Fix | Delete
[317] Fix | Delete
# We inherit object.__hash__, so we must deny this explicitly
[318] Fix | Delete
__hash__ = None
[319] Fix | Delete
[320] Fix | Delete
# Assorted helpers
[321] Fix | Delete
[322] Fix | Delete
def _binary_sanity_check(self, other):
[323] Fix | Delete
# Check that the other argument to a binary operation is also
[324] Fix | Delete
# a set, raising a TypeError otherwise.
[325] Fix | Delete
if not isinstance(other, BaseSet):
[326] Fix | Delete
raise TypeError, "Binary operation only permitted between sets"
[327] Fix | Delete
[328] Fix | Delete
def _compute_hash(self):
[329] Fix | Delete
# Calculate hash code for a set by xor'ing the hash codes of
[330] Fix | Delete
# the elements. This ensures that the hash code does not depend
[331] Fix | Delete
# on the order in which elements are added to the set. This is
[332] Fix | Delete
# not called __hash__ because a BaseSet should not be hashable;
[333] Fix | Delete
# only an ImmutableSet is hashable.
[334] Fix | Delete
result = 0
[335] Fix | Delete
for elt in self:
[336] Fix | Delete
result ^= hash(elt)
[337] Fix | Delete
return result
[338] Fix | Delete
[339] Fix | Delete
def _update(self, iterable):
[340] Fix | Delete
# The main loop for update() and the subclass __init__() methods.
[341] Fix | Delete
data = self._data
[342] Fix | Delete
[343] Fix | Delete
# Use the fast update() method when a dictionary is available.
[344] Fix | Delete
if isinstance(iterable, BaseSet):
[345] Fix | Delete
data.update(iterable._data)
[346] Fix | Delete
return
[347] Fix | Delete
[348] Fix | Delete
value = True
[349] Fix | Delete
[350] Fix | Delete
if type(iterable) in (list, tuple, xrange):
[351] Fix | Delete
# Optimized: we know that __iter__() and next() can't
[352] Fix | Delete
# raise TypeError, so we can move 'try:' out of the loop.
[353] Fix | Delete
it = iter(iterable)
[354] Fix | Delete
while True:
[355] Fix | Delete
try:
[356] Fix | Delete
for element in it:
[357] Fix | Delete
data[element] = value
[358] Fix | Delete
return
[359] Fix | Delete
except TypeError:
[360] Fix | Delete
transform = getattr(element, "__as_immutable__", None)
[361] Fix | Delete
if transform is None:
[362] Fix | Delete
raise # re-raise the TypeError exception we caught
[363] Fix | Delete
data[transform()] = value
[364] Fix | Delete
else:
[365] Fix | Delete
# Safe: only catch TypeError where intended
[366] Fix | Delete
for element in iterable:
[367] Fix | Delete
try:
[368] Fix | Delete
data[element] = value
[369] Fix | Delete
except TypeError:
[370] Fix | Delete
transform = getattr(element, "__as_immutable__", None)
[371] Fix | Delete
if transform is None:
[372] Fix | Delete
raise # re-raise the TypeError exception we caught
[373] Fix | Delete
data[transform()] = value
[374] Fix | Delete
[375] Fix | Delete
[376] Fix | Delete
class ImmutableSet(BaseSet):
[377] Fix | Delete
"""Immutable set class."""
[378] Fix | Delete
[379] Fix | Delete
__slots__ = ['_hashcode']
[380] Fix | Delete
[381] Fix | Delete
# BaseSet + hashing
[382] Fix | Delete
[383] Fix | Delete
def __init__(self, iterable=None):
[384] Fix | Delete
"""Construct an immutable set from an optional iterable."""
[385] Fix | Delete
self._hashcode = None
[386] Fix | Delete
self._data = {}
[387] Fix | Delete
if iterable is not None:
[388] Fix | Delete
self._update(iterable)
[389] Fix | Delete
[390] Fix | Delete
def __hash__(self):
[391] Fix | Delete
if self._hashcode is None:
[392] Fix | Delete
self._hashcode = self._compute_hash()
[393] Fix | Delete
return self._hashcode
[394] Fix | Delete
[395] Fix | Delete
def __getstate__(self):
[396] Fix | Delete
return self._data, self._hashcode
[397] Fix | Delete
[398] Fix | Delete
def __setstate__(self, state):
[399] Fix | Delete
self._data, self._hashcode = state
[400] Fix | Delete
[401] Fix | Delete
class Set(BaseSet):
[402] Fix | Delete
""" Mutable set class."""
[403] Fix | Delete
[404] Fix | Delete
__slots__ = []
[405] Fix | Delete
[406] Fix | Delete
# BaseSet + operations requiring mutability; no hashing
[407] Fix | Delete
[408] Fix | Delete
def __init__(self, iterable=None):
[409] Fix | Delete
"""Construct a set from an optional iterable."""
[410] Fix | Delete
self._data = {}
[411] Fix | Delete
if iterable is not None:
[412] Fix | Delete
self._update(iterable)
[413] Fix | Delete
[414] Fix | Delete
def __getstate__(self):
[415] Fix | Delete
# getstate's results are ignored if it is not
[416] Fix | Delete
return self._data,
[417] Fix | Delete
[418] Fix | Delete
def __setstate__(self, data):
[419] Fix | Delete
self._data, = data
[420] Fix | Delete
[421] Fix | Delete
# In-place union, intersection, differences.
[422] Fix | Delete
# Subtle: The xyz_update() functions deliberately return None,
[423] Fix | Delete
# as do all mutating operations on built-in container types.
[424] Fix | Delete
# The __xyz__ spellings have to return self, though.
[425] Fix | Delete
[426] Fix | Delete
def __ior__(self, other):
[427] Fix | Delete
"""Update a set with the union of itself and another."""
[428] Fix | Delete
self._binary_sanity_check(other)
[429] Fix | Delete
self._data.update(other._data)
[430] Fix | Delete
return self
[431] Fix | Delete
[432] Fix | Delete
def union_update(self, other):
[433] Fix | Delete
"""Update a set with the union of itself and another."""
[434] Fix | Delete
self._update(other)
[435] Fix | Delete
[436] Fix | Delete
def __iand__(self, other):
[437] Fix | Delete
"""Update a set with the intersection of itself and another."""
[438] Fix | Delete
self._binary_sanity_check(other)
[439] Fix | Delete
self._data = (self & other)._data
[440] Fix | Delete
return self
[441] Fix | Delete
[442] Fix | Delete
def intersection_update(self, other):
[443] Fix | Delete
"""Update a set with the intersection of itself and another."""
[444] Fix | Delete
if isinstance(other, BaseSet):
[445] Fix | Delete
self &= other
[446] Fix | Delete
else:
[447] Fix | Delete
self._data = (self.intersection(other))._data
[448] Fix | Delete
[449] Fix | Delete
def __ixor__(self, other):
[450] Fix | Delete
"""Update a set with the symmetric difference of itself and another."""
[451] Fix | Delete
self._binary_sanity_check(other)
[452] Fix | Delete
self.symmetric_difference_update(other)
[453] Fix | Delete
return self
[454] Fix | Delete
[455] Fix | Delete
def symmetric_difference_update(self, other):
[456] Fix | Delete
"""Update a set with the symmetric difference of itself and another."""
[457] Fix | Delete
data = self._data
[458] Fix | Delete
value = True
[459] Fix | Delete
if not isinstance(other, BaseSet):
[460] Fix | Delete
other = Set(other)
[461] Fix | Delete
if self is other:
[462] Fix | Delete
self.clear()
[463] Fix | Delete
for elt in other:
[464] Fix | Delete
if elt in data:
[465] Fix | Delete
del data[elt]
[466] Fix | Delete
else:
[467] Fix | Delete
data[elt] = value
[468] Fix | Delete
[469] Fix | Delete
def __isub__(self, other):
[470] Fix | Delete
"""Remove all elements of another set from this set."""
[471] Fix | Delete
self._binary_sanity_check(other)
[472] Fix | Delete
self.difference_update(other)
[473] Fix | Delete
return self
[474] Fix | Delete
[475] Fix | Delete
def difference_update(self, other):
[476] Fix | Delete
"""Remove all elements of another set from this set."""
[477] Fix | Delete
data = self._data
[478] Fix | Delete
if not isinstance(other, BaseSet):
[479] Fix | Delete
other = Set(other)
[480] Fix | Delete
if self is other:
[481] Fix | Delete
self.clear()
[482] Fix | Delete
for elt in ifilter(data.__contains__, other):
[483] Fix | Delete
del data[elt]
[484] Fix | Delete
[485] Fix | Delete
# Python dict-like mass mutations: update, clear
[486] Fix | Delete
[487] Fix | Delete
def update(self, iterable):
[488] Fix | Delete
"""Add all values from an iterable (such as a list or file)."""
[489] Fix | Delete
self._update(iterable)
[490] Fix | Delete
[491] Fix | Delete
def clear(self):
[492] Fix | Delete
"""Remove all elements from this set."""
[493] Fix | Delete
self._data.clear()
[494] Fix | Delete
[495] Fix | Delete
# Single-element mutations: add, remove, discard
[496] Fix | Delete
[497] Fix | Delete
def add(self, element):
[498] Fix | Delete
"""Add an element to a set.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function