Dictionaries
dictionaries
#
Dict utility functions and classes supporting attribute access and dot-notation.
This module provides tools for working with dictionaries that allow: - Attribute-style access to dictionary keys - Conversion between nested and flattened (dot-notated) dictionaries - Safe access to nested dictionary or object fields - Recursive data cleaning of metadata dictionaries
Examples:
Functions:
Name | Description |
---|---|
cleanse_metadata |
Recursively cleanse metadata dictionaries for serialization. |
expand_dictionary |
Expand dot-notated keys into a nested dictionary. |
flatten_dictionary |
Flatten a nested dictionary using dot-notation keys. |
retrieve_nested_value |
Retrieve a value or attribute using a dot-notation path. |
AttrDict
#
Bases: dict
A dictionary that supports dot-style attribute access and nested utilities.
This class extends the built-in dict
to enable accessing keys as attributes
and includes helpers to flatten and expand nested structures.
Examples:
Methods:
Name | Description |
---|---|
from_flat_dict |
Inflate a flat dict into a nested AttrDict. |
to_flat_dict |
Return a flattened dictionary using dot-notation keys. |
Source code in src/imgtools/utils/dictionaries.py
from_flat_dict
classmethod
#
from_flat_dict(
*args: typing.Any, **kwargs: typing.Any
) -> imgtools.utils.dictionaries.AttrDict
Inflate a flat dict into a nested AttrDict.
Example
AttrDict.from_flat_dict({"a.b": 1}) {'a': {'b': 1}}
Source code in src/imgtools/utils/dictionaries.py
to_flat_dict
#
attrify
#
Recursively convert dicts to AttrDict and handle lists of dicts as well.
Source code in src/imgtools/utils/dictionaries.py
cleanse_metadata
#
cleanse_metadata(metadata: typing.Any) -> typing.Any
Recursively cleanse metadata dictionaries for serialization.
Fixes applied: 1. Converts NaN values to None. 2. Cleans nested dictionaries and iterables. 3. Converts datetime.{datetime,date,time} to ISO format strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
typing.Any
|
The input dictionary, list, or primitive. |
required |
Returns:
Type | Description |
---|---|
typing.Any
|
The cleansed version of the input. |
Source code in src/imgtools/utils/dictionaries.py
expand_dictionary
#
expand_dictionary(
flat_dict: dict[str, typing.Any],
) -> dict[str, typing.Any]
Expand dot-notated keys into a nested dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict
|
Dictionary with keys in dot-notation. |
required |
Returns:
Type | Description |
---|---|
dict
|
Nested dictionary. |
Examples:
Source code in src/imgtools/utils/dictionaries.py
flatten_dictionary
#
flatten_dictionary(
nested_dict: dict[str, typing.Any],
parent_key_prefix: str = "",
) -> dict[str, typing.Any]
Flatten a nested dictionary using dot-notation keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict
|
The nested dictionary to flatten. |
required |
|
str
|
Prefix for internal recursive use. |
''
|
Returns:
Type | Description |
---|---|
dict
|
Flattened dictionary. |
Examples:
Source code in src/imgtools/utils/dictionaries.py
retrieve_nested_value
#
retrieve_nested_value(
container: typing.Any, field_path: str
) -> typing.Any | None
Retrieve a value or attribute using a dot-notation path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
typing.Any
|
The object or dictionary to access. |
required |
|
str
|
Dot-notation string path. |
required |
Returns:
Type | Description |
---|---|
typing.Any or None
|
The resolved value or None if not found. |
Examples: