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