Skip to content

Commit

Permalink
support or operation on dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Nov 8, 2024
1 parent ef269da commit 85da0c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,33 @@ def test_dct_pop(client, request):
dct.pop("a")

assert dct.pop("a", "default") == "default"


@pytest.mark.parametrize(
"client", ["znsclient", "znsclient_w_redis", "redisclient", "empty"]
)
def test_dct_merge(client, request):
c = request.getfixturevalue(client)
if c is not None:
dct = znsocket.Dict(r=c, key="list:test")
else:
dct = {}


dct.update({"a": "1", "b": "2"})

new_dct = dct | {"b": "3", "c": "4"}

assert new_dct == {"a": "1", "b": "3", "c": "4"}
assert isinstance(new_dct, dict)

assert dct == {"a": "1", "b": "2"}

if c is not None:
dct2 = znsocket.Dict(r=c, key="list:test2")

dct2.update({"b": "3", "c": "4"})
new_dct = dct | dct2
assert new_dct == {"a": "1", "b": "3", "c": "4"}
assert isinstance(new_dct, dict)

5 changes: 5 additions & 0 deletions znsocket/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,8 @@ def on_refresh(self, callback: t.Callable[[RefreshDataTypeDict], None]) -> None:
raise ValueError("No socket connection available")

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

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

0 comments on commit 85da0c4

Please sign in to comment.