1from __future__
import annotations
3from typing
import Any, Dict, List, Type, TypeAlias
8T: TypeAlias = IArchive.T
14 A key-value store with type checking, based on a dict.
17 - bool, int, float, str
19 - Lists of the above (with homogeneous element types)
23 from ids_peak_icv.serialization import Archive
28 archive.set("B", "Text")
29 archive.set("C", [1, 2, 3])
31 sub_archive = Archive()
32 sub_archive.set("a", 1.2)
33 sub_archive.set("b", True)
34 archive.set("D", sub_archive)
36 @since ids_peak_icv 1.0
38 @ingroup ids_peak_icv_python_serialization
40 AllowedValue = IArchive.AllowedValue | IArchive
42 def __init__(self, data: Dict[str, Archive.AllowedValue] |
None =
None) ->
None:
45 Initializes the archive with optional initial data.
47 If ``data`` is provided,
48 all entries are validated for supported types
49 before the dict is stored by reference.
50 If ``data`` is ``None``,
51 the archive is initially empty.
53 @param data: Optional dictionary of initial key-value pairs.
55 @exception NotSupportedException: An entry in data has an unsupported type.
57 @since ids_peak_icv 1.0
62 for key, value
in data.items():
65 f
"Unsupported value type for key '{key}': {type(value).__name__}"
70 def set(self, key: str, value: AllowedValue) ->
None:
73 Stores a value under the specified key.
75 @param key: The key under which to store the value.
76 @param value: The value to store (restricted types only).
78 @exception NotSupportedException: The value has an unsupported type.
80 @since ids_peak_icv 1.0
86 f
"Unsupported value type: {type(value).__name__}"
89 def get(self, key: str, datatype: Type[T] |
None) -> T:
92 Returns the value stored under the specified key.
94 @param key: Key of the value to retrieve.
95 @param datatype: The expected datatype of the value.
96 If None is given, the type check is not executed.
98 @return The stored value.
100 @exception CorruptedException: If the key is not found
101 or the specified datatype is not the type of the stored value.
103 @since ids_peak_icv 1.0
110 if datatype
is not type(value)
and datatype
is not None:
111 raise CorruptedException(f
"The value of the specified key '{key}' is not of the expected datatype.")
118 Returns the archive stored under the given key.
120 @param key: Key of the archive to retrieve.
122 @return The stored archive.
124 @exception CorruptedException: The key is not found
125 or the specified datatype is not of type ``IArchive``.
127 @since ids_peak_icv 1.0
133 if not isinstance(value, IArchive):
134 raise CorruptedException(f
"The value of the specified key '{key}' is not of the expected datatype.")
138 def has(self, key: str) -> bool:
141 Checks whether a given key exists in the archive.
143 @param key: The key to check.
145 @return True if the key exists, False otherwise.
147 @since ids_peak_icv 1.0
154 Returns all keys currently stored in the archive.
156 @return List of keys.
158 @since ids_peak_icv 1.0
165 Creates a new, empty archive instance.
167 @since ids_peak_icv 1.0
174 A string representation of the archive.
176 @since ids_peak_icv 1.0
183 @since ids_peak_icv 1.0
185 return f
"Archive({str(self)})"
190 @since ids_peak_icv 1.0
192 if not isinstance(other, Archive):
207 @since ids_peak_icv 1.0
209 if isinstance(lhs, Archive)
and isinstance(rhs, Archive):
210 return bool(lhs == rhs)
212 if isinstance(lhs, list)
and isinstance(rhs, list):
213 if len(lhs) != len(rhs):
215 return all(self.
_compare_values(val1, val2)
for val1, val2
in zip(lhs, rhs))
217 return bool(lhs == rhs)
219 _ALLOWED_SCALARS = (bool, int, float, str, IArchive)
224 Returns whether the value’s type is supported.
226 @param value: A value or a list whose type is validated.
228 @return `True` if the type supported, `False` otherwise.
230 @since ids_peak_icv 1.0
234 if isinstance(value, list):
237 first_type = type(value[0])
238 if all(isinstance(v, first_type)
for v
in value):
Simplified interface for a hierarchical key-value archive system.
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Thrown when a requested operation is not implemented or supported.
A key-value store with type checking, based on a dict.
T get(self, str key, Type[T]|None datatype)
Returns the value stored under the specified key.
IArchive create_archive(self)
Creates a new, empty archive instance.
None __init__(self, Dict[str, Archive.AllowedValue]|None data=None)
Initializes the archive with optional initial data.
IArchive get_archive(self, str key)
Returns the archive stored under the given key.
List[str] keys(self)
Returns all keys currently stored in the archive.
None set(self, str key, AllowedValue value)
Stores a value under the specified key.
bool _is_supported_type(self, Any value)
Returns whether the value’s type is supported.
bool _compare_values(self, Any lhs, Any rhs)
bool has(self, str key)
Checks whether a given key exists in the archive.
str __str__(self)
A string representation of the archive.
bool __eq__(self, Any other)