power-grid-model 1.10.74__py3-none-win_amd64.whl → 1.12.119__py3-none-win_amd64.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 power-grid-model might be problematic. Click here for more details.
- power_grid_model/__init__.py +54 -29
- power_grid_model/_core/__init__.py +3 -3
- power_grid_model/_core/buffer_handling.py +507 -478
- power_grid_model/_core/data_handling.py +195 -141
- power_grid_model/_core/data_types.py +142 -0
- power_grid_model/_core/dataset_definitions.py +109 -109
- power_grid_model/_core/enum.py +226 -0
- power_grid_model/_core/error_handling.py +215 -202
- power_grid_model/_core/errors.py +134 -0
- power_grid_model/_core/index_integer.py +17 -17
- power_grid_model/_core/options.py +71 -69
- power_grid_model/_core/power_grid_core.py +577 -597
- power_grid_model/_core/power_grid_dataset.py +545 -528
- power_grid_model/_core/power_grid_meta.py +262 -244
- power_grid_model/_core/power_grid_model.py +1025 -692
- power_grid_model/_core/power_grid_model_c/__init__.py +3 -0
- power_grid_model/_core/power_grid_model_c/bin/power_grid_model_c.dll +0 -0
- power_grid_model/_core/power_grid_model_c/get_pgm_dll_path.py +63 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/basics.h +251 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/buffer.h +108 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset.h +332 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset_definitions.h +1060 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/handle.h +111 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/meta_data.h +189 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/model.h +130 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/options.h +142 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/serialization.h +118 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c.h +36 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/basics.hpp +65 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/buffer.hpp +61 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/dataset.hpp +224 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/handle.hpp +108 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/meta_data.hpp +84 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/model.hpp +63 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/options.hpp +52 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/serialization.hpp +124 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/utils.hpp +81 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp.hpp +19 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfig.cmake +37 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfigVersion.cmake +65 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets-release.cmake +19 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets.cmake +144 -0
- power_grid_model/_core/power_grid_model_c/lib/power_grid_model_c.lib +0 -0
- power_grid_model/_core/power_grid_model_c/share/LICENSE +292 -0
- power_grid_model/_core/power_grid_model_c/share/README.md +15 -0
- power_grid_model/_core/serialization.py +319 -317
- power_grid_model/_core/typing.py +20 -0
- power_grid_model/{_utils.py → _core/utils.py} +798 -783
- power_grid_model/data_types.py +321 -319
- power_grid_model/enum.py +27 -214
- power_grid_model/errors.py +37 -123
- power_grid_model/typing.py +43 -48
- power_grid_model/utils.py +529 -400
- power_grid_model/validation/__init__.py +25 -14
- power_grid_model/validation/_rules.py +1167 -904
- power_grid_model/validation/_validation.py +1172 -980
- power_grid_model/validation/assertions.py +93 -92
- power_grid_model/validation/errors.py +602 -520
- power_grid_model/validation/utils.py +313 -318
- {power_grid_model-1.10.74.dist-info → power_grid_model-1.12.119.dist-info}/METADATA +162 -171
- power_grid_model-1.12.119.dist-info/RECORD +65 -0
- {power_grid_model-1.10.74.dist-info → power_grid_model-1.12.119.dist-info}/WHEEL +1 -1
- power_grid_model-1.12.119.dist-info/entry_points.txt +3 -0
- power_grid_model/_core/_power_grid_core.dll +0 -0
- power_grid_model-1.10.74.dist-info/RECORD +0 -32
- power_grid_model-1.10.74.dist-info/top_level.txt +0 -1
- {power_grid_model-1.10.74.dist-info → power_grid_model-1.12.119.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,317 +1,319 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
|
|
2
|
-
#
|
|
3
|
-
# SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
|
|
5
|
-
"""
|
|
6
|
-
Power grid model (de)serialization
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
from abc import ABC, abstractmethod
|
|
10
|
-
from ctypes import byref
|
|
11
|
-
from enum import IntEnum
|
|
12
|
-
|
|
13
|
-
from power_grid_model._core.
|
|
14
|
-
from power_grid_model._core.
|
|
15
|
-
from power_grid_model._core.
|
|
16
|
-
from power_grid_model._core.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
from power_grid_model.
|
|
26
|
-
from power_grid_model.typing import ComponentAttributeMapping
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class SerializationType(IntEnum):
|
|
30
|
-
"""Serialization Format Types"""
|
|
31
|
-
|
|
32
|
-
JSON = 0
|
|
33
|
-
MSGPACK = 1
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
class Deserializer:
|
|
37
|
-
"""
|
|
38
|
-
Deserializer for the Power grid model
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
_deserializer: DeserializerPtr
|
|
42
|
-
_dataset_ptr: WritableDatasetPtr
|
|
43
|
-
_dataset: CWritableDataset
|
|
44
|
-
_data_filter: ComponentAttributeMapping
|
|
45
|
-
|
|
46
|
-
def __new__(
|
|
47
|
-
cls,
|
|
48
|
-
data: str | bytes,
|
|
49
|
-
serialization_type: SerializationType,
|
|
50
|
-
data_filter: ComponentAttributeMapping,
|
|
51
|
-
):
|
|
52
|
-
instance = super().__new__(cls)
|
|
53
|
-
|
|
54
|
-
raw_data = data if isinstance(data, bytes) else data.encode()
|
|
55
|
-
instance._deserializer =
|
|
56
|
-
raw_data, len(raw_data), serialization_type.value
|
|
57
|
-
)
|
|
58
|
-
assert_no_error()
|
|
59
|
-
|
|
60
|
-
instance._dataset_ptr =
|
|
61
|
-
assert_no_error()
|
|
62
|
-
|
|
63
|
-
instance._data_filter = data_filter
|
|
64
|
-
instance._dataset = CWritableDataset(instance._dataset_ptr, data_filter=data_filter)
|
|
65
|
-
assert_no_error()
|
|
66
|
-
|
|
67
|
-
return instance
|
|
68
|
-
|
|
69
|
-
def __del__(self):
|
|
70
|
-
if hasattr(self, "_deserializer"):
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def load(self) -> Dataset:
|
|
74
|
-
"""
|
|
75
|
-
Load the deserialized data to a new dataset.
|
|
76
|
-
|
|
77
|
-
Raises:
|
|
78
|
-
ValueError: if the data is inconsistent with the rest of the dataset or a component is unknown.
|
|
79
|
-
PowerGridError: if there was an internal error.
|
|
80
|
-
|
|
81
|
-
Returns:
|
|
82
|
-
A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.
|
|
83
|
-
"""
|
|
84
|
-
|
|
85
|
-
return self._dataset.get_data()
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
class Serializer(ABC):
|
|
89
|
-
"""
|
|
90
|
-
Serializer for the Power grid model
|
|
91
|
-
"""
|
|
92
|
-
|
|
93
|
-
_data: Dataset
|
|
94
|
-
_dataset: CConstDataset
|
|
95
|
-
_serializer: SerializerPtr
|
|
96
|
-
|
|
97
|
-
def __new__(cls, data: Dataset, serialization_type: SerializationType, dataset_type: DatasetType | None = None):
|
|
98
|
-
instance = super().__new__(cls)
|
|
99
|
-
|
|
100
|
-
instance._data = data
|
|
101
|
-
instance._dataset = CConstDataset(instance._data, dataset_type=dataset_type)
|
|
102
|
-
assert_no_error()
|
|
103
|
-
|
|
104
|
-
instance._serializer =
|
|
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
|
-
"""
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
"""
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
"""
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
"""
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
"""
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
"""
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
1
|
+
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
Power grid model (de)serialization
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from ctypes import byref
|
|
11
|
+
from enum import IntEnum
|
|
12
|
+
|
|
13
|
+
from power_grid_model._core.data_types import Dataset
|
|
14
|
+
from power_grid_model._core.dataset_definitions import DatasetType, _map_to_component_types, _str_to_datatype
|
|
15
|
+
from power_grid_model._core.error_handling import assert_no_error
|
|
16
|
+
from power_grid_model._core.errors import PowerGridSerializationError
|
|
17
|
+
from power_grid_model._core.index_integer import IdxC
|
|
18
|
+
from power_grid_model._core.power_grid_core import (
|
|
19
|
+
CharPtr,
|
|
20
|
+
DeserializerPtr,
|
|
21
|
+
SerializerPtr,
|
|
22
|
+
WritableDatasetPtr,
|
|
23
|
+
get_power_grid_core as get_pgc,
|
|
24
|
+
)
|
|
25
|
+
from power_grid_model._core.power_grid_dataset import CConstDataset, CWritableDataset
|
|
26
|
+
from power_grid_model._core.typing import ComponentAttributeMapping
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SerializationType(IntEnum):
|
|
30
|
+
"""Serialization Format Types"""
|
|
31
|
+
|
|
32
|
+
JSON = 0
|
|
33
|
+
MSGPACK = 1
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Deserializer:
|
|
37
|
+
"""
|
|
38
|
+
Deserializer for the Power grid model
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
_deserializer: DeserializerPtr
|
|
42
|
+
_dataset_ptr: WritableDatasetPtr
|
|
43
|
+
_dataset: CWritableDataset
|
|
44
|
+
_data_filter: ComponentAttributeMapping
|
|
45
|
+
|
|
46
|
+
def __new__(
|
|
47
|
+
cls,
|
|
48
|
+
data: str | bytes,
|
|
49
|
+
serialization_type: SerializationType,
|
|
50
|
+
data_filter: ComponentAttributeMapping,
|
|
51
|
+
):
|
|
52
|
+
instance = super().__new__(cls)
|
|
53
|
+
|
|
54
|
+
raw_data = data if isinstance(data, bytes) else data.encode()
|
|
55
|
+
instance._deserializer = get_pgc().create_deserializer_from_binary_buffer(
|
|
56
|
+
raw_data, len(raw_data), serialization_type.value
|
|
57
|
+
)
|
|
58
|
+
assert_no_error()
|
|
59
|
+
|
|
60
|
+
instance._dataset_ptr = get_pgc().deserializer_get_dataset(instance._deserializer)
|
|
61
|
+
assert_no_error()
|
|
62
|
+
|
|
63
|
+
instance._data_filter = data_filter
|
|
64
|
+
instance._dataset = CWritableDataset(instance._dataset_ptr, data_filter=data_filter)
|
|
65
|
+
assert_no_error()
|
|
66
|
+
|
|
67
|
+
return instance
|
|
68
|
+
|
|
69
|
+
def __del__(self):
|
|
70
|
+
if hasattr(self, "_deserializer"):
|
|
71
|
+
get_pgc().destroy_deserializer(self._deserializer)
|
|
72
|
+
|
|
73
|
+
def load(self) -> Dataset:
|
|
74
|
+
"""
|
|
75
|
+
Load the deserialized data to a new dataset.
|
|
76
|
+
|
|
77
|
+
Raises:
|
|
78
|
+
ValueError: if the data is inconsistent with the rest of the dataset or a component is unknown.
|
|
79
|
+
PowerGridError: if there was an internal error.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.
|
|
83
|
+
"""
|
|
84
|
+
get_pgc().deserializer_parse_to_buffer(self._deserializer)
|
|
85
|
+
return self._dataset.get_data()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class Serializer(ABC):
|
|
89
|
+
"""
|
|
90
|
+
Serializer for the Power grid model
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
_data: Dataset
|
|
94
|
+
_dataset: CConstDataset
|
|
95
|
+
_serializer: SerializerPtr
|
|
96
|
+
|
|
97
|
+
def __new__(cls, data: Dataset, serialization_type: SerializationType, dataset_type: DatasetType | None = None):
|
|
98
|
+
instance = super().__new__(cls)
|
|
99
|
+
|
|
100
|
+
instance._data = data
|
|
101
|
+
instance._dataset = CConstDataset(instance._data, dataset_type=dataset_type)
|
|
102
|
+
assert_no_error()
|
|
103
|
+
|
|
104
|
+
instance._serializer = get_pgc().create_serializer(
|
|
105
|
+
instance._dataset.get_dataset_ptr(), serialization_type.value
|
|
106
|
+
)
|
|
107
|
+
assert_no_error()
|
|
108
|
+
|
|
109
|
+
return instance
|
|
110
|
+
|
|
111
|
+
def __del__(self):
|
|
112
|
+
if hasattr(self, "_serializer"):
|
|
113
|
+
get_pgc().destroy_serializer(self._serializer)
|
|
114
|
+
|
|
115
|
+
def dump_str(self, *, use_compact_list: bool = False, indent: int = 2) -> str:
|
|
116
|
+
"""
|
|
117
|
+
Dump the data to a decoded str; if supported.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
use_compact_list: whether or not to use compact lists (sparse data). Defaults to False.
|
|
121
|
+
indent:
|
|
122
|
+
use specified indentation to make data more readable
|
|
123
|
+
Use 0 or negative value for no indentation. Defaults to 2
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
A serialized string containing the dataset.
|
|
127
|
+
"""
|
|
128
|
+
data = get_pgc().serializer_get_to_zero_terminated_string(self._serializer, int(use_compact_list), indent)
|
|
129
|
+
assert_no_error()
|
|
130
|
+
return data
|
|
131
|
+
|
|
132
|
+
def dump_bytes(self, *, use_compact_list: bool = False) -> bytes:
|
|
133
|
+
"""
|
|
134
|
+
Dump the data to a bytes object; if supported.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
use_compact_list (bool, optional): whether or not to use compact lists (sparse data). Defaults to False.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
The raw bytes of the serialization of the datast.
|
|
141
|
+
"""
|
|
142
|
+
raw_data = CharPtr()
|
|
143
|
+
size = IdxC()
|
|
144
|
+
get_pgc().serializer_get_to_binary_buffer(self._serializer, int(use_compact_list), byref(raw_data), byref(size))
|
|
145
|
+
assert_no_error()
|
|
146
|
+
|
|
147
|
+
result = raw_data[: size.value]
|
|
148
|
+
if not isinstance(result, bytes):
|
|
149
|
+
raise PowerGridSerializationError("Invalid output data type")
|
|
150
|
+
|
|
151
|
+
return result
|
|
152
|
+
|
|
153
|
+
@abstractmethod
|
|
154
|
+
def dump(self, *args, **kwargs):
|
|
155
|
+
"""
|
|
156
|
+
Dump the data in the recommended data format (e.g. str or bytes, depending on the serialization_type).
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
*: any serialization_type dependent args
|
|
160
|
+
"""
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class _BytesSerializer(Serializer):
|
|
164
|
+
"""
|
|
165
|
+
Base type for serialization to bytes
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
dump = Serializer.dump_bytes
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _StringSerializer(Serializer):
|
|
172
|
+
"""
|
|
173
|
+
Base type for serialization to str
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
dump = Serializer.dump_str
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class JsonDeserializer(Deserializer):
|
|
180
|
+
"""
|
|
181
|
+
JSON deserializer for the Power grid model
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
def __new__(cls, data: str | bytes, data_filter: ComponentAttributeMapping):
|
|
185
|
+
return super().__new__(cls, data, SerializationType.JSON, data_filter=data_filter)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class MsgpackDeserializer(Deserializer):
|
|
189
|
+
"""
|
|
190
|
+
msgpack deserializer for the Power grid model
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def __new__(cls, data: bytes, data_filter: ComponentAttributeMapping):
|
|
194
|
+
return super().__new__(cls, data, SerializationType.MSGPACK, data_filter=data_filter)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class JsonSerializer(_StringSerializer):
|
|
198
|
+
"""
|
|
199
|
+
JSON deserializer for the Power grid model
|
|
200
|
+
"""
|
|
201
|
+
|
|
202
|
+
def __new__(cls, data: Dataset, dataset_type: DatasetType | None = None):
|
|
203
|
+
return super().__new__(cls, data, SerializationType.JSON, dataset_type=dataset_type)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class MsgpackSerializer(_BytesSerializer):
|
|
207
|
+
"""
|
|
208
|
+
msgpack deserializer for the Power grid model
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
def __new__(cls, data: Dataset, dataset_type: DatasetType | None = None):
|
|
212
|
+
return super().__new__(cls, data, SerializationType.MSGPACK, dataset_type=dataset_type)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def json_deserialize(
|
|
216
|
+
data: str | bytes,
|
|
217
|
+
data_filter: ComponentAttributeMapping = None,
|
|
218
|
+
) -> Dataset:
|
|
219
|
+
"""
|
|
220
|
+
Load serialized JSON data to a new dataset.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
data: the data to deserialize.
|
|
224
|
+
data_filter: the data filter to apply to the dataset.
|
|
225
|
+
|
|
226
|
+
Raises:
|
|
227
|
+
ValueError: if the data is inconsistent with the rest of the dataset or a component is unknown.
|
|
228
|
+
PowerGridError: if there was an internal error.
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.
|
|
232
|
+
"""
|
|
233
|
+
result = JsonDeserializer(data, data_filter=data_filter).load()
|
|
234
|
+
assert_no_error()
|
|
235
|
+
return result
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def json_serialize(
|
|
239
|
+
data: Dataset,
|
|
240
|
+
dataset_type: DatasetType | None = None,
|
|
241
|
+
use_compact_list: bool = False,
|
|
242
|
+
indent: int = 2,
|
|
243
|
+
) -> str:
|
|
244
|
+
"""
|
|
245
|
+
Dump data to a JSON str.
|
|
246
|
+
|
|
247
|
+
If the dataset_type is not specified or None, it will be deduced from the dataset if possible.
|
|
248
|
+
Deduction is not possible in case data is empty.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
data: the dataset
|
|
252
|
+
dataset_type: the type of the dataset. Defaults to None. Required str-type if data is empty.
|
|
253
|
+
use_compact_list: whether or not to use compact lists (sparse data). Defaults to False.
|
|
254
|
+
indent:
|
|
255
|
+
use specified indentation to make data more readable
|
|
256
|
+
Use 0 or negative value for no indentation. Defaults to 2
|
|
257
|
+
|
|
258
|
+
Raises:
|
|
259
|
+
PowerGridError: if there was an internal error.
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
A serialized string containing the dataset.
|
|
263
|
+
"""
|
|
264
|
+
data = _map_to_component_types(data)
|
|
265
|
+
if dataset_type is not None:
|
|
266
|
+
dataset_type = _str_to_datatype(dataset_type)
|
|
267
|
+
result = JsonSerializer(data=data, dataset_type=dataset_type).dump(use_compact_list=use_compact_list, indent=indent)
|
|
268
|
+
assert_no_error()
|
|
269
|
+
return result
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def msgpack_deserialize(data: bytes, data_filter: ComponentAttributeMapping = None) -> Dataset:
|
|
273
|
+
"""
|
|
274
|
+
Load serialized msgpack data to a new dataset.
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
data: the data to deserialize.
|
|
278
|
+
|
|
279
|
+
Raises:
|
|
280
|
+
ValueError: if the data is inconsistent with the rest of the dataset or a component is unknown.
|
|
281
|
+
PowerGridError: if there was an internal error.
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.
|
|
285
|
+
"""
|
|
286
|
+
result = MsgpackDeserializer(data, data_filter=data_filter).load()
|
|
287
|
+
assert_no_error()
|
|
288
|
+
return result
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def msgpack_serialize(
|
|
292
|
+
data: Dataset,
|
|
293
|
+
dataset_type: DatasetType | None = None,
|
|
294
|
+
use_compact_list: bool = False,
|
|
295
|
+
) -> bytes:
|
|
296
|
+
"""
|
|
297
|
+
Dump the data to raw msgpack bytes.
|
|
298
|
+
|
|
299
|
+
If the dataset_type is not specified or None, it will be deduced from the dataset if possible.
|
|
300
|
+
Deduction is not possible in case data is empty.
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
data: the dataset
|
|
304
|
+
dataset_type: the type of the dataset. Defaults to None. Required str-type if data is empty.
|
|
305
|
+
use_compact_list: whether or not to use compact lists (sparse data). Defaults to False.
|
|
306
|
+
|
|
307
|
+
Raises:
|
|
308
|
+
KeyError: if the dataset_type was not provided and could not be deduced from the dataset (i.e. it was empty).
|
|
309
|
+
PowerGridError: if there was an internal error.
|
|
310
|
+
|
|
311
|
+
Returns:
|
|
312
|
+
A serialized string containing the dataset.
|
|
313
|
+
"""
|
|
314
|
+
data = _map_to_component_types(data)
|
|
315
|
+
if dataset_type is not None:
|
|
316
|
+
dataset_type = _str_to_datatype(dataset_type)
|
|
317
|
+
result = MsgpackSerializer(data=data, dataset_type=dataset_type).dump(use_compact_list=use_compact_list)
|
|
318
|
+
assert_no_error()
|
|
319
|
+
return result
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
Type hints for for library-internal use.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from power_grid_model._core.dataset_definitions import ComponentType, ComponentTypeVar
|
|
10
|
+
from power_grid_model._core.enum import ComponentAttributeFilterOptions
|
|
11
|
+
|
|
12
|
+
ComponentAttributeMappingDict = dict[ComponentType, set[str] | list[str] | None | ComponentAttributeFilterOptions]
|
|
13
|
+
|
|
14
|
+
ComponentAttributeMapping = (
|
|
15
|
+
set[ComponentTypeVar]
|
|
16
|
+
| list[ComponentTypeVar]
|
|
17
|
+
| ComponentAttributeFilterOptions
|
|
18
|
+
| None
|
|
19
|
+
| ComponentAttributeMappingDict
|
|
20
|
+
)
|