Skip to content

Commit

Permalink
fix list.pop by using Pipelines (#69)
Browse files Browse the repository at this point in the history
* fix `list.pop` by using Pipelines

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix test

* update test to reflect only positiv integers

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Nov 7, 2024
1 parent 24884d6 commit e53502a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def test_list_refresh_delitem(client, request, znsclient):
lst.pop()
znsclient.sio.sleep(0.01)
assert len(lst) == 2
mock.assert_called_once_with({"start": -1, "stop": None})
mock.assert_called_once_with({"start": 2, "stop": None})

# pop again
del lst[0]
Expand Down
22 changes: 22 additions & 0 deletions znsocket/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,28 @@ def extend(self, values: t.Iterable) -> None:
refresh_data: RefreshDataTypeDict = {"target": self.key, "data": refresh}
self.socket.sio.emit(f"refresh", refresh_data, namespace="/znsocket")

def pop(self, index: int = -1) -> t.Any:
"""Pop an item from the list."""
if index < 0:
index = len(self) + index

value = self.redis.lindex(self.key, index)
if value is None:
raise IndexError("pop index out of range")

pipeline = self.redis.pipeline(**self._pipeline_kwargs)
pipeline.lset(self.key, index, "__DELETED__")
pipeline.lrem(self.key, 0, "__DELETED__")
try:
pipeline.execute()
except redis.exceptions.ResponseError:
raise IndexError("pop index out of range")
if self.socket is not None:
refresh: RefreshTypeDict = {"start": index, "stop": None}
refresh_data: RefreshDataTypeDict = {"target": self.key, "data": refresh}
self.socket.sio.emit(f"refresh", refresh_data, namespace="/znsocket")
return _decode(self, value)

def copy(self, key: str) -> "List":
"""Copy the list to a new key.
Expand Down

0 comments on commit e53502a

Please sign in to comment.