From dec7801891b86dce6c5209d143352ab3be4e027a Mon Sep 17 00:00:00 2001 From: Tamino Huxohl Date: Fri, 13 Dec 2024 13:30:03 +0100 Subject: [PATCH] implement set by kwargs in dict_state --- widget_state/dict_state.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/widget_state/dict_state.py b/widget_state/dict_state.py index 855bf8f..19bebdb 100644 --- a/widget_state/dict_state.py +++ b/widget_state/dict_state.py @@ -53,9 +53,13 @@ def values(self) -> list[Any]: """ return [attr.value for attr in self] - def set(self, *args: BasicState[Any] | Primitive) -> None: + def set( + self, + *args: BasicState[Any] | Primitive, + **kwargs: dict[str, BasicState[Any] | Primitive], + ) -> None: """ - Reassign all internal basic state values and only + Reassign internal basic state values and only trigger a notification afterwards. """ assert len(args) == len(self) @@ -63,3 +67,9 @@ def set(self, *args: BasicState[Any] | Primitive) -> None: with self: for i, arg in enumerate(args): self[i].value = arg.value if isinstance(arg, BasicState) else arg + + _dict = self.dict() + for name, kwarg in kwargs.items(): + attr = _dict[name] + assert isinstance(attr, BasicState) + attr.value = kwarg.value if isinstance(kwarg, BasicState) else kwarg