dict Module (stx.dict)
Dictionary utilities (DotDict, safe_merge) for the SciTeX ecosystem.
- class scitex.dict.DotDict(dictionary=None)[source]
Bases:
objectA dictionary-like object that allows attribute-like access (for valid identifier keys) and standard item access for all keys (including integers, etc.).
- to_dict(include_private=False)[source]
Recursively converts DotDict and nested DotDict objects back to ordinary dictionaries.
- Parameters:
include_private – If False, exclude keys starting with ‘_’ (default: False)
- __repr__()[source]
Returns a string representation suitable for debugging. Returns a nicely formatted representation that pprint can display properly. Private keys (starting with ‘_’) are hidden by default.
- pformat(indent=2, width=80, depth=None, compact=False, include_private=False)[source]
Return a pretty-formatted string representation of the DotDict.
- Parameters:
indent – Number of spaces per indentation level (default: 2)
width – Maximum line width (default: 80)
depth – Maximum depth to print (default: None for unlimited)
compact – If True, use more compact representation (default: False)
include_private – If True, include keys starting with ‘_’ (default: False)
- Returns:
Pretty-formatted string
- update(dictionary)[source]
Updates the dictionary with the key-value pairs from another dictionary or iterable.
- setdefault(key, default=None)[source]
Returns the value of the given key. If the key does not exist, insert the key with the specified default value and return the default value.
- pop(key, *args)[source]
Removes the specified key and returns the corresponding value. If key is not found, default is returned if given, otherwise KeyError is raised. Accepts optional default value like dict.pop.
- copy()[source]
Creates a shallow copy of the DotDict object. Nested DotDicts/dicts/lists will be references, not copies. Use deepcopy for a fully independent copy.
- scitex.dict.listed_dict(keys=None)[source]
Return a
defaultdict(list), optionally pre-seeded with empty lists.Examples
>>> import random >>> random.seed(42) >>> d = listed_dict() >>> for _ in range(10): ... d['a'].append(random.randint(0, 10)) >>> print(d) defaultdict(<class 'list'>, {'a': [10, 1, 0, 4, 3, 3, 2, 1, 10, 8]})
>>> import random >>> random.seed(42) >>> keys = ['a', 'b', 'c'] >>> d = listed_dict(keys) >>> for _ in range(10): ... d['a'].append(random.randint(0, 10)) ... d['b'].append(random.randint(0, 10)) ... d['c'].append(random.randint(0, 10)) >>> print(d) defaultdict(<class 'list'>, {'a': [10, 4, 2, 8, 6, 1, 8, 8, 8, 7], 'b': [1, 3, 1, 1, 0, 3, 9, 3, 6, 9], 'c': [0, 3, 10, 9, 0, 3, 0, 10, 3, 4]})
- scitex.dict.pop_keys(keys_list, keys_to_pop)[source]
Remove specified keys from a list of keys.
- Parameters:
- Returns:
A new list with the specified keys removed.
- Return type:
Example
>>> keys_list = ['a', 'b', 'c', 'd', 'e', 'bde'] >>> keys_to_pop = ['b', 'd'] >>> pop_keys(keys_list, keys_to_pop) ['a', 'c', 'e', 'bde']
- scitex.dict.safe_merge(*dicts)[source]
Merges dictionaries while checking for key conflicts.
Example
>>> dict1 = {'a': 1, 'b': 2} >>> dict2 = {'c': 3, 'd': 4} >>> safe_merge(dict1, dict2) {'a': 1, 'b': 2, 'c': 3, 'd': 4}
- Parameters:
*dicts (Dict[_Any, _Any]) – Variable number of dictionaries to merge
- Returns:
Merged dictionary
- Return type:
Dict[_Any, _Any]
- Raises:
ValueError – If overlapping keys are found between dictionaries