squirrels 0.5.0rc0__py3-none-any.whl → 0.5.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.
Potentially problematic release.
This version of squirrels might be problematic. Click here for more details.
- dateutils/__init__.py +6 -0
- dateutils/_enums.py +25 -0
- squirrels/dateutils.py → dateutils/_implementation.py +58 -111
- dateutils/types.py +6 -0
- squirrels/__init__.py +10 -12
- squirrels/_api_routes/__init__.py +5 -0
- squirrels/_api_routes/auth.py +271 -0
- squirrels/_api_routes/base.py +171 -0
- squirrels/_api_routes/dashboards.py +158 -0
- squirrels/_api_routes/data_management.py +148 -0
- squirrels/_api_routes/datasets.py +265 -0
- squirrels/_api_routes/oauth2.py +298 -0
- squirrels/_api_routes/project.py +252 -0
- squirrels/_api_server.py +245 -781
- squirrels/_arguments/__init__.py +0 -0
- squirrels/{arguments → _arguments}/init_time_args.py +7 -2
- squirrels/{arguments → _arguments}/run_time_args.py +13 -35
- squirrels/_auth.py +720 -212
- squirrels/_command_line.py +81 -41
- squirrels/_compile_prompts.py +147 -0
- squirrels/_connection_set.py +16 -7
- squirrels/_constants.py +29 -9
- squirrels/{_dashboards_io.py → _dashboards.py} +87 -6
- squirrels/_data_sources.py +570 -0
- squirrels/{dataset_result.py → _dataset_types.py} +2 -4
- squirrels/_exceptions.py +9 -37
- squirrels/_initializer.py +83 -59
- squirrels/_logging.py +117 -0
- squirrels/_manifest.py +129 -62
- squirrels/_model_builder.py +10 -52
- squirrels/_model_configs.py +3 -3
- squirrels/_model_queries.py +1 -1
- squirrels/_models.py +249 -118
- squirrels/{package_data → _package_data}/base_project/.env +16 -4
- squirrels/{package_data → _package_data}/base_project/.env.example +15 -3
- squirrels/{package_data → _package_data}/base_project/connections.yml +4 -3
- squirrels/{package_data → _package_data}/base_project/dashboards/dashboard_example.py +4 -4
- squirrels/_package_data/base_project/dashboards/dashboard_example.yml +22 -0
- squirrels/{package_data → _package_data}/base_project/duckdb_init.sql +1 -0
- squirrels/_package_data/base_project/macros/macros_example.sql +17 -0
- squirrels/{package_data → _package_data}/base_project/models/builds/build_example.py +2 -2
- squirrels/{package_data → _package_data}/base_project/models/builds/build_example.sql +1 -1
- squirrels/{package_data → _package_data}/base_project/models/builds/build_example.yml +2 -0
- squirrels/_package_data/base_project/models/dbviews/dbview_example.sql +17 -0
- squirrels/_package_data/base_project/models/dbviews/dbview_example.yml +32 -0
- squirrels/_package_data/base_project/models/federates/federate_example.py +48 -0
- squirrels/_package_data/base_project/models/federates/federate_example.sql +21 -0
- squirrels/{package_data → _package_data}/base_project/models/federates/federate_example.yml +7 -7
- squirrels/{package_data → _package_data}/base_project/models/sources.yml +5 -6
- squirrels/{package_data → _package_data}/base_project/parameters.yml +32 -45
- squirrels/_package_data/base_project/pyconfigs/connections.py +18 -0
- squirrels/{package_data → _package_data}/base_project/pyconfigs/context.py +31 -22
- squirrels/_package_data/base_project/pyconfigs/parameters.py +141 -0
- squirrels/_package_data/base_project/pyconfigs/user.py +44 -0
- squirrels/{package_data → _package_data}/base_project/seeds/seed_categories.yml +1 -1
- squirrels/{package_data → _package_data}/base_project/seeds/seed_subcategories.yml +1 -1
- squirrels/_package_data/base_project/squirrels.yml.j2 +61 -0
- squirrels/_package_data/templates/dataset_results.html +112 -0
- squirrels/_package_data/templates/oauth_login.html +271 -0
- squirrels/_package_data/templates/squirrels_studio.html +20 -0
- squirrels/_parameter_configs.py +76 -55
- squirrels/_parameter_options.py +348 -0
- squirrels/_parameter_sets.py +53 -45
- squirrels/_parameters.py +1664 -0
- squirrels/_project.py +403 -242
- squirrels/_py_module.py +3 -2
- squirrels/_request_context.py +33 -0
- squirrels/_schemas/__init__.py +0 -0
- squirrels/_schemas/auth_models.py +167 -0
- squirrels/_schemas/query_param_models.py +75 -0
- squirrels/{_api_response_models.py → _schemas/response_models.py} +48 -18
- squirrels/_seeds.py +1 -1
- squirrels/_sources.py +23 -19
- squirrels/_utils.py +121 -39
- squirrels/_version.py +1 -1
- squirrels/arguments.py +7 -0
- squirrels/auth.py +4 -0
- squirrels/connections.py +3 -0
- squirrels/dashboards.py +2 -81
- squirrels/data_sources.py +14 -563
- squirrels/parameter_options.py +13 -348
- squirrels/parameters.py +14 -1266
- squirrels/types.py +16 -0
- {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/METADATA +42 -30
- squirrels-0.5.1.dist-info/RECORD +98 -0
- squirrels/package_data/base_project/dashboards/dashboard_example.yml +0 -22
- squirrels/package_data/base_project/macros/macros_example.sql +0 -15
- squirrels/package_data/base_project/models/dbviews/dbview_example.sql +0 -12
- squirrels/package_data/base_project/models/dbviews/dbview_example.yml +0 -26
- squirrels/package_data/base_project/models/federates/federate_example.py +0 -44
- squirrels/package_data/base_project/models/federates/federate_example.sql +0 -17
- squirrels/package_data/base_project/pyconfigs/connections.py +0 -14
- squirrels/package_data/base_project/pyconfigs/parameters.py +0 -93
- squirrels/package_data/base_project/pyconfigs/user.py +0 -23
- squirrels/package_data/base_project/squirrels.yml.j2 +0 -71
- squirrels-0.5.0rc0.dist-info/RECORD +0 -70
- /squirrels/{package_data → _package_data}/base_project/assets/expenses.db +0 -0
- /squirrels/{package_data → _package_data}/base_project/assets/weather.db +0 -0
- /squirrels/{package_data → _package_data}/base_project/docker/.dockerignore +0 -0
- /squirrels/{package_data → _package_data}/base_project/docker/Dockerfile +0 -0
- /squirrels/{package_data → _package_data}/base_project/docker/compose.yml +0 -0
- /squirrels/{package_data/base_project/.gitignore → _package_data/base_project/gitignore} +0 -0
- /squirrels/{package_data → _package_data}/base_project/seeds/seed_categories.csv +0 -0
- /squirrels/{package_data → _package_data}/base_project/seeds/seed_subcategories.csv +0 -0
- /squirrels/{package_data → _package_data}/base_project/tmp/.gitignore +0 -0
- {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/WHEEL +0 -0
- {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/entry_points.txt +0 -0
- {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
from typing import TypeVar, Iterable, Any
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from decimal import Decimal, InvalidOperation as InvalidDecimalConversion
|
|
4
|
+
from datetime import datetime, date
|
|
5
|
+
from abc import ABCMeta, abstractmethod
|
|
6
|
+
|
|
7
|
+
from ._utils import ConfigurationError
|
|
8
|
+
|
|
9
|
+
Number = Decimal | int | float | str
|
|
10
|
+
Comparables = TypeVar("Comparables", Decimal, date)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class ParameterOption(metaclass=ABCMeta):
|
|
15
|
+
"""
|
|
16
|
+
Abstract class for parameter options
|
|
17
|
+
"""
|
|
18
|
+
_user_groups: frozenset[Any]
|
|
19
|
+
_parent_option_ids: frozenset[str]
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def __init__(
|
|
23
|
+
self, *, user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
24
|
+
) -> None:
|
|
25
|
+
self._user_groups = frozenset({user_groups} if isinstance(user_groups, str) else user_groups)
|
|
26
|
+
self._parent_option_ids = frozenset({parent_option_ids} if isinstance(parent_option_ids, str) else parent_option_ids)
|
|
27
|
+
|
|
28
|
+
def _validate_lower_upper_values(self, lower_label: str, lower_value: Comparables, upper_label: str, upper_value: Comparables):
|
|
29
|
+
if lower_value > upper_value:
|
|
30
|
+
raise ConfigurationError(f'The {lower_label} "{lower_value}" must be less than or equal to the {upper_label} "{upper_value}"')
|
|
31
|
+
|
|
32
|
+
def _is_valid(self, user_group: Any, selected_parent_option_ids: Iterable[str] | None) -> bool:
|
|
33
|
+
"""
|
|
34
|
+
Checks if this option is valid given the selected parent options and user group of user if applicable.
|
|
35
|
+
|
|
36
|
+
Arguments:
|
|
37
|
+
user_group: The value of the user's "user group attribute". Only None when "user_attribute" is not specified
|
|
38
|
+
for the Parameter factory.
|
|
39
|
+
selected_parent_option_ids: List of selected option ids from the parent parameter. Only None when the Parameter
|
|
40
|
+
object has no parent parameter.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
True if valid, False otherwise
|
|
44
|
+
"""
|
|
45
|
+
if user_group is not None and user_group not in self._user_groups:
|
|
46
|
+
return False
|
|
47
|
+
|
|
48
|
+
if selected_parent_option_ids is not None and self._parent_option_ids.isdisjoint(selected_parent_option_ids):
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
return True
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class SelectParameterOption(ParameterOption):
|
|
56
|
+
"""
|
|
57
|
+
Parameter option for a select parameter
|
|
58
|
+
"""
|
|
59
|
+
_identifier: str
|
|
60
|
+
_label: str
|
|
61
|
+
_is_default: bool
|
|
62
|
+
custom_fields: dict[str, Any]
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self, id: str, label: str, *, is_default: bool = False, user_groups: Iterable[Any] | str = frozenset(),
|
|
66
|
+
parent_option_ids: Iterable[str] | str = frozenset(), custom_fields: dict[str, Any] = {}, **kwargs
|
|
67
|
+
) -> None:
|
|
68
|
+
"""
|
|
69
|
+
Constructor for SelectParameterOption
|
|
70
|
+
|
|
71
|
+
Arguments:
|
|
72
|
+
identifier: Unique identifier for this option that never changes over time
|
|
73
|
+
label: Human readable label that gets shown as a dropdown option
|
|
74
|
+
is_default: True if this is a default option, False otherwise
|
|
75
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
76
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
77
|
+
custom_fields: Dictionary to associate custom attributes to the parameter option
|
|
78
|
+
**kwargs: Any additional keyword arguments specified (except the ones above) gets included into custom_fields as well
|
|
79
|
+
"""
|
|
80
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
81
|
+
self._identifier = id
|
|
82
|
+
self._label = label
|
|
83
|
+
self._is_default = is_default
|
|
84
|
+
self.custom_fields = {
|
|
85
|
+
**kwargs, **custom_fields, **self._to_json_dict()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
def get_custom_field(self, field: str, *, default_field: str | None = None, default: Any = None, **kwargs) -> Any:
|
|
89
|
+
"""
|
|
90
|
+
Get field value from the custom_fields attribute
|
|
91
|
+
|
|
92
|
+
Arguments:
|
|
93
|
+
field: The key to use to fetch the custom field from "custom_fields"
|
|
94
|
+
default_field: If value at "field" key does not exist in "custom_fields", then this is used instead as the field (if not None)
|
|
95
|
+
default: If value at "field" or "default_field" (if not None) key does not exist in "custom_fields", then this value
|
|
96
|
+
is used as default, or throws an error if None
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The type of the custom field
|
|
100
|
+
"""
|
|
101
|
+
if default_field is not None:
|
|
102
|
+
default = self.get_custom_field(default_field, default=default)
|
|
103
|
+
|
|
104
|
+
if default is not None:
|
|
105
|
+
selected_field = self.custom_fields.get(field, default)
|
|
106
|
+
else:
|
|
107
|
+
try:
|
|
108
|
+
selected_field = self.custom_fields[field]
|
|
109
|
+
except KeyError as e:
|
|
110
|
+
raise ConfigurationError(f"Field '{field}' must exist for parameter option {self._to_json_dict()}") from e
|
|
111
|
+
|
|
112
|
+
return selected_field
|
|
113
|
+
|
|
114
|
+
def _to_json_dict(self):
|
|
115
|
+
return {'id': self._identifier, 'label': self._label}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class _DateTypeParameterOption(ParameterOption):
|
|
120
|
+
"""
|
|
121
|
+
Abstract class (or type) for date type parameter options
|
|
122
|
+
"""
|
|
123
|
+
_min_date: date | None
|
|
124
|
+
_max_date: date | None
|
|
125
|
+
_date_format: str
|
|
126
|
+
|
|
127
|
+
@abstractmethod
|
|
128
|
+
def __init__(
|
|
129
|
+
self, *, min_date: str | date | None = None, max_date: str | date | None = None, date_format: str = '%Y-%m-%d',
|
|
130
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
131
|
+
) -> None:
|
|
132
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
133
|
+
self._date_format = date_format
|
|
134
|
+
self._min_date, self._max_date = None, None # preset for using _validate_date()
|
|
135
|
+
self._min_date = self._validate_date(min_date) if min_date is not None else None
|
|
136
|
+
self._max_date = self._validate_date(max_date) if max_date is not None else None
|
|
137
|
+
if self._min_date is not None and self._max_date is not None:
|
|
138
|
+
self._validate_lower_upper_values("min_date", self._min_date, "max_date", self._max_date)
|
|
139
|
+
|
|
140
|
+
def _validate_date(self, date_str: str | date) -> date:
|
|
141
|
+
try:
|
|
142
|
+
date_obj = datetime.strptime(date_str, self._date_format).date() if isinstance(date_str, str) else date_str
|
|
143
|
+
except ValueError as e:
|
|
144
|
+
raise ConfigurationError(f'Invalid format for date "{date_str}".') from e
|
|
145
|
+
|
|
146
|
+
if self._min_date is not None and date_obj < self._min_date:
|
|
147
|
+
raise ConfigurationError(f'The provided date "{date_obj}" is less than the min date "{self._min_date}"')
|
|
148
|
+
if self._max_date is not None and date_obj > self._max_date:
|
|
149
|
+
raise ConfigurationError(f'The provided date "{date_obj}" is greater than the max date "{self._max_date}"')
|
|
150
|
+
|
|
151
|
+
return date_obj
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@dataclass
|
|
155
|
+
class DateParameterOption(_DateTypeParameterOption):
|
|
156
|
+
"""
|
|
157
|
+
Parameter option for default dates if it varies based on selection of another parameter
|
|
158
|
+
"""
|
|
159
|
+
_default_date: date
|
|
160
|
+
|
|
161
|
+
def __init__(
|
|
162
|
+
self, default_date: str | date, *, min_date: str | date | None = None, max_date: str | date | None = None, date_format: str = '%Y-%m-%d',
|
|
163
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
164
|
+
) -> None:
|
|
165
|
+
"""
|
|
166
|
+
Constructor for DateParameterOption
|
|
167
|
+
|
|
168
|
+
Arguments:
|
|
169
|
+
default_date: Default date for this option
|
|
170
|
+
min_date: Minimum date for this option
|
|
171
|
+
max_date: Maximum date for this option
|
|
172
|
+
date_format: Format of the default date, default is '%Y-%m-%d'
|
|
173
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
174
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
175
|
+
"""
|
|
176
|
+
super().__init__(
|
|
177
|
+
date_format=date_format, min_date=min_date, max_date=max_date, user_groups=user_groups, parent_option_ids=parent_option_ids
|
|
178
|
+
)
|
|
179
|
+
self._default_date = self._validate_date(default_date)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@dataclass
|
|
183
|
+
class DateRangeParameterOption(_DateTypeParameterOption):
|
|
184
|
+
"""
|
|
185
|
+
Parameter option for default dates if it varies based on selection of another parameter
|
|
186
|
+
"""
|
|
187
|
+
_default_start_date: date
|
|
188
|
+
_default_end_date: date
|
|
189
|
+
|
|
190
|
+
def __init__(
|
|
191
|
+
self, default_start_date: str | date, default_end_date: str | date, *, min_date: str | date | None = None,
|
|
192
|
+
max_date: str | date | None = None, date_format: str = '%Y-%m-%d', user_groups: Iterable[Any] | str = frozenset(),
|
|
193
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
194
|
+
) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Constructor for DateRangeParameterOption
|
|
197
|
+
|
|
198
|
+
Arguments:
|
|
199
|
+
default_start_date: Default start date for this option
|
|
200
|
+
default_end_date: Default end date for this option
|
|
201
|
+
min_date: Minimum date for this option
|
|
202
|
+
max_date: Maximum date for this option
|
|
203
|
+
date_format: Format of the default date, default is '%Y-%m-%d'
|
|
204
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
205
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
206
|
+
"""
|
|
207
|
+
super().__init__(
|
|
208
|
+
date_format=date_format, min_date=min_date, max_date=max_date, user_groups=user_groups, parent_option_ids=parent_option_ids
|
|
209
|
+
)
|
|
210
|
+
self._default_start_date = self._validate_date(default_start_date)
|
|
211
|
+
self._default_end_date = self._validate_date(default_end_date)
|
|
212
|
+
self._validate_lower_upper_values("default_start_date", self._default_start_date, "default_end_date", self._default_end_date)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@dataclass
|
|
216
|
+
class _NumericParameterOption(ParameterOption):
|
|
217
|
+
"""
|
|
218
|
+
Abstract class (or type) for numeric parameter options
|
|
219
|
+
"""
|
|
220
|
+
_min_value: Decimal
|
|
221
|
+
_max_value: Decimal
|
|
222
|
+
_increment: Decimal
|
|
223
|
+
|
|
224
|
+
@abstractmethod
|
|
225
|
+
def __init__(
|
|
226
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, user_groups: Iterable[Any] | str = frozenset(),
|
|
227
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
228
|
+
) -> None:
|
|
229
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
230
|
+
try:
|
|
231
|
+
self._min_value = Decimal(str(min_value))
|
|
232
|
+
self._max_value = Decimal(str(max_value))
|
|
233
|
+
self._increment = Decimal(str(increment))
|
|
234
|
+
except InvalidDecimalConversion as e:
|
|
235
|
+
raise ConfigurationError(f'Could not convert either min, max, or increment to number') from e
|
|
236
|
+
|
|
237
|
+
self._validate_lower_upper_values("min_value", self._min_value, "max_value", self._max_value)
|
|
238
|
+
|
|
239
|
+
if (self._max_value - self._min_value) % self._increment != 0:
|
|
240
|
+
raise ConfigurationError(f'The increment "{self._increment}" must fit evenly between ' +
|
|
241
|
+
f'the min_value "{self._min_value}" and max_value "{self._max_value}"')
|
|
242
|
+
|
|
243
|
+
def __value_in_range(self, value: Decimal) -> bool:
|
|
244
|
+
return self._min_value <= value <= self._max_value
|
|
245
|
+
|
|
246
|
+
def __value_on_increment(self, value: Decimal) -> bool:
|
|
247
|
+
diff = (value - self._min_value)
|
|
248
|
+
return diff >= 0 and diff % self._increment == 0
|
|
249
|
+
|
|
250
|
+
def _validate_value(self, value: Number) -> Decimal:
|
|
251
|
+
try:
|
|
252
|
+
value = Decimal(str(value))
|
|
253
|
+
except InvalidDecimalConversion as e:
|
|
254
|
+
raise ConfigurationError(f'Could not convert "{value}" to number', e)
|
|
255
|
+
|
|
256
|
+
if not self.__value_in_range(value):
|
|
257
|
+
raise ConfigurationError(f'The selected value "{value}" is outside of bounds ' +
|
|
258
|
+
f'"{self._min_value}" and "{self._max_value}".')
|
|
259
|
+
if not self.__value_on_increment(value):
|
|
260
|
+
raise ConfigurationError(f'The difference between selected value "{value}" and lower value ' +
|
|
261
|
+
f'"{self._min_value}" must be a multiple of increment "{self._increment}".')
|
|
262
|
+
return value
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass
|
|
266
|
+
class NumberParameterOption(_NumericParameterOption):
|
|
267
|
+
"""
|
|
268
|
+
Parameter option for default numbers if it varies based on selection of another parameter
|
|
269
|
+
"""
|
|
270
|
+
_default_value: Decimal
|
|
271
|
+
|
|
272
|
+
def __init__(
|
|
273
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, default_value: Number | None = None,
|
|
274
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
275
|
+
) -> None:
|
|
276
|
+
"""
|
|
277
|
+
Constructor for NumberParameterOption
|
|
278
|
+
|
|
279
|
+
* Note that the "Number" type denotes an int, a Decimal (from decimal module), or a string that can be parsed to Decimal
|
|
280
|
+
|
|
281
|
+
Arguments:
|
|
282
|
+
min_value: Minimum selectable value
|
|
283
|
+
max_value: Maximum selectable value
|
|
284
|
+
increment: Increment of selectable values, and must fit evenly between min_value and max_value
|
|
285
|
+
default_value: Default value for this option, and must be selectable based on min_value, max_value, and increment
|
|
286
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
287
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
288
|
+
"""
|
|
289
|
+
super().__init__(min_value, max_value, increment=increment, user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
290
|
+
self._default_value = self._validate_value(default_value) if default_value is not None else self._min_value
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
@dataclass
|
|
294
|
+
class NumberRangeParameterOption(_NumericParameterOption):
|
|
295
|
+
"""
|
|
296
|
+
Parameter option for default numeric ranges if it varies based on selection of another parameter
|
|
297
|
+
"""
|
|
298
|
+
_default_lower_value: Decimal
|
|
299
|
+
_default_upper_value: Decimal
|
|
300
|
+
|
|
301
|
+
def __init__(
|
|
302
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, default_lower_value: Number | None = None,
|
|
303
|
+
default_upper_value: Number | None = None, user_groups: Iterable[Any] | str = frozenset(),
|
|
304
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
305
|
+
) -> None:
|
|
306
|
+
"""
|
|
307
|
+
Constructor for NumberRangeParameterOption
|
|
308
|
+
|
|
309
|
+
* Note that the "Number" type denotes an int, a Decimal (from decimal module), or a string that can be parsed to Decimal
|
|
310
|
+
|
|
311
|
+
Arguments:
|
|
312
|
+
min_value: Minimum selectable value
|
|
313
|
+
max_value: Maximum selectable value
|
|
314
|
+
increment: Increment of selectable values, and must fit evenly between min_value and max_value
|
|
315
|
+
default_lower_value: Default lower value for this option, and must be selectable based on min_value, max_value, and increment
|
|
316
|
+
default_upper_value: Default upper value for this option, and must be selectable based on min_value, max_value, and increment.
|
|
317
|
+
Must also be greater than default_lower_value
|
|
318
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
319
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
320
|
+
"""
|
|
321
|
+
super().__init__(min_value, max_value, increment=increment, user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
322
|
+
self._default_lower_value = self._validate_value(default_lower_value) if default_lower_value is not None else self._min_value
|
|
323
|
+
self._default_upper_value = self._validate_value(default_upper_value) if default_upper_value is not None else self._max_value
|
|
324
|
+
self._validate_lower_upper_values("default_lower_value", self._default_lower_value, "default_upper_value", self._default_upper_value)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@dataclass
|
|
328
|
+
class TextParameterOption(ParameterOption):
|
|
329
|
+
"""
|
|
330
|
+
Parameter option for default text values if it varies based on selection of another parameter
|
|
331
|
+
"""
|
|
332
|
+
_default_text: str
|
|
333
|
+
|
|
334
|
+
def __init__(
|
|
335
|
+
self, *, default_text: str = "", user_groups: Iterable[Any] | str = frozenset(),
|
|
336
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
337
|
+
) -> None:
|
|
338
|
+
"""
|
|
339
|
+
Constructor for TextParameterOption
|
|
340
|
+
|
|
341
|
+
Arguments:
|
|
342
|
+
default_text: Default text for this option
|
|
343
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
344
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
345
|
+
"""
|
|
346
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
347
|
+
self._default_text = default_text
|
|
348
|
+
|
squirrels/_parameter_sets.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
from typing import Optional, Sequence, Any
|
|
2
|
+
from typing import Optional, Sequence, Callable, Any
|
|
3
3
|
from dataclasses import dataclass, field
|
|
4
4
|
from collections import OrderedDict
|
|
5
5
|
import time, concurrent.futures, polars as pl
|
|
6
6
|
|
|
7
|
-
from . import
|
|
8
|
-
from .
|
|
7
|
+
from . import _parameters as p, _utils as u, _constants as c, _parameter_configs as pc, _py_module as pm
|
|
8
|
+
from ._schemas import response_models as rm
|
|
9
|
+
from ._arguments.init_time_args import ParametersArgs
|
|
9
10
|
from ._manifest import ParametersConfig, ManifestConfig
|
|
10
|
-
from ._connection_set import ConnectionSet
|
|
11
|
+
from ._connection_set import ConnectionSet
|
|
11
12
|
from ._seeds import Seeds
|
|
12
|
-
from .
|
|
13
|
+
from ._schemas.auth_models import AbstractUser
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
@dataclass
|
|
@@ -22,11 +23,11 @@ class ParameterSet:
|
|
|
22
23
|
def get_parameters_as_dict(self) -> dict[str, p.Parameter]:
|
|
23
24
|
return self._parameters_dict.copy()
|
|
24
25
|
|
|
25
|
-
def to_api_response_model0(self) ->
|
|
26
|
+
def to_api_response_model0(self) -> rm.ParametersModel:
|
|
26
27
|
parameters = []
|
|
27
28
|
for x in self._parameters_dict.values():
|
|
28
29
|
parameters.append(x._to_api_response_model0())
|
|
29
|
-
return
|
|
30
|
+
return rm.ParametersModel(parameters=parameters)
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
@dataclass
|
|
@@ -34,21 +35,21 @@ class ParameterConfigsSet:
|
|
|
34
35
|
"""
|
|
35
36
|
Pool of parameter configs, can create multiple for unit testing purposes
|
|
36
37
|
"""
|
|
37
|
-
_data: dict[str,
|
|
38
|
-
_data_source_params: dict[str,
|
|
38
|
+
_data: dict[str, pc.ParameterConfigBase] = field(default_factory=OrderedDict)
|
|
39
|
+
_data_source_params: dict[str, pc.DataSourceParameterConfig] = field(default_factory=dict)
|
|
39
40
|
|
|
40
|
-
def get(self, name: Optional[str]) -> Optional[
|
|
41
|
+
def get(self, name: Optional[str]) -> Optional[pc.ParameterConfigBase]:
|
|
41
42
|
try:
|
|
42
43
|
return self._data[name] if name is not None else None
|
|
43
44
|
except KeyError as e:
|
|
44
45
|
raise u.ConfigurationError(f'Unable to find parameter named "{name}"') from e
|
|
45
46
|
|
|
46
|
-
def add(self, param_config:
|
|
47
|
+
def add(self, param_config: pc.ParameterConfigBase) -> None:
|
|
47
48
|
self._data[param_config.name] = param_config
|
|
48
|
-
if isinstance(param_config,
|
|
49
|
+
if isinstance(param_config, pc.DataSourceParameterConfig):
|
|
49
50
|
self._data_source_params[param_config.name] = param_config
|
|
50
51
|
|
|
51
|
-
def _get_all_ds_param_configs(self) -> Sequence[
|
|
52
|
+
def _get_all_ds_param_configs(self) -> Sequence[pc.DataSourceParameterConfig]:
|
|
52
53
|
return list(self._data_source_params.values())
|
|
53
54
|
|
|
54
55
|
def __convert_datasource_params(self, df_dict: dict[str, pl.DataFrame]) -> None:
|
|
@@ -64,7 +65,7 @@ class ParameterConfigsSet:
|
|
|
64
65
|
if parent_name is not None and parent_name not in done:
|
|
65
66
|
stack.append(parent_name)
|
|
66
67
|
continue
|
|
67
|
-
if isinstance(param,
|
|
68
|
+
if isinstance(param, pc.DataSourceParameterConfig):
|
|
68
69
|
if name not in df_dict:
|
|
69
70
|
raise u.ConfigurationError(f'No reference data found for parameter "{name}"')
|
|
70
71
|
self._data[name] = param.convert(df_dict[name])
|
|
@@ -73,12 +74,12 @@ class ParameterConfigsSet:
|
|
|
73
74
|
|
|
74
75
|
def __validate_param_relationships(self) -> None:
|
|
75
76
|
for param_config in self._data.values():
|
|
76
|
-
assert isinstance(param_config,
|
|
77
|
+
assert isinstance(param_config, pc.ParameterConfig)
|
|
77
78
|
parent_name = param_config.parent_name
|
|
78
79
|
parent = self.get(parent_name)
|
|
79
80
|
if parent:
|
|
80
|
-
if not isinstance(param_config,
|
|
81
|
-
if not isinstance(parent,
|
|
81
|
+
if not isinstance(param_config, pc.SelectionParameterConfig):
|
|
82
|
+
if not isinstance(parent, pc.SingleSelectParameterConfig):
|
|
82
83
|
raise u.ConfigurationError(f'Only single-select parameters can be parents of non-select parameters. ' +
|
|
83
84
|
f'Parameter "{parent_name}" is the parent of non-select parameter ' +
|
|
84
85
|
f'"{param_config.name}" but "{parent_name}" is not a single-select parameter.')
|
|
@@ -92,7 +93,7 @@ class ParameterConfigsSet:
|
|
|
92
93
|
f'among the options of non-select parameter "{param_config.name}".')
|
|
93
94
|
seen.update(lookup_keys)
|
|
94
95
|
|
|
95
|
-
if not isinstance(parent,
|
|
96
|
+
if not isinstance(parent, pc.SelectionParameterConfig):
|
|
96
97
|
raise u.ConfigurationError(f'Only selection parameters can be parents. Parameter "{parent_name}" is the parent of ' +
|
|
97
98
|
f'"{param_config.name}" but "{parent_name}" is not a selection parameter.')
|
|
98
99
|
|
|
@@ -103,13 +104,10 @@ class ParameterConfigsSet:
|
|
|
103
104
|
self.__validate_param_relationships()
|
|
104
105
|
|
|
105
106
|
def apply_selections(
|
|
106
|
-
self, dataset_params: Optional[Sequence[str]], selections: dict[str, Any], user:
|
|
107
|
+
self, dataset_params: Optional[Sequence[str]], selections: dict[str, Any], user: AbstractUser, *, parent_param: str | None = None
|
|
107
108
|
) -> ParameterSet:
|
|
108
109
|
if dataset_params is None:
|
|
109
|
-
|
|
110
|
-
dataset_params = [k for k, v in self._data.items() if v.user_attribute is None]
|
|
111
|
-
else:
|
|
112
|
-
dataset_params = list(self._data.keys())
|
|
110
|
+
dataset_params = list(self._data.keys())
|
|
113
111
|
|
|
114
112
|
parameters_by_name: dict[str, p.Parameter] = {}
|
|
115
113
|
params_to_process = [parent_param] if parent_param else dataset_params
|
|
@@ -121,7 +119,7 @@ class ParameterConfigsSet:
|
|
|
121
119
|
children = []
|
|
122
120
|
if curr_name not in parameters_by_name:
|
|
123
121
|
param_conf = self.get(curr_name)
|
|
124
|
-
assert isinstance(param_conf,
|
|
122
|
+
assert isinstance(param_conf, pc.ParameterConfig)
|
|
125
123
|
parent_name = param_conf.parent_name
|
|
126
124
|
if parent_name is None:
|
|
127
125
|
parent = None
|
|
@@ -133,7 +131,7 @@ class ParameterConfigsSet:
|
|
|
133
131
|
assert isinstance(parent, p._SelectionParameter) or parent is None
|
|
134
132
|
param = param_conf.with_selection(selections.get(curr_name), user, parent)
|
|
135
133
|
parameters_by_name[curr_name] = param
|
|
136
|
-
if isinstance(param_conf,
|
|
134
|
+
if isinstance(param_conf, pc.SelectionParameterConfig):
|
|
137
135
|
children = list(x for x in param_conf.children.keys() if x in dataset_params)
|
|
138
136
|
stack.pop()
|
|
139
137
|
stack.extend(children)
|
|
@@ -141,10 +139,10 @@ class ParameterConfigsSet:
|
|
|
141
139
|
ordered_parameters = OrderedDict((key, parameters_by_name[key]) for key in dataset_params if key in parameters_by_name)
|
|
142
140
|
return ParameterSet(ordered_parameters)
|
|
143
141
|
|
|
144
|
-
def get_all_api_field_info(self) -> dict[str,
|
|
142
|
+
def get_all_api_field_info(self) -> dict[str, pc.APIParamFieldInfo]:
|
|
145
143
|
api_field_infos = {}
|
|
146
144
|
for param, config in self._data.items():
|
|
147
|
-
assert isinstance(config,
|
|
145
|
+
assert isinstance(config, pc.ParameterConfig)
|
|
148
146
|
api_field_infos[param] = config.get_api_field_info()
|
|
149
147
|
return api_field_infos
|
|
150
148
|
|
|
@@ -153,44 +151,54 @@ class ParameterConfigsSetIO:
|
|
|
153
151
|
"""
|
|
154
152
|
Static class for the singleton object of ParameterConfigsSet
|
|
155
153
|
"""
|
|
156
|
-
|
|
154
|
+
param_factories: list[Callable[[ParametersArgs], pc.ParameterConfigBase]] = [] # this is static (set in load_from_file) to stage the functions from pyconfigs/parameters.py before using them
|
|
157
155
|
|
|
158
156
|
@classmethod
|
|
159
|
-
def _get_df_dict_from_data_sources(
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
def _get_df_dict_from_data_sources(
|
|
158
|
+
cls, param_configs_set: ParameterConfigsSet, default_conn_name: str, seeds: Seeds, conn_set: ConnectionSet, datalake_db_path: str
|
|
159
|
+
) -> dict[str, pl.DataFrame]:
|
|
162
160
|
|
|
163
|
-
|
|
161
|
+
def get_dataframe(ds_param_config: pc.DataSourceParameterConfig) -> tuple[str, pl.DataFrame]:
|
|
162
|
+
return ds_param_config.name, ds_param_config.get_dataframe(default_conn_name, conn_set, seeds, datalake_db_path)
|
|
163
|
+
|
|
164
|
+
ds_param_configs = param_configs_set._get_all_ds_param_configs()
|
|
164
165
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
165
166
|
df_dict = dict(executor.map(get_dataframe, ds_param_configs))
|
|
166
167
|
|
|
167
168
|
return df_dict
|
|
168
169
|
|
|
169
170
|
@classmethod
|
|
170
|
-
def _add_from_dict(cls, param_config: ParametersConfig) -> None:
|
|
171
|
+
def _add_from_dict(cls, param_configs_set: ParameterConfigsSet, param_config: ParametersConfig) -> None:
|
|
171
172
|
ptype = getattr(p, param_config.type)
|
|
172
173
|
factory = getattr(ptype, param_config.factory)
|
|
173
|
-
factory(**param_config.arguments)
|
|
174
|
-
|
|
175
|
-
@classmethod
|
|
176
|
-
def get_param_args(cls, conn_args: ConnectionsArgs) -> ParametersArgs:
|
|
177
|
-
return ParametersArgs(conn_args.project_path, conn_args.proj_vars, conn_args.env_vars)
|
|
174
|
+
obj = factory(**param_config.arguments)
|
|
175
|
+
param_configs_set.add(obj)
|
|
178
176
|
|
|
179
177
|
@classmethod
|
|
180
178
|
def load_from_file(
|
|
181
|
-
cls, logger: u.Logger, base_path: str, manifest_cfg: ManifestConfig, seeds: Seeds, conn_set: ConnectionSet, param_args: ParametersArgs
|
|
179
|
+
cls, logger: u.Logger, base_path: str, manifest_cfg: ManifestConfig, seeds: Seeds, conn_set: ConnectionSet, param_args: ParametersArgs,
|
|
180
|
+
datalake_db_path: str
|
|
182
181
|
) -> ParameterConfigsSet:
|
|
183
182
|
start = time.time()
|
|
184
|
-
|
|
183
|
+
param_configs_set = ParameterConfigsSet()
|
|
185
184
|
|
|
186
185
|
for param_as_dict in manifest_cfg.parameters:
|
|
187
|
-
cls._add_from_dict(param_as_dict)
|
|
186
|
+
cls._add_from_dict(param_configs_set, param_as_dict)
|
|
187
|
+
|
|
188
|
+
main_result = pm.run_pyconfig_main(base_path, c.PARAMETERS_FILE, {"sqrl": param_args}) # adds to cls.param_factories as side effect
|
|
189
|
+
param_factories = cls.param_factories
|
|
190
|
+
cls.param_factories = []
|
|
191
|
+
|
|
192
|
+
for param_factory in param_factories:
|
|
193
|
+
param_configs_set.add(param_factory(param_args))
|
|
188
194
|
|
|
189
|
-
|
|
195
|
+
if isinstance(main_result, list):
|
|
196
|
+
for param_config in main_result:
|
|
197
|
+
param_configs_set.add(param_config)
|
|
190
198
|
|
|
191
199
|
default_conn_name = manifest_cfg.env_vars.get(c.SQRL_CONNECTIONS_DEFAULT_NAME_USED, "default")
|
|
192
|
-
df_dict = cls._get_df_dict_from_data_sources(default_conn_name, seeds, conn_set)
|
|
193
|
-
|
|
200
|
+
df_dict = cls._get_df_dict_from_data_sources(param_configs_set, default_conn_name, seeds, conn_set, datalake_db_path)
|
|
201
|
+
param_configs_set._post_process_params(df_dict)
|
|
194
202
|
|
|
195
203
|
logger.log_activity_time("loading parameters", start)
|
|
196
|
-
return
|
|
204
|
+
return param_configs_set
|