The LabelDiver supports templating use cases for any Labelled object, by
presenting the labels as a graph of objects that may be accessed by attribute. For
example:
diver = LabelDiver({
'hello.world': 'foo',
'hello.world.again': 'bar'
})
assert str(diver.hello.world) == 'foo'
assert str(diver.hello.world.again) == 'bar'
Source code in src/prefect/server/events/schemas/labelling.py
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 | class LabelDiver:
"""The LabelDiver supports templating use cases for any Labelled object, by
presenting the labels as a graph of objects that may be accessed by attribute. For
example:
diver = LabelDiver({
'hello.world': 'foo',
'hello.world.again': 'bar'
})
assert str(diver.hello.world) == 'foo'
assert str(diver.hello.world.again) == 'bar'
"""
_value: str
_divers: Dict[str, "LabelDiver"]
_labels: Dict[str, str]
def __init__(self, labels: Dict[str, str], value: str = ""):
self._labels = labels.copy()
self._value = value
divers: Dict[str, Dict[str, str]] = {}
values: Dict[str, str] = {}
for key, value in labels.items():
head, _, tail = key.partition(".")
if tail:
if head not in divers:
divers[head] = {}
divers[head][tail] = labels[key]
else:
values[head] = value
# start with keys that had sub-divers...
self._divers: Dict[str, LabelDiver] = {
k: LabelDiver(v, value=values.pop(k, "")) for k, v in divers.items()
}
# ...then mix in any remaining keys that _only_ had values
self._divers.update(**{k: LabelDiver({}, value=v) for k, v in values.items()})
def __str__(self) -> str:
return self._value or ""
def __repr__(self) -> str:
return f"LabelDiver(divers={self._divers!r}, value={self._value!r})"
def __len__(self) -> int:
return len(self._labels)
def __iter__(self) -> Iterator[Tuple[str, str]]:
return iter(self._labels.items())
def __getitem__(self, key: str) -> str:
return self._labels[key]
def __getattr__(self, name: str) -> "LabelDiver":
if name.startswith("_"):
raise AttributeError
try:
return self._divers[name]
except KeyError:
raise AttributeError
|