Skip to content

Commit 811e154

Browse files
committed
explicit implementation of __reversed__
1 parent cbc0087 commit 811e154

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class BlockIndex:
4545
def copy(self,) -> 'BlockIndex': ...
4646
def __len__(self,) -> int: ...
4747
def __iter__(self,) -> tp.Iterator[tp.Tuple[int, int]]: ...
48+
def __reversed__(self,) -> tp.Iterator[tp.Tuple[int, int]]: ...
4849
def __getitem__(self, __key: int) -> tp.Tuple[int, int]: ...
4950
def __getstate__(self,) -> tp.Tuple[int, int, int, int, bytes]: ...
5051
def __setstate__(self, state: tp.Tuple[int, int, int, int, bytes]) -> None: ...

src/_arraykit.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5564,7 +5564,12 @@ BlockIndex_get_column(BlockIndexObject *self, PyObject *key){
55645564

55655565
static PyObject*
55665566
BlockIndex_iter(BlockIndexObject* self) {
5567-
return BIIter_new(self, 0);
5567+
return BIIter_new(self, false);
5568+
}
5569+
5570+
static PyObject*
5571+
BlockIndex_reversed(BlockIndexObject* self) {
5572+
return BIIter_new(self, true);
55685573
}
55695574

55705575
// Given key, return an iterator of a selection.
@@ -5626,6 +5631,7 @@ static PyMethodDef BlockIndex_methods[] = {
56265631
{"__getstate__", (PyCFunction) BlockIndex_getstate, METH_NOARGS, NULL},
56275632
{"__setstate__", (PyCFunction) BlockIndex_setstate, METH_O, NULL},
56285633
{"__sizeof__", (PyCFunction) BlockIndex_sizeof, METH_NOARGS, NULL},
5634+
{"__reversed__", (PyCFunction) BlockIndex_reversed, METH_NOARGS, NULL},
56295635
{"to_list", (PyCFunction)BlockIndex_to_list, METH_NOARGS, NULL},
56305636
{"to_bytes", (PyCFunction)BlockIndex_to_bytes, METH_NOARGS, NULL},
56315637
{"copy", (PyCFunction)BlockIndex_copy, METH_NOARGS, NULL},

test/test_block_index.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def test_block_index_get_column_a(self) -> None:
327327

328328

329329
#---------------------------------------------------------------------------
330-
def test_block_index_iter_a(self) -> None:
330+
def test_block_index_iter_a1(self) -> None:
331331
bi1 = BlockIndex()
332332
bi1.register(np.arange(2))
333333
bi1.register(np.arange(6).reshape(2,3))
@@ -339,9 +339,7 @@ def test_block_index_iter_a(self) -> None:
339339
self.assertEqual(list(bi1), [(0, 0), (1, 0), (1, 1), (1, 2), (2, 0)])
340340
self.assertEqual(list(reversed(bi1)), [(2, 0), (1, 2), (1, 1), (1, 0), (0, 0)])
341341

342-
343-
#---------------------------------------------------------------------------
344-
def test_block_index_iter_a(self) -> None:
342+
def test_block_index_iter_a2(self) -> None:
345343
bi1 = BlockIndex()
346344
bi1.register(np.arange(4).reshape(2,2))
347345
bi1.register(np.arange(2))

0 commit comments

Comments
 (0)