remove blist sortedset as soft dependency

PYTHON-385
This commit is contained in:
Adam Holmberg
2015-10-12 11:09:37 -05:00
parent c0087da8f8
commit 827f6a2c79
2 changed files with 187 additions and 160 deletions

View File

@@ -485,24 +485,18 @@ class WeakSet(object):
def isdisjoint(self, other):
return len(self.intersection(other)) == 0
try:
from blist import sortedset
except ImportError:
from bisect import bisect_left
class sortedset(object):
class SortedSet(object):
'''
A sorted set based on sorted list
This set is used in place of blist.sortedset in Python environments
where blist module/extension is not available.
A sorted set implementation is used in this case because it does not
require its elements to be immutable/hashable.
#Not implemented: update functions, inplace operators
'''
def __init__(self, iterable=()):
@@ -533,19 +527,19 @@ except ImportError:
if isinstance(other, self.__class__):
return self._items == other._items
else:
if not isinstance(other, set):
return False
return len(other) == len(self._items) and all(item in other for item in self._items)
try:
return len(other) == len(self._items) and all(item in self for item in other)
except TypeError:
return NotImplemented
def __ne__(self, other):
if isinstance(other, self.__class__):
return self._items != other._items
else:
if not isinstance(other, set):
return True
return len(other) != len(self._items) or any(item not in other for item in self._items)
try:
return len(other) != len(self._items) or any(item not in self for item in other)
except TypeError:
return NotImplemented
def __le__(self, other):
return self.issubset(other)
@@ -561,20 +555,52 @@ except ImportError:
def __and__(self, other):
return self._intersect(other)
__rand__ = __and__
def __iand__(self, other):
isect = self._intersect(other)
self._items = isect._items
return self
def __or__(self, other):
return self.union(other)
__ror__ = __or__
def __ior__(self, other):
union = self.union(other)
self._items = union._items
return self
def __sub__(self, other):
return self._diff(other)
def __rsub__(self, other):
return sortedset(other) - self
def __isub__(self, other):
diff = self._diff(other)
self._items = diff._items
return self
def __xor__(self, other):
return self.symmetric_difference(other)
__rxor__ = __xor__
def __ixor__(self, other):
sym_diff = self.symmetric_difference(other)
self._items = sym_diff._items
return self
def __contains__(self, item):
i = bisect_left(self._items, item)
return i < len(self._items) and self._items[i] == item
def __delitem__(self, i):
del self._items[i]
def __delslice__(self, i, j):
del self._items[i:j]
def add(self, item):
i = bisect_left(self._items, item)
if i < len(self._items):
@@ -690,6 +716,8 @@ except ImportError:
isect.add(item)
return isect
sortedset = SortedSet # backwards-compatibility
from collections import Mapping
from six.moves import cPickle

View File

@@ -9,7 +9,6 @@ deps = nose
[testenv]
deps = {[base]deps}
py{26,27,33,34}: blist
cython
py26: unittest2
py{26,27}: gevent