Skip to content

Commit c7c7bd1

Browse files
committed
Fix pop() and slice item operations for ListFields (issue 92).
Thanks to melkjug for the inital patch.
1 parent 05c6102 commit c7c7bd1

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

ChangeLog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ http://couchdb-python.googlecode.com/hg/?r=default
1414
instance of their `schema.Document` (issue 101).
1515
* `schema.ListField` proxy objects now have a more consistent (though somewhat
1616
slower) `count()` method (issue 91).
17+
* `schema.ListField` objects now have correct behavior for slicing operations
18+
and the `pop()` method (issue 92).
1719

1820

1921
Version 0.6

couchdb/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,15 @@ def __getitem__(self, index):
679679
def __setitem__(self, index, value):
680680
self.list[index] = self.field._to_json(value)
681681

682+
def __delslice__(self, i, j):
683+
del self.list[i:j]
684+
685+
def __getslice__(self, i, j):
686+
return ListField.Proxy(self.list[i:j], self.field)
687+
688+
def __setslice__(self, i, j, seq):
689+
self.list[i:j] = (self.field._to_json(v) for v in seq)
690+
682691
def __contains__(self, value):
683692
for item in self.list:
684693
if self.field._to_python(item) == value:
@@ -727,3 +736,6 @@ def insert(self, idx, *args, **kwargs):
727736

728737
def remove(self, value):
729738
return self.list.remove(self.field._to_json(value))
739+
740+
def pop(self, *args):
741+
return self.field._to_python(self.list.pop(*args))

couchdb/tests/schema.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,30 @@ class Post(schema.Document):
191191
post = Post.load(self.db, 'test')
192192
assert isinstance(post.comments[0], dict)
193193

194+
def test_proxy_pop(self):
195+
class Thing(schema.Document):
196+
numbers = schema.ListField(schema.DecimalField)
197+
thing = Thing()
198+
thing.numbers = [Decimal('%d' % i) for i in range(3)]
199+
self.assertEqual(thing.numbers.pop(), Decimal('2.0'))
200+
self.assertEqual(len(thing.numbers), 2)
201+
self.assertEqual(thing.numbers.pop(0), Decimal('0.0'))
202+
203+
def test_proxy_slices(self):
204+
class Thing(schema.Document):
205+
numbers = schema.ListField(schema.DecimalField)
206+
thing = Thing()
207+
thing.numbers = [Decimal('%d' % i) for i in range(5)]
208+
ll = thing.numbers[1:3]
209+
self.assertEqual(len(ll), 2)
210+
self.assertEqual(ll[0], Decimal('1.0'))
211+
thing.numbers[2:4] = [Decimal('%d' % i) for i in range(6, 8)]
212+
self.assertEqual(thing.numbers[2], Decimal('6.0'))
213+
self.assertEqual(thing.numbers[4], Decimal('4.0'))
214+
self.assertEqual(len(thing.numbers), 5)
215+
del thing.numbers[3:]
216+
self.assertEquals(len(thing.numbers), 3)
217+
194218

195219
def suite():
196220
suite = unittest.TestSuite()

0 commit comments

Comments
 (0)