Skip to content

Commit dddaf8f

Browse files
committed
Fix for issue 77 (index and contains methods on list proxy objects).
--HG-- extra : convert_revision : svn%3A7a298fb0-333a-0410-83e7-658617cd9cf3/trunk%40153
1 parent da7e334 commit dddaf8f

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

ChangeLog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ http://couchdb-python.googlecode.com/svn/tags/0.6.0
1010
* Changed the `update()` method of the `client.Database` class to simplify
1111
the handling of errors. The method now returns a list of `(success, docid,
1212
rev_or_exc)` tuples. See the docstring of that method for the details.
13+
* `schema.ListField` proxy objects now support the `__contains__()` and
14+
`index()` methods (issue 77).
1315

1416

1517
Version 0.5

couchdb/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,12 @@ def __getitem__(self, index):
683683
def __setitem__(self, index, value):
684684
self.list[index] = self.field._to_json(item)
685685

686+
def __contains__(self, value):
687+
for item in self.list:
688+
if self.field._to_python(item) == value:
689+
return True
690+
return False
691+
686692
def __iter__(self):
687693
for index in range(len(self)):
688694
yield self[index]
@@ -705,3 +711,9 @@ def append(self, *args, **kwargs):
705711
def extend(self, list):
706712
for item in list:
707713
self.append(item)
714+
715+
def index(self, value):
716+
for idx, item in enumerate(self.list):
717+
if self.field._to_python(item) == value:
718+
return idx
719+
raise ValueError('x not in list')

couchdb/tests/schema.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# This software is licensed as described in the file COPYING, which
77
# you should have received as part of this distribution.
88

9+
from decimal import Decimal
910
import doctest
1011
import os
1112
import unittest
@@ -91,6 +92,22 @@ class Post(schema.Document):
9192
self.assertEqual([{'content': 'Bla bla', 'author': 'myself'}],
9293
post.comments)
9394

95+
def test_proxy_contains(self):
96+
class Thing(schema.Document):
97+
numbers = schema.ListField(schema.DecimalField)
98+
thing = Thing(numbers=[Decimal('1.0'), Decimal('2.0')])
99+
assert isinstance(thing.numbers, schema.ListField.Proxy)
100+
assert '1.0' not in thing.numbers
101+
assert Decimal('1.0') in thing.numbers
102+
103+
def test_proxy_index(self):
104+
class Thing(schema.Document):
105+
numbers = schema.ListField(schema.DecimalField)
106+
thing = Thing(numbers=[Decimal('1.0'), Decimal('2.0')])
107+
assert isinstance(thing.numbers, schema.ListField.Proxy)
108+
self.assertEqual(0, thing.numbers.index(Decimal('1.0')))
109+
self.assertRaises(ValueError, thing.numbers.index, '3.0')
110+
94111

95112
def suite():
96113
suite = unittest.TestSuite()

0 commit comments

Comments
 (0)