hydraflow 0.14.4__py3-none-any.whl → 0.15.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hydraflow/core/param.py DELETED
@@ -1,165 +0,0 @@
1
- """Provide utility functions for parameter matching.
2
-
3
- The main function `match` checks if a given parameter matches a specified value.
4
- It supports various types of values including None, boolean, list, tuple, int,
5
- float, and str.
6
-
7
- Helper functions `_match_list` and `_match_tuple` are used internally to handle
8
- matching for list and tuple types respectively.
9
- """
10
-
11
- from __future__ import annotations
12
-
13
- from typing import TYPE_CHECKING, Any
14
-
15
- from omegaconf import ListConfig, OmegaConf
16
-
17
- if TYPE_CHECKING:
18
- from mlflow.entities import Run
19
-
20
-
21
- def match(param: str, value: Any) -> bool:
22
- """Check if the string matches the specified value.
23
-
24
- Args:
25
- param (str): The parameter to check.
26
- value (Any): The value to check.
27
-
28
- Returns:
29
- True if the parameter matches the specified value,
30
- False otherwise.
31
-
32
- """
33
- if callable(value):
34
- return value(param)
35
-
36
- if any(value is x for x in [None, True, False]):
37
- return param == str(value)
38
-
39
- if isinstance(value, list) and (m := _match_list(param, value)) is not None:
40
- return m
41
-
42
- if isinstance(value, tuple) and (m := _match_tuple(param, value)) is not None:
43
- return m
44
-
45
- if isinstance(value, int | float):
46
- return float(param) == value
47
-
48
- if isinstance(value, str):
49
- return param == value
50
-
51
- return param == str(value)
52
-
53
-
54
- def _match_list(param: str, value: list) -> bool | None:
55
- if not value:
56
- return None
57
-
58
- if any(param.startswith(x) for x in ["[", "(", "{"]):
59
- return None
60
-
61
- if isinstance(value[0], bool):
62
- return None
63
-
64
- if not isinstance(value[0], int | float | str):
65
- return None
66
-
67
- return type(value[0])(param) in value
68
-
69
-
70
- def _match_tuple(param: str, value: tuple) -> bool | None:
71
- if len(value) != 2:
72
- return None
73
-
74
- if any(param.startswith(x) for x in ["[", "(", "{"]):
75
- return None
76
-
77
- if isinstance(value[0], bool):
78
- return None
79
-
80
- if not isinstance(value[0], int | float | str):
81
- return None
82
-
83
- if type(value[0]) is not type(value[1]):
84
- return None
85
-
86
- return value[0] <= type(value[0])(param) <= value[1] # type: ignore
87
-
88
-
89
- def to_value(param: str | None, type_: type) -> Any:
90
- """Convert the parameter to the specified type.
91
-
92
- Args:
93
- param (str | None): The parameter to convert.
94
- type_ (type): The type to convert to.
95
-
96
- Returns:
97
- The converted value.
98
-
99
- """
100
- if param is None or param == "None":
101
- return None
102
-
103
- if type_ is int:
104
- return int(param)
105
-
106
- if type_ is float:
107
- return float(param)
108
-
109
- if type_ is bool:
110
- return param == "True"
111
-
112
- if type_ is list or type_ is ListConfig:
113
- return list(OmegaConf.create(param))
114
-
115
- return param
116
-
117
-
118
- def get_params(run: Run, *names: str | list[str]) -> tuple[str | None, ...]:
119
- """Retrieve the values of specified parameters from the given run.
120
-
121
- This function extracts the values of the parameters identified by the
122
- provided names from the specified run. It can accept both individual
123
- parameter names and lists of parameter names.
124
-
125
- Args:
126
- run (Run): The run object from which to extract parameter values.
127
- *names (str | list[str]): The names of the parameters to retrieve.
128
- This can be a single parameter name or multiple names provided
129
- as separate arguments or as a list.
130
-
131
- Returns:
132
- tuple[str | None, ...]: A tuple containing the values of the specified
133
- parameters in the order they were provided.
134
-
135
- """
136
- names_ = []
137
- for name in names:
138
- if isinstance(name, list):
139
- names_.extend(name)
140
- else:
141
- names_.append(name)
142
-
143
- params = run.data.params
144
- return tuple(params.get(name) for name in names_)
145
-
146
-
147
- def get_values(run: Run, names: list[str], types: list[type]) -> tuple[Any, ...]:
148
- """Retrieve the values of specified parameters from the given run.
149
-
150
- This function extracts the values of the parameters identified by the
151
- provided names from the specified run.
152
-
153
- Args:
154
- run (Run): The run object from which to extract parameter values.
155
- names (list[str]): The names of the parameters to retrieve.
156
- types (list[type]): The types to convert to.
157
-
158
- Returns:
159
- tuple[Any, ...]: A tuple containing the values of the specified
160
- parameters in the order they were provided.
161
-
162
- """
163
- params = get_params(run, names)
164
- it = zip(params, types, strict=True)
165
- return tuple(to_value(param, type_) for param, type_ in it)
File without changes