[py3k] __getslice__ → __getitem__ with slice support
authorRémi Cardona <remi.cardona@logilab.fr>
Wed, 16 Sep 2015 15:26:56 +0200
changeset 10691 af266f27c4d5
parent 10690 c6290d727c0c
child 10692 14e7ddb0f670
[py3k] __getslice__ → __getitem__ with slice support
rset.py
test/unittest_rset.py
utils.py
--- a/rset.py	Wed Sep 16 15:50:42 2015 +0200
+++ b/rset.py	Wed Sep 16 15:26:56 2015 +0200
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """The `ResultSet` class which is returned as result of an rql query"""
-
+from __future__ import print_function
 __docformat__ = "restructuredtext en"
 
 from warnings import warn
@@ -121,12 +121,9 @@
 
     def __getitem__(self, i):
         """returns the ith element of the result set"""
+        #print('__getitem__', i)
         return self.rows[i] #ResultSetRow(self.rows[i])
 
-    def __getslice__(self, i, j):
-        """returns slice [i:j] of the result set"""
-        return self.rows[i:j]
-
     def __iter__(self):
         """Returns an iterator over rows"""
         return iter(self.rows)
--- a/test/unittest_rset.py	Wed Sep 16 15:50:42 2015 +0200
+++ b/test/unittest_rset.py	Wed Sep 16 15:26:56 2015 +0200
@@ -566,6 +566,19 @@
             self.assertIsInstance(str(rset), string_types)
             self.assertEqual(len(str(rset).splitlines()), 1)
 
+    def test_slice(self):
+        rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'],
+                        [12000, 'adim', u'Jardiner facile'],
+                        [13000, 'syt',  u'Le carrelage en 42 leçons'],
+                        [14000, 'nico', u'La tarte tatin en 15 minutes'],
+                        [14000, 'nico', u"L'épluchage du castor commun"]],
+                       'Any U, L, T WHERE U is CWUser, U login L,'\
+                       'D created_by U, D title T',
+                       description=[['CWUser', 'String', 'String']] * 5)
+        self.assertEqual(rs[1::2],
+            [[12000, 'adim', u'Jardiner facile'],
+             [14000, 'nico', u'La tarte tatin en 15 minutes']])
+
     def test_nonregr_symmetric_relation(self):
         # see https://www.cubicweb.org/ticket/4739253
         with self.admin_access.client_cnx() as cnx:
--- a/utils.py	Wed Sep 16 15:50:42 2015 +0200
+++ b/utils.py	Wed Sep 16 15:26:56 2015 +0200
@@ -186,13 +186,13 @@
     def __iter__(self):
         return repeat(self._item, self._size)
     def __getitem__(self, index):
+        if isinstance(index, slice):
+            # XXX could be more efficient, but do we bother?
+            return ([self._item] * self._size)[index]
         return self._item
     def __delitem__(self, idc):
         assert self._size > 0
         self._size -= 1
-    def __getslice__(self, i, j):
-        # XXX could be more efficient, but do we bother?
-        return ([self._item] * self._size)[i:j]
     def __add__(self, other):
         if isinstance(other, RepeatList):
             if other._item == self._item: