Observer pattern
还没有人认领这个 Issue。
评估
调研方向
从 switch.py 和 issue 中展示的 Widget/YDoc 实现开始,然后查看 psygnal 和 traitlets 如何表示被观察的属性。确定可行的 observer 设计是否涵盖 YMap 值条目和嵌套的 YDoc 结构。完成的标准是确定一致认可的实现范围,并明确观察这些结构的方法。
由索引模型根据 Issue 内容生成。
描述
In https://github.com/jupyter-widgets/ipywidgets/issues/3293#issuecomment-1433848240 @tlambert03 mentioned psygnal, that could be used to implement the equivalent of traitlets.
I think it is a good idea, but I'm not sure how it could fit here. For instance, take the Switch model, currently implemented as:
from .ypywidgets import Widget
class Switch(Widget):
def __init__(self, value: bool = False, open_comm: bool = True) -> None:
super().__init__(name="switch", open_comm=open_comm)
self.yvalue = self.ydoc.get_map("value")
self._set(value)
def _set(self, value: bool) -> None:
with self.ydoc.begin_transaction() as t:
self.yvalue.set(t, "value", value)
@property
def value(self) -> bool:
return self.yvalue["value"]
@value.setter
def value(self, value: bool):
if value == self.value:
return
self._set(value)
def toggle(self):
self.value = not self.value
Its YDoc consists of a YMap named value, with a single entry in it, also named value, which contains the boolean value of the switch. Here the observer pattern is manually implemented using a getter and a setter function for the value attribute.
If we were to use psygnal, we would need a way to point to the YDoc's structure (self.yvalue and value entry in it) from the psygnal's attribute.
I think that's the reason why traitlets use Python's descriptor protocol, which allows to get the name of an attribute at runtime.
Also, I'm not sure how we could connect nested YDoc structures (e.g. a YMap can contain other Y structures), but I don't think it was possible with traitlets anyway, so that would be an improvement.
- 主要语言
- Python
- 星标
- 16
- 派生
- 7
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
QuantStack/ypywidgets 的其他 Issue
-
难度 5/5 一周以上 新手友好度 15/100
QuantStack/ypywidgets#12 · 1 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
QuantStack/ypywidgets#2 ·
查看 QuantStack/ypywidgets 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
huggingface/peft#3955 ·
维护者通常 1 天内回复
-
author-page correction
难度 2/5 1-3 小时 新手友好度 60/100
acl-org/acl-anthology#10313 ·
维护者通常 1 天内回复
-
anime duplicate exception request-theme
难度 1/5 1 小时以内 新手友好度 82/100
LizardByte/ThemerrDB#8926 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
huggingface/transformers#49556 ·
维护者通常 1 天内回复
-
area/security bug
难度 2/5 1-3 小时 新手友好度 82/100
mozilla-ai/otari#2245 ·
维护者通常 1 天内回复