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
dict1are present indict2, recursively.- Parameters:
- Returns:
Returns
Trueif every key indict1exists indict2and the corresponding values match (including nested dictionaries). ReturnsFalseotherwise.- Return type:
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
smallexist in the dictionarybig.- Parameters:
- Returns:
Returns
Trueif every item insmallexists inbig(including nested dictionaries and lists). ReturnsFalseotherwise.- Return type:
Note
For dictionaries, all keys and their corresponding values must exist in
big.For lists, all elements in
smallmust be present inbig; 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:
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}