-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
dict.py
145 lines (118 loc) · 5.42 KB
/
dict.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import typing as t
from lightning_app.utilities.app_helpers import _LightningAppRef, _set_child_name
T = t.TypeVar("T")
if t.TYPE_CHECKING:
from lightning_app.utilities.types import Component
def _prepare_name(component: "Component") -> str:
return str(component.name.split(".")[-1])
# TODO: add support and tests for dict operations (insertion, update, etc.)
class Dict(t.Dict[str, T]):
def __init__(self, **kwargs: T):
"""The Dict Object is used to represents dict collection of
:class:`~lightning_app.core.work.LightningWork`
or :class:`~lightning_app.core.flow.LightningFlow`.
.. doctest::
>>> from lightning_app import LightningFlow, LightningWork
>>> from lightning_app.structures import Dict
>>> class CounterWork(LightningWork):
... def __init__(self):
... super().__init__()
... self.counter = 0
... def run(self):
... self.counter += 1
...
>>> class RootFlow(LightningFlow):
... def __init__(self):
... super().__init__()
... self.dict = Dict(**{"work_0": CounterWork(), "work_1": CounterWork()})
... def run(self):
... for work_name, work in self.dict.items():
... work.run()
...
>>> flow = RootFlow()
>>> flow.run()
>>> assert flow.dict["work_0"].counter == 1
Arguments:
items: A sequence of LightningWork or LightningFlow.
"""
super().__init__(**kwargs)
from lightning_app.runners.backends import Backend
self._name: t.Optional[str] = ""
self._backend: t.Optional[Backend] = None
for k, v in kwargs.items():
if "." in k:
raise Exception(f"The provided name {k} contains . which is forbidden.")
_set_child_name(self, v, k)
def __setitem__(self, k, v):
from lightning_app import LightningFlow, LightningWork
if not isinstance(k, str):
raise Exception("The provided key should be an string")
if isinstance(k, str) and "." in k:
raise Exception(f"The provided name {k} contains . which is forbidden.")
_set_child_name(self, v, k)
if self._backend:
if isinstance(v, LightningFlow):
LightningFlow._attach_backend(v, self._backend)
elif isinstance(v, LightningWork):
self._backend._wrap_run_method(_LightningAppRef().get_current(), v)
v._name = f"{self.name}.{k}"
super().__setitem__(k, v)
@property
def works(self):
from lightning_app import LightningFlow, LightningWork
works = [item for item in self.values() if isinstance(item, LightningWork)]
for flow in [item for item in self.values() if isinstance(item, LightningFlow)]:
for child_work in flow.works(recurse=False):
works.append(child_work)
return works
@property
def flows(self):
from lightning_app import LightningFlow
from lightning_app.structures import Dict, List
flows = {}
for item in self.values():
if isinstance(item, LightningFlow):
flows[item.name] = item
for child_flow in item.flows.values():
flows[child_flow.name] = child_flow
if isinstance(item, (Dict, List)):
for child_flow in item.flows.values():
flows[child_flow.name] = child_flow
return flows
@property
def name(self):
return self._name or "root"
@property
def state(self):
"""Returns the state of its flows and works."""
from lightning_app import LightningFlow, LightningWork
return {
"works": {key: item.state for key, item in self.items() if isinstance(item, LightningWork)},
"flows": {key: item.state for key, item in self.items() if isinstance(item, LightningFlow)},
}
@property
def state_vars(self):
from lightning_app import LightningFlow, LightningWork
return {
"works": {key: item.state_vars for key, item in self.items() if isinstance(item, LightningWork)},
"flows": {key: item.state_vars for key, item in self.items() if isinstance(item, LightningFlow)},
}
@property
def state_with_changes(self):
from lightning_app import LightningFlow, LightningWork
return {
"works": {key: item.state_with_changes for key, item in self.items() if isinstance(item, LightningWork)},
"flows": {key: item.state_with_changes for key, item in self.items() if isinstance(item, LightningFlow)},
}
def set_state(self, state):
state_keys = set(list(state["works"].keys()) + list(state["flows"].keys()))
current_state_keys = set(self.keys())
if current_state_keys != state_keys:
key_diff = (current_state_keys - state_keys) | (state_keys - current_state_keys)
raise Exception(
f"The provided state doesn't match the `Dict` {self.name}. Found `{key_diff}` un-matching keys"
)
for work_key, work_state in state["works"].items():
self[work_key].set_state(work_state)
for child_key, child_state in state["flows"].items():
self[child_key].set_state(child_state)