Skip to content

Commit

Permalink
use pipeline for updates (#74)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Nov 8, 2024
1 parent 04be910 commit c9fff3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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

0 comments on commit c9fff3b

Please sign in to comment.