-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
list.py
164 lines (133 loc) · 5.99 KB
/
list.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import typing as t
from pyparsing import Optional
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 list operations (concatenation, deletion, insertion, etc.)
class List(t.List[T]):
def __init__(self, *items: T):
"""The List Object is used to represents list 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 List
>>> 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.list = List(*[CounterWork(), CounterWork()])
... def run(self):
... for work in self.list:
... work.run()
...
>>> flow = RootFlow()
>>> flow.run()
>>> assert flow.list[0].counter == 1
Arguments:
items: A sequence of LightningWork or LightningFlow.
"""
super().__init__()
from lightning_app.runners.backends import Backend
self._name: t.Optional[str] = ""
self._last_index = 0
self._backend: Optional[Backend] = None
for item in items:
self.append(item)
def append(self, v):
from lightning_app import LightningFlow, LightningWork
_set_child_name(self, v, str(self._last_index))
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}.{self._last_index}"
self._last_index += 1
super().append(v)
@property
def name(self):
"""Returns the name of this List object."""
return self._name or "root"
@property
def works(self):
from lightning_app import LightningFlow, LightningWork
works = [item for item in self if isinstance(item, LightningWork)]
for flow in [item for item in self 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:
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 state(self):
"""Returns the state of its flows and works."""
from lightning_app import LightningFlow, LightningWork
works = [item for item in self if isinstance(item, LightningWork)]
children = [item for item in self if isinstance(item, LightningFlow)]
return {
"works": {_prepare_name(w): w.state for w in works},
"flows": {_prepare_name(flow): flow.state for flow in children},
}
@property
def state_vars(self):
from lightning_app import LightningFlow, LightningWork
works = [item for item in self if isinstance(item, LightningWork)]
children = [item for item in self if isinstance(item, LightningFlow)]
return {
"works": {_prepare_name(w): w.state_vars for w in works},
"flows": {_prepare_name(flow): flow.state_vars for flow in children},
}
@property
def state_with_changes(self):
from lightning_app import LightningFlow, LightningWork
works = [item for item in self if isinstance(item, LightningWork)]
children = [item for item in self if isinstance(item, LightningFlow)]
return {
"works": {str(_prepare_name(w)): w.state_with_changes for w in works},
"flows": {_prepare_name(flow): flow.state_with_changes for flow in children},
}
def set_state(self, state):
"""Method to set the state of the list and its children."""
from lightning_app import LightningFlow, LightningWork
works = [item for item in self if isinstance(item, LightningWork)]
children = [item for item in self if isinstance(item, LightningFlow)]
current_state_keys = {_prepare_name(w) for w in self}
state_keys = set(list(state["works"].keys()) + list(state["flows"].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 `List` {self.name}. Found `{key_diff}` un-matching keys"
)
for work_key, work_state in state["works"].items():
for work in works:
if _prepare_name(work) == work_key:
work.set_state(work_state)
for child_key, child_state in state["flows"].items():
for child in children:
if _prepare_name(child) == child_key:
child.set_state(child_state)
def __len__(self):
"""Returns the number of elements within this List."""
return len([v for v in self])