Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit c9fff3b

Browse files
use pipeline for updates (#74)
* use pipeline for updates * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 04be910 commit c9fff3b

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

tests/test_dict.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def test_dct_update(client, request):
6767
dct.update({"b": "3", "c": "4"})
6868
assert dct == {"a": "1", "b": "3", "c": "4"}
6969

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

7174
@pytest.mark.parametrize(
7275
"client", ["znsclient", "znsclient_w_redis", "redisclient", "empty"]

znsocket/objects/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,34 @@ def on_refresh(self, callback: t.Callable[[RefreshDataTypeDict], None]) -> None:
479479

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

482+
def update(self, *args, **kwargs):
483+
"""Update the dict with another dict or iterable."""
484+
if len(args) > 1:
485+
raise TypeError("update expected at most 1 argument, got %d" % len(args))
486+
if args:
487+
other = args[0]
488+
if isinstance(other, Dict):
489+
other = dict(other)
490+
elif isinstance(other, MutableMapping):
491+
pass
492+
else:
493+
raise TypeError(
494+
"update expected at most 1 argument, got %d" % len(args)
495+
)
496+
else:
497+
other = kwargs
498+
499+
pipeline = self.redis.pipeline()
500+
for key, value in other.items():
501+
if isinstance(value, Dict):
502+
if value.key == self.key:
503+
raise ValueError("Can not set circular reference to self")
504+
value = f"znsocket.Dict:{value.key}"
505+
if isinstance(value, List):
506+
value = f"znsocket.List:{value.key}"
507+
pipeline.hset(self.key, key, _encode(self, value))
508+
pipeline.execute()
509+
482510
def __or__(self, value: "dict|Dict") -> dict:
483511
if isinstance(value, Dict):
484512
value = dict(value)

0 commit comments

Comments
 (0)