1from typing
import Any, Dict, Type, TypeVar, List
12 A type-safe container for metadata key-value pairs.
14 This class provides a flexible mechanism for storing and retrieving metadata:
15 - Arbitrary runtime keys (string string_values).
16 - Strongly typed keys defined in a schema.
18 Values are stored in a Python dictionary but accessed with additional
19 safety checks to ensure type correctness.
24 metadata.set_value_by_key(MetadataKey.DEVICE_FRAME_ID, 1)
25 print(metadata.get_value_by_key(MetadataKey.DEVICE_FRAME_ID)) # outputs: 1
26 print(metadata.has_entry_by_key(MetadataKey.DEVICE_FRAME_ID)) # outputs: True
28 @ingroup ids_peak_common_python_types
33 @brief Initialize an empty metadata container."""
39 Store a value using a string-based key.
41 @param key: Runtime string string_value.
42 @param value: Value to store.
44 self.
_data[key] = value
49 Check if a string key is present in the metadata.
51 @param key: The metadata key.
52 @return True if the key exists, False otherwise.
54 return key
in self.
_data
59 Retrieve a value by string key, returning None if not found.
61 @param key: The metadata key.
62 @return The stored value if found, otherwise None.
64 return self.
_data.get(key,
None)
69 Retrieve the value for a key and validate that it is of the expected type.
71 @param expected_type: The expected type of the stored value.
72 @param key: The metadata key.
73 @exception OutOfRangeException: If the key does not exist.
74 @exception InvalidCastException: If the stored type does not match.
75 @return The stored value cast to the expected type.
77 if key
not in self.
_data:
80 value = self.
_data[key]
81 if not isinstance(value, expected_type):
83 f
"Value for key '{key}' is of type {type(value).__name__}, "
84 f
"expected {expected_type.__name__}"
91 Store a value using a strongly typed metadata key.
93 @param key: A MetadataKey object containing key name and expected type.
94 @param value: The value to store.
95 @exception InvalidCastException: If the value type does not match the trait type.
97 if not isinstance(value, key.type):
99 f
"Value for key '{key.string_value}' must be of type {key.type.__name__}, "
100 f
"got {type(value).__name__}"
102 self.
_data[key.string_value] = value
107 Check if a strongly typed metadata key is present and matches the expected type.
109 @param key: A MetadataKey object.
110 @return True if the key exists and type matches, False otherwise.
112 if key.string_value
not in self.
_data:
114 return isinstance(self.
_data[key.string_value], key.type)
119 Retrieve the value of a strongly typed metadata key.
121 @param key: A MetadataKey object.
122 @exception OutOfRangeException: If the key does not exist.
123 @exception InvalidCastException: If the type does not match.
124 @return The stored value.
126 if key.string_value
not in self.
_data:
129 value = self.
_data[key.string_value]
130 if not isinstance(value, key.type):
132 f
"Value for key '{key.string_value}' is of type {type(value).__name__}, "
133 f
"expected {key.type.__name__}"
140 Returns a list of metadata key names.
142 @return A list of metadata key names
144 return list(self.
_data.keys())
149 Check if two metadata are equal.
151 @param other: Another Metadata object
153 @return True if metadata are equal, False otherwise
155 if isinstance(other, Metadata):
156 return self.
_data == other._data
162 Check if two metadata are not equal.
164 @param other: Another metadata object
166 @return True if metadata are not equal, False otherwise
168 return not self.
__eq__(other)
173 Return a human-readable string of metadata keys and values.
176 { "DeviceFrameID": 42, "DeviceTimestamp": 1695472000, "custom_data": "xyz" }
178 items = [f
'"{k}": {v!r}' for k, v
in self.
_data.items()]
180 return f
"( {', '.join(items)} )"
185 Return an unambiguous string of the Metadata object.
187 return f
"{self.__class__.__name__}{str(self)}"
Thrown when a cast to an incompatible type is attempted.
Thrown when a parameter is outside the valid range.