-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparameter_registry.py
More file actions
33 lines (25 loc) · 1.04 KB
/
parameter_registry.py
File metadata and controls
33 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ParameterRegistry:
"""A minimal registry to route parameter updates through a single apply path.
Each key is registered with two callables:
- read(): () -> value
- apply(value, *, transient: bool = False): apply value to Model + UI
"""
def __init__(self):
self._entries = {}
def register(self, key, read_fn, apply_fn):
if not callable(read_fn) or not callable(apply_fn):
raise ValueError("read_fn and apply_fn must be callable")
self._entries[key] = {
"read": read_fn,
"apply": apply_fn,
}
def has_key(self, key):
return key in self._entries
def read(self, key):
if key not in self._entries:
raise KeyError(f"Parameter key not registered: {key}")
return self._entries[key]["read"]()
def apply(self, key, value, *, transient=False):
if key not in self._entries:
raise KeyError(f"Parameter key not registered: {key}")
self._entries[key]["apply"](value, transient=transient)