Dict Utilities

smarter.common.utils.dict

Module providing dictionary utility functions for the Smarter framework.

This module includes helper functions for:

  • Recursively sorting dictionaries by key for deterministic output.

  • Checking if all keys and values in one dictionary are present in another (deep containment).

  • Recursively verifying if a dictionary or list is a subset of another.

These utilities are useful for testing, serialization, comparison, and data validation tasks.

Functions

  • recursive_sort_dict(d): Recursively sorts a dictionary by its keys.

  • dict_is_contained_in(dict1, dict2): Checks if all keys and values in dict1 are present in dict2, recursively.

  • dict_is_subset(small, big): Recursively checks that all items in the dictionary or list ‘small’ exist in ‘big’.

Example

from smarter.common.utils import recursive_sort_dict, dict_is_contained_in, dict_is_subset

d = {"b": 2, "a": {"d": 4, "c": 3}}
sorted_d = recursive_sort_dict(d)

model = {"name": "Alice", "profile": {"age": 30}}
test = {"name": "Alice", "profile": {"age": 30}, "extra": "value"}
result = dict_is_contained_in(model, test)

big = {"roles": ["admin", "user"]}
small = {"roles": ["admin"]}
result = dict_is_subset(small, big)
smarter.common.utils.dict.dict_is_contained_in(dict1, dict2)[source]

Checks whether all keys and values in dict1 are present in dict2, recursively.

Parameters:
  • dict1 (dict) – The dictionary whose keys and values are to be checked for containment.

  • dict2 (dict) – The dictionary in which to check for the presence of keys and values from dict1.

Returns:

Returns True if every key in dict1 exists in dict2 and the corresponding values match (including nested dictionaries). Returns False otherwise.

Return type:

bool

Example usage:

from smarter.common.utils import dict_is_contained_in

model = {
    "name": "Alice",
    "profile": {
        "age": 30,
        "city": "Wonderland"
    }
}

test = {
    "name": "Alice",
    "profile": {
        "age": 30,
        "city": "Wonderland"
    },
    "extra": "value"
}

result = dict_is_contained_in(model, test)
print(result)  # True

# Example with missing key
test_missing = {
    "name": "Alice"
}
result = dict_is_contained_in(model, test_missing)
print(result)  # False
smarter.common.utils.dict.dict_is_subset(small, big)[source]

Recursively checks that all items in the dictionary small exist in the dictionary big.

Parameters:
  • small (dict or list) – The dictionary (or list) whose items should be checked for existence in big.

  • big (dict or list) – The dictionary (or list) in which to check for the presence of items from small.

Returns:

Returns True if every item in small exists in big (including nested dictionaries and lists). Returns False otherwise.

Return type:

bool

Note

  • For dictionaries, all keys and their corresponding values must exist in big.

  • For lists, all elements in small must be present in big; order does not matter.

  • Nested dictionaries and lists are checked recursively.

Example usage:

from smarter.common.utils import dict_is_subset

big = {
    "name": "Alice",
    "profile": {
        "age": 30,
        "city": "Wonderland"
    },
    "roles": ["admin", "user"]
}

small = {
    "profile": {
        "age": 30
    },
    "roles": ["admin"]
}

result = dict_is_subset(small, big)
print(result)  # True

# Example with missing value
small_missing = {
    "profile": {
        "age": 31
    }
}
result = dict_is_subset(small_missing, big)
print(result)  # False
smarter.common.utils.dict.recursive_sort_dict(d)[source]

Recursively sorts a dictionary by its keys.

Parameters:

d (dict) – The input dictionary to be sorted. Nested dictionaries are also sorted recursively.

Returns:

A new dictionary with all keys sorted in ascending order. If a value is itself a dictionary, it is also sorted recursively.

Return type:

dict

Example usage:

from smarter.common.utils import recursive_sort_dict

data = {
    "b": 2,
    "a": {
        "d": 4,
        "c": 3
    }
}

sorted_data = recursive_sort_dict(data)
print(sorted_data)
# Output: {'a': {'c': 3, 'd': 4}, 'b': 2}