Skip to content

Commit

Permalink
Fix #13 - support slicing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlenzen committed Jan 20, 2015
1 parent 0ce375e commit f2bc1c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
43 changes: 26 additions & 17 deletions collections_extended/setlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,36 @@ class setlist(_basesetlist, MutableSequence, MutableSet):
## Implement MutableSequence
def __setitem__(self, index, value):
if isinstance(index, slice):
pass
index = self._fix_neg_index(index)
old_value = self._list[index]
if value in self:
if value == old_value:
return
old_values = self[index]
for v in value:
if v in self and v not in old_values:
raise ValueError
else:
raise ValueError
del self._dict[old_value]
self._list[index] = value
self._dict[value] = index
self._list[index] = value
self._dict = {v: i for i, v in enumerate(self._list)}
else:
index = self._fix_neg_index(index)
old_value = self._list[index]
if value in self:
if value == old_value:
return
else:
raise ValueError
del self._dict[old_value]
self._list[index] = value
self._dict[value] = index

def __delitem__(self, index):
if isinstance(index, slice):
pass
index = self._fix_neg_index(index)
value = self._list[index]
del self._dict[value]
for elem in self._list[index+1:]:
self._dict[elem] -= 1
del self._list[index]
values_to_remove = self._list[index]
self.remove_all(values_to_remove)
else:
index = self._fix_neg_index(index)
value = self._list[index]
del self._dict[value]
for elem in self._list[index+1:]:
self._dict[elem] -= 1
del self._list[index]

def insert(self, index, value):
if value in self:
Expand Down
29 changes: 15 additions & 14 deletions tests/test_setlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,22 @@ def test_setitem():
with pytest.raises(IndexError):
sl[10] = 'd'
assert sl == setlist(['a'] + list(range(1, 9)) + ['c'])
test_set_slice(slice(0, 2), ['a', 'b'])
test_set_slice(slice(2, 4), ['a', 'b'])
test_set_slice(slice(7, 9), ['a', 'b'])
test_set_slice(slice(2, -2), ['a', 'b'])
test_set_slice(slice(2, 7, 2), ['a', 'b'])
test_set_slice(slice(-1, 0, -1), ['a', 'b'])
test_set_slice(slice(-1, 0, -2), ['a', 'b'])
compare_set_slice_to_list(slice(0, 2), ['a', 'b'])
compare_set_slice_to_list(slice(2, 4), ['a', 'b'])
compare_set_slice_to_list(slice(7, 9), ['a', 'b'])
compare_set_slice_to_list(slice(2, -2), ['a', 'b'])
compare_set_slice_to_list(slice(2, 5, 2), ['a', 'b'])
compare_set_slice_to_list(slice(-1, None, -1), list(range(10)))
with pytest.raises(TypeError):
sl[0:2] = 1
sl = setlist(range(10))
with pytest.raises(ValueError):
sl[0:2] = [8, 9]
with pytest.raises(ValueError):
sl[-1:0:-2] = ['a', 'b']


def test_set_slice(slice_, replacement):
def compare_set_slice_to_list(slice_, replacement):
sl = setlist(range(10))
sl[slice_] = replacement
l = list(range(10))
Expand All @@ -182,14 +183,14 @@ def test_delitem():
assert sl == setlist(range(1, 8))
with pytest.raises(IndexError):
del sl[10]
test_del_slice(slice(0, 2))
test_del_slice(slice(6, 9))
test_del_slice(slice(3, 7))
test_del_slice(slice(7, 3, -1))
test_del_slice(slice(0, 7, 2))
compare_del_slice_to_list(slice(0, 2))
compare_del_slice_to_list(slice(6, 9))
compare_del_slice_to_list(slice(3, 7))
compare_del_slice_to_list(slice(7, 3, -1))
compare_del_slice_to_list(slice(0, 7, 2))


def test_del_slice(slice_):
def compare_del_slice_to_list(slice_):
sl = setlist(range(10))
del sl[slice_]
l = list(range(10))
Expand Down

0 comments on commit f2bc1c7

Please sign in to comment.