Fix some set algebra methods of WeakSet objects.

This commit is contained in:
Antoine Pitrou 2012-03-04 20:47:05 +01:00
parent de89d4b097
commit 9c47ac05d1
2 changed files with 30 additions and 33 deletions

View file

@ -121,26 +121,14 @@ class WeakSet:
self.update(other)
return self
# Helper functions for simple delegating methods.
def _apply(self, other, method):
if not isinstance(other, self.__class__):
other = self.__class__(other)
newdata = method(other.data)
newset = self.__class__()
newset.data = newdata
return newset
def difference(self, other):
return self._apply(other, self.data.difference)
newset = self.copy()
newset.difference_update(other)
return newset
__sub__ = difference
def difference_update(self, other):
if self._pending_removals:
self._commit_removals()
if self is other:
self.data.clear()
else:
self.data.difference_update(ref(item) for item in other)
self.__isub__(other)
def __isub__(self, other):
if self._pending_removals:
self._commit_removals()
@ -151,13 +139,11 @@ class WeakSet:
return self
def intersection(self, other):
return self._apply(other, self.data.intersection)
return self.__class__(item for item in other if item in self)
__and__ = intersection
def intersection_update(self, other):
if self._pending_removals:
self._commit_removals()
self.data.intersection_update(ref(item) for item in other)
self.__iand__(other)
def __iand__(self, other):
if self._pending_removals:
self._commit_removals()
@ -184,27 +170,24 @@ class WeakSet:
return self.data == set(ref(item) for item in other)
def symmetric_difference(self, other):
return self._apply(other, self.data.symmetric_difference)
newset = self.copy()
newset.symmetric_difference_update(other)
return newset
__xor__ = symmetric_difference
def symmetric_difference_update(self, other):
if self._pending_removals:
self._commit_removals()
if self is other:
self.data.clear()
else:
self.data.symmetric_difference_update(ref(item) for item in other)
self.__ixor__(other)
def __ixor__(self, other):
if self._pending_removals:
self._commit_removals()
if self is other:
self.data.clear()
else:
self.data.symmetric_difference_update(ref(item) for item in other)
self.data.symmetric_difference_update(ref(item, self._remove) for item in other)
return self
def union(self, other):
return self._apply(other, self.data.union)
return self.__class__(e for s in (self, other) for e in s)
__or__ = union
def isdisjoint(self, other):