scitex_core.dict
Scitex Core dict module.
- class scitex_core.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.).
- __dir__()[source]
Provides attribute suggestions for dir() and tab completion. Includes both standard methods/attributes and the keys stored in _data.
- __repr__()[source]
Returns a string representation suitable for debugging. Shows the class name and the internal data representation.
- 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.
- 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.
- 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.
- to_dict()[source]
Recursively converts DotDict and nested DotDict objects back to ordinary dictionaries.
- scitex_core.dict.listed_dict(keys=None)[source]
- Example 1:
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]})
- Example 2:
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_core.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_core.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