Skip to content

Commit

Permalink
Support reverse iteration of DataStore (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
MtkN1 committed Jan 5, 2022
1 parent 2eb1117 commit 064fa92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pybotters/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[Item]:
return iter(self._data.values())

def __reversed__(self) -> Iterator[Item]:
return reversed(self._data.values())

@staticmethod
def _hash(item: dict[str, Hashable]) -> int:
return hash(tuple(item.items()))
Expand Down
18 changes: 18 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys
import uuid

import pytest
Expand Down Expand Up @@ -285,6 +286,23 @@ def test__iter__():
next(data_iter)


def test__reversed__():
data = [{'foo': f'bar{i}'} for i in range(5)]
ds = pybotters.store.DataStore(keys=['foo'], data=data)
if sys.version_info.major == 3 and sys.version_info.minor >= 8:
data_iter = reversed(ds)
assert next(data_iter) == {'foo': 'bar4'}
assert next(data_iter) == {'foo': 'bar3'}
assert next(data_iter) == {'foo': 'bar2'}
assert next(data_iter) == {'foo': 'bar1'}
assert next(data_iter) == {'foo': 'bar0'}
with pytest.raises(StopIteration):
next(data_iter)
else:
with pytest.raises(TypeError):
data_iter = reversed(ds)


def test_set():
ds = pybotters.store.DataStore()
event = asyncio.Event()
Expand Down

0 comments on commit 064fa92

Please sign in to comment.