akkudoktoreos.core.pydantic.PydanticModelNestedValueMixin
- class akkudoktoreos.core.pydantic.PydanticModelNestedValueMixin
Bases:
objectA mixin providing methods to get, set and track nested values within a Pydantic model.
The methods use a ‘/’-separated path to denote the nested values. Supports handling Optional, List, and Dict types, ensuring correct initialization of missing attributes.
Example
- class Address(PydanticBaseModel):
city: str
- class User(PydanticBaseModel):
name: str address: Address
- def on_city_change(old, new, path):
print(f”{path}: {old} -> {new}”)
user = User(name=”Alice”, address=Address(city=”NY”)) user.track_nested_value(“address/city”, on_city_change) user.set_nested_value(“address/city”, “LA”) # triggers callback
- __init__()
Methods
__init__()get_nested_value(path)Retrieve a nested value from the model using a '/'-separated path.
set_nested_value(path, value)Set a nested value in the model using a '/'-separated path.
track_nested_value(path, callback)Register a callback for a specific path (or subtree).
- track_nested_value(path: str, callback: Callable[[Any, str, Any, Any], None]) None
Register a callback for a specific path (or subtree).
Callback triggers if set path is equal or deeper.
- Parameters:
path (str) – ‘/’-separated path to track.
callback (callable) – Function called as callback(model_instance, set_path, old_value, new_value).
- get_nested_value(path: str) Any
Retrieve a nested value from the model using a ‘/’-separated path.
Supports accessing nested attributes and list indices.
- Parameters:
path (str) – A ‘/’-separated path to the nested attribute (e.g., “key1/key2/0”).
- Returns:
The retrieved value.
- Return type:
Any
- Raises:
KeyError – If a key is not found in the model.
IndexError – If a list index is out of bounds or invalid.
Example
```python class Address(PydanticBaseModel):
city: str
- class User(PydanticBaseModel):
name: str address: Address
user = User(name=”Alice”, address=Address(city=”New York”)) city = user.get_nested_value(“address/city”) print(city) # Output: “New York” ```
- set_nested_value(path: str, value: Any) None
Set a nested value in the model using a ‘/’-separated path.
Supports modifying nested attributes and list indices while preserving Pydantic validation. Automatically initializes missing Optional, Union, dict, and list fields if necessary. If a missing field cannot be initialized, raises an exception.
Triggers the callbacks registered by track_nested_value().
- Parameters:
path (str) – A ‘/’-separated path to the nested attribute (e.g., “key1/key2/0”).
value (Any) – The new value to set.
- Raises:
KeyError – If a key is not found in the model.
IndexError – If a list index is out of bounds or invalid.
ValueError – If a validation error occurs.
TypeError – If a missing field cannot be initialized.
Example
```python class Address(PydanticBaseModel):
city: Optional[str]
- class User(PydanticBaseModel):
name: str address: Optional[Address] settings: Optional[Dict[str, Any]]
user = User(name=”Alice”, address=None, settings=None) user.set_nested_value(“address/city”, “Los Angeles”) user.set_nested_value(“settings/theme”, “dark”)
print(user.address.city) # Output: “Los Angeles” print(user.settings) # Output: {‘theme’: ‘dark’} ```