/
select.py
79 lines (59 loc) · 1.79 KB
/
select.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
from typing import Any, Iterable, List
from paramtools.tree import Tree
from paramtools.typing import ValueObject, CmpFunc
def select(
value_objects: List[ValueObject],
exact_match: bool,
cmp_func: CmpFunc,
labels: dict,
tree: Tree = None,
) -> List[ValueObject]:
"""
Query a parameter along some labels. If exact_match is True,
all values in `labels` must be equal to the corresponding label
in the parameter's "value" dictionary.
Ignores state.
Returns: [{"value": val, "label0": ..., }]
"""
if not labels:
return value_objects
if tree is None:
tree = Tree(vos=value_objects, label_grid=None)
return tree.select(labels, cmp_func, exact_match)
def eq_func(x: Any, y: Iterable) -> bool:
return x in y
def ne_func(x: Any, y: Iterable) -> bool:
return x not in y
def gt_func(x: Any, y: Iterable) -> bool:
return all(x > item for item in y)
def gt_ix_func(cmp_list: list, x: Any, y: Iterable) -> bool:
x_val = cmp_list.index(x)
return all(x_val > cmp_list.index(item) for item in y)
def select_eq(
value_objects: List[ValueObject],
exact_match: bool,
labels: dict,
tree: Tree = None,
) -> List[ValueObject]:
return select(value_objects, exact_match, eq_func, labels, tree)
def select_gt(
value_objects: List[ValueObject],
exact_match: bool,
labels: dict,
tree: Tree = None,
) -> List[ValueObject]:
return select(value_objects, exact_match, gt_func, labels, tree)
def select_gt_ix(
value_objects: List[ValueObject],
exact_match: bool,
labels: dict,
cmp_list: List,
tree: Tree = None,
) -> List[ValueObject]:
return select(
value_objects,
exact_match,
lambda x, y: gt_ix_func(cmp_list, x, y),
labels,
tree,
)