dic_utils
This module provides utility functions for handling nested dictionaries and YAML files.
Functions:
| Name | Description |
|---|---|
load_dic_from_path |
str, ryaml: ruamel.yaml.YAML | None = None) -> tuple[dict, ruamel.yaml.YAML]: Load a dictionary from a YAML file. |
write_dic_to_path |
dict, path: str, ryaml: ruamel.yaml.YAML | None = None) -> None: Write a dictionary to a YAML file. |
nested_get |
dict, keys: list) -> Any: Get the value from a nested dictionary using a list of keys. |
nested_set |
dict, keys: list, value: Any) -> None: Set a value in a nested dictionary using a list of keys. |
find_item_in_dic |
dict, key: str) -> Any: Find an item in a nested dictionary. |
set_item_in_dic |
dict, key: str, value: Any, found: bool = False) -> None: Set an item in a nested dictionary. |
clean_dic |
Any) -> None: Convert numpy types to standard types in a nested dictionary containing numbers and lists. |
clean_dic(o)
Convert numpy types to standard types in a nested dictionary containing number and lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o
|
Any
|
The object to convert. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in study_da/utils/dic_utils.py
find_item_in_dic(obj, key)
Find an item in a nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict
|
The nested dictionary. |
required |
key
|
str
|
The key to find in the nested dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The value corresponding to the key in the nested dictionary. |
Source code in study_da/utils/dic_utils.py
load_dic_from_path(path, ryaml=None)
Load a dictionary from a yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the yaml file. |
required |
ryaml
|
YAML
|
The yaml reader. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, YAML]
|
tuple[dict, ruamel.yaml.YAML]: The dictionary and the yaml reader. |
Source code in study_da/utils/dic_utils.py
nested_get(dic, keys)
Get the value from a nested dictionary using a list of keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dic
|
dict
|
The nested dictionary. |
required |
keys
|
list
|
The list of keys to traverse the nested dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The value corresponding to the keys in the nested dictionary. |
Source code in study_da/utils/dic_utils.py
nested_set(dic, keys, value)
Set a value in a nested dictionary using a list of keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dic
|
dict
|
The nested dictionary. |
required |
keys
|
list
|
The list of keys to traverse the nested dictionary. |
required |
value
|
Any
|
The value to set in the nested dictionary. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in study_da/utils/dic_utils.py
set_item_in_dic(obj, key, value, found=False)
Set an item in a nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict
|
The nested dictionary. |
required |
key
|
str
|
The key to set in the nested dictionary. |
required |
value
|
Any
|
The value to set in the nested dictionary. |
required |
found
|
bool
|
Whether the key has been found in the nested dictionary. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in study_da/utils/dic_utils.py
write_dic_to_path(dic, path, ryaml=None)
Write a dictionary to a yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dic
|
dict
|
The dictionary to write. |
required |
path
|
str
|
The path to the yaml file. |
required |
ryaml
|
YAML
|
The yaml reader. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |