Rev 2255: implement __setitem__ and __delitem__ to make it act a lot like a list in http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/list_patch
John Arbash Meinel
john at arbash-meinel.com
Tue Jan 30 18:34:03 GMT 2007
At http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/list_patch
------------------------------------------------------------
revno: 2255
revision-id: john at arbash-meinel.com-20070130183358-hom3lwhr0sew9so0
parent: john at arbash-meinel.com-20070130175551-v30z9f40xpvx2m4v
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: list_patch
timestamp: Tue 2007-01-30 12:33:58 -0600
message:
implement __setitem__ and __delitem__ to make it act a lot like a list
modified:
bzrlib/hybrid_linked_list.py hybrid_linked_list.p-20070130001028-zlexwz74tzo56o2t-1
bzrlib/tests/test_hybrid_linked_list.py test_hybrid_linked_l-20070130001028-zlexwz74tzo56o2t-2
-------------- next part --------------
=== modified file 'bzrlib/hybrid_linked_list.py'
--- a/bzrlib/hybrid_linked_list.py 2007-01-30 17:55:51 +0000
+++ b/bzrlib/hybrid_linked_list.py 2007-01-30 18:33:58 +0000
@@ -115,6 +115,28 @@
start, end, content) = self._find_nodes_for_pos(pos)
return content[start+offset]
+ def __setitem__(self, pos, value):
+ if isinstance(pos, slice):
+ cur, stop, stride = pos.indices(len(self))
+ if stride != 1:
+ raise TypeError('Stride is not supported when setting items.')
+ self.replace(cur, stop, value)
+ else:
+ if pos < 0:
+ pos = len(self) + pos
+ self.replace(pos, pos+1, [value])
+
+ def __delitem__(self, pos):
+ if isinstance(pos, slice):
+ cur, stop, stride = pos.indices(len(self))
+ if stride != 1:
+ raise TypeError('Stride is not supported when deleting items.')
+ self.delete(cur, stop)
+ else:
+ if pos < 0:
+ pos = len(self) + pos
+ self.delete(pos, pos+1)
+
def flatten(self):
"""Flatten the linked structure into a single list."""
self._reset(list(self))
=== modified file 'bzrlib/tests/test_hybrid_linked_list.py'
--- a/bzrlib/tests/test_hybrid_linked_list.py 2007-01-30 17:55:51 +0000
+++ b/bzrlib/tests/test_hybrid_linked_list.py 2007-01-30 18:33:58 +0000
@@ -51,6 +51,21 @@
self.assertEqual((2, None, 1, None, None, None),
hll._find_nodes_for_pos(7))
+ def test__delitem__(self):
+ hll = hybrid_linked_list.HybridLinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8])
+ del hll[2]
+ self.assertEqual([0, 1, 3, 4, 5, 6, 7, 8], list(hll))
+
+ del hll[5:7]
+ self.assertEqual([0, 1, 3, 4, 5, 8], list(hll))
+
+ del hll[-1]
+ self.assertEqual([0, 1, 3, 4, 5], list(hll))
+
+ # Do nothing when the range == same
+ del hll[-1:-1]
+ self.assertEqual([0, 1, 3, 4, 5], list(hll))
+
def test__getitem__(self):
hll = hybrid_linked_list.HybridLinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8])
hll.delete(2, 3)
@@ -70,6 +85,32 @@
self.assertEqual([], hll[20:])
self.assertEqual([], hll[20:21])
+ def test__setitem__(self):
+ hll = hybrid_linked_list.HybridLinkedList([0, 1, 2, 3, 4, 5, 6, 7, 8])
+ hll.delete(2, 3)
+ hll.delete(5, 7)
+ self.assertEqual([0, 1, 3, 4, 5, 8], list(hll))
+
+ # Set a single item
+ hll[1] = 9
+ self.assertEqual([0, 9, 3, 4, 5, 8], list(hll))
+
+ # Set a range of items
+ hll[1:3] = [10]
+ self.assertEqual([0, 10, 4, 5, 8], list(hll))
+
+ # Delete a range
+ hll[2:4] = []
+ self.assertEqual([0, 10, 8], list(hll))
+
+ # Set using negative values
+ hll[-1] = 11
+ self.assertEqual([0, 10, 11], list(hll))
+
+ # Set using negative values
+ hll[-2:-1] = [12, 13]
+ self.assertEqual([0, 12, 13, 11], list(hll))
+
def test_insert(self):
hll = hybrid_linked_list.HybridLinkedList([0, 1, 2, 3, 4])
hll.insert(3, [5, 6, 7])
More information about the bazaar-commits
mailing list