Skip to content

main.dict

Classes

IterateCombineExtendDict

IterateCombineExtendDict()

Combine and extend nested dictionaries with list values.

This class processes nested dictionaries where the deepest level contains lists, and combines all lists into a single flat list.

Initialize the dictionary combiner.

Source code in pyadvtools/main/dict.py
def __init__(self) -> None:
    """Initialize the dictionary combiner."""
    pass

Functions

data_combine staticmethod
data_combine(old)

Combine all list values from dictionary.

Parameters:

Name Type Description Default
old dict[str, Any]

Dictionary with list values to combine.

required

Returns:

Type Description
list[Any]

List[Any]: Combined list of all values.

Source code in pyadvtools/main/dict.py
@staticmethod
def data_combine(old: dict[str, Any]) -> list[Any]:
    """Combine all list values from dictionary.

    Args:
        old: Dictionary with list values to combine.

    Returns:
        List[Any]: Combined list of all values.
    """
    data_list = []
    for key in old:
        data_list.extend(old[key])
    return data_list
dict_update
dict_update(data_dict)

Update and combine nested dictionary lists.

Processes a nested dictionary structure and combines all list values from the deepest level into a single flat list.

Parameters:

Name Type Description Default
data_dict dict[str, Any]

Nested dictionary with list values at deepest level.

required

Returns:

Type Description
list[Any]

List[Any]: Combined list of all values from nested structure.

Source code in pyadvtools/main/dict.py
def dict_update(self, data_dict: dict[str, Any]) -> list[Any]:
    """Update and combine nested dictionary lists.

    Processes a nested dictionary structure and combines all list values
    from the deepest level into a single flat list.

    Args:
        data_dict: Nested dictionary with list values at deepest level.

    Returns:
        List[Any]: Combined list of all values from nested structure.
    """
    data_dict = self.dict_update_iteration(copy.deepcopy(data_dict))
    data_list = self.data_combine(data_dict)
    return data_list
dict_update_iteration
dict_update_iteration(old)

Recursively process nested dictionary structure.

Parameters:

Name Type Description Default
old dict[str, Any]

Dictionary to process recursively.

required

Returns:

Type Description
dict[str, Any]

Dict[str, Any]: Processed dictionary structure.

Source code in pyadvtools/main/dict.py
def dict_update_iteration(self, old: dict[str, Any]) -> dict[str, Any]:
    """Recursively process nested dictionary structure.

    Args:
        old: Dictionary to process recursively.

    Returns:
        Dict[str, Any]: Processed dictionary structure.
    """
    for key in old:
        if isinstance(old[key], dict):
            old[key] = self.dict_update(old[key])
    return old

IterateSortDict

IterateSortDict(reverse=False)

Recursively sort dictionary keys using natural string ordering.

This class provides functionality to recursively sort dictionary keys at all levels of nesting using natural string ordering that handles embedded numbers properly.

Attributes:

Name Type Description
reverse

If True, sorts keys in descending order.

Initialize the dictionary sorter.

Parameters:

Name Type Description Default
reverse bool

If True, sorts keys in descending order.

False
Source code in pyadvtools/main/dict.py
def __init__(self, reverse: bool = False) -> None:
    """Initialize the dictionary sorter.

    Args:
        reverse: If True, sorts keys in descending order.
    """
    self.reverse = reverse

Functions

dict_sort
dict_sort(old)

Sort dictionary keys using natural string ordering.

Parameters:

Name Type Description Default
old dict

Dictionary to sort.

required

Returns:

Name Type Description
dict dict

Dictionary with keys sorted.

Source code in pyadvtools/main/dict.py
def dict_sort(self, old: dict) -> dict:
    """Sort dictionary keys using natural string ordering.

    Args:
        old: Dictionary to sort.

    Returns:
        dict: Dictionary with keys sorted.
    """
    return {k: old[k] for k in sort_int_str(list(old.keys()), self.reverse)}
dict_sort_iteration
dict_sort_iteration(old)

Recursively sort nested dictionary keys.

Parameters:

Name Type Description Default
old dict

Dictionary to sort recursively.

required

Returns:

Name Type Description
dict dict

Dictionary with nested keys sorted.

Source code in pyadvtools/main/dict.py
def dict_sort_iteration(self, old: dict) -> dict:
    """Recursively sort nested dictionary keys.

    Args:
        old: Dictionary to sort recursively.

    Returns:
        dict: Dictionary with nested keys sorted.
    """
    for key in old:
        if isinstance(old[key], dict):
            old[key] = self.dict_update(old[key])
    return old
dict_update
dict_update(old)

Update and sort a dictionary recursively.

Sorts the dictionary keys at all levels using natural string ordering.

Parameters:

Name Type Description Default
old dict

Dictionary to sort.

required

Returns:

Name Type Description
dict dict

Dictionary with all keys sorted recursively.

Source code in pyadvtools/main/dict.py
def dict_update(self, old: dict) -> dict:
    """Update and sort a dictionary recursively.

    Sorts the dictionary keys at all levels using natural string ordering.

    Args:
        old: Dictionary to sort.

    Returns:
        dict: Dictionary with all keys sorted recursively.
    """
    old = self.dict_sort_iteration(old)
    old = self.dict_sort(old)
    return old

IterateUpdateDict

IterateUpdateDict()

Recursively update nested dictionaries.

This class provides functionality to recursively update nested dictionaries, merging new values into existing structures while preserving nested dictionary hierarchies.

Initialize the dictionary updater.

Source code in pyadvtools/main/dict.py
def __init__(self) -> None:
    """Initialize the dictionary updater."""
    pass

Functions

dict_add staticmethod
dict_add(old, new)

Add new keys to dictionary.

Adds keys from new dictionary that don't exist in old dictionary.

Parameters:

Name Type Description Default
old dict

Dictionary to add keys to.

required
new dict

Dictionary containing keys to add.

required

Returns:

Name Type Description
dict dict

Dictionary with added keys.

Source code in pyadvtools/main/dict.py
@staticmethod
def dict_add(old: dict, new: dict) -> dict:
    """Add new keys to dictionary.

    Adds keys from new dictionary that don't exist in old dictionary.

    Args:
        old: Dictionary to add keys to.
        new: Dictionary containing keys to add.

    Returns:
        dict: Dictionary with added keys.
    """
    for key in new:
        if key not in old:
            old[key] = new[key]
    return old
dict_update
dict_update(old, new)

Update a dictionary with new values recursively.

Merges new dictionary values into the old dictionary, handling nested dictionaries by recursively updating them.

Parameters:

Name Type Description Default
old dict

Original dictionary to update.

required
new dict

Dictionary containing new values to merge.

required

Returns:

Name Type Description
dict dict

Updated dictionary with merged values.

Source code in pyadvtools/main/dict.py
def dict_update(self, old: dict, new: dict) -> dict:
    """Update a dictionary with new values recursively.

    Merges new dictionary values into the old dictionary, handling
    nested dictionaries by recursively updating them.

    Args:
        old: Original dictionary to update.
        new: Dictionary containing new values to merge.

    Returns:
        dict: Updated dictionary with merged values.
    """
    old = self.dict_update_iteration(old, new)
    old = self.dict_add(old, new)
    return old
dict_update_iteration
dict_update_iteration(old, new)

Recursively update nested dictionary values.

Parameters:

Name Type Description Default
old dict

Original dictionary to update.

required
new dict

Dictionary with new values.

required

Returns:

Name Type Description
dict dict

Dictionary with recursively updated values.

Source code in pyadvtools/main/dict.py
def dict_update_iteration(self, old: dict, new: dict) -> dict:
    """Recursively update nested dictionary values.

    Args:
        old: Original dictionary to update.
        new: Dictionary with new values.

    Returns:
        dict: Dictionary with recursively updated values.
    """
    for key in old:
        if key not in new:
            continue

        if isinstance(old[key], dict) and isinstance(new[key], dict):
            old[key] = self.dict_update(old[key], new[key])
        else:
            old[key] = new[key]  # update

    return old

Functions