Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pipeline for updates #74

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def test_dct_update(client, request):
dct.update({"b": "3", "c": "4"})
assert dct == {"a": "1", "b": "3", "c": "4"}

dct.update(d="5")
assert dct == {"a": "1", "b": "3", "c": "4", "d": "5"}


@pytest.mark.parametrize(
"client", ["znsclient", "znsclient_w_redis", "redisclient", "empty"]
Expand Down
28 changes: 28 additions & 0 deletions znsocket/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,34 @@ def on_refresh(self, callback: t.Callable[[RefreshDataTypeDict], None]) -> None:

self.socket.refresh_callbacks[self.key] = callback

def update(self, *args, **kwargs):
"""Update the dict with another dict or iterable."""
if len(args) > 1:
raise TypeError("update expected at most 1 argument, got %d" % len(args))
if args:
other = args[0]
if isinstance(other, Dict):
other = dict(other)
elif isinstance(other, MutableMapping):
pass
else:
raise TypeError(
"update expected at most 1 argument, got %d" % len(args)
)
else:
other = kwargs

pipeline = self.redis.pipeline()
for key, value in other.items():
if isinstance(value, Dict):
if value.key == self.key:
raise ValueError("Can not set circular reference to self")
value = f"znsocket.Dict:{value.key}"
if isinstance(value, List):
value = f"znsocket.List:{value.key}"
pipeline.hset(self.key, key, _encode(self, value))
pipeline.execute()

def __or__(self, value: "dict|Dict") -> dict:
if isinstance(value, Dict):
value = dict(value)
Expand Down
Loading