watson.common.datastructures¶
-
class
watson.common.datastructures.
ImmutableDict
(*args)[source]¶ Creates an immutable dict.
While not truly immutable (_mutable can be changed), it works effectively in the same fashion.
-
class
watson.common.datastructures.
ImmutableMultiDict
(*args)[source]¶ Creates an immuatable MultiDict.
-
class
watson.common.datastructures.
MultiDict
(args=None)[source]¶ A dictionary type that can contain multiple items for a single key.
Dictionary type that will create a list of values if more than one item is set for that particular key.
Example:
multi_dict = MultiDict() multi_dict['one'] = 1 multi_dict['one'] = 'itchi' print(multi_dict) # {'one': [1, 'itchi']}
-
set
(key, value, replace=False)[source]¶ Add a new item to the dictionary.
Set the key to value on the dictionary, converting the existing value to a list if it is a string, otherwise append the value.
Parameters: - key (string) – The key used to the store the value.
- value (mixed) – The value to store.
- replace (bool) – Whether or not the value should be replaced.
Example:
multi_dict = MultiDict() multi_dict.set('item', 'value') # or multi_dict['item'] = 'value'
-
-
watson.common.datastructures.
dict_deep_update
(d1, d2)[source]¶ Recursively merge two dictionaries.
Merges two dictionaries together rather than a shallow update().
Parameters: - d1 (dict) – The original dict.
- d2 (dict) – The dict to merge with d1.
Returns: A new dict containing the merged dicts.
Return type: dict
-
watson.common.datastructures.
merge_dicts
(*dicts)[source]¶ Merges multiple dictionaries and returns a single new dict.
Unlike dict.update this will create a new dict.
Parameters: dicts (list) – The dicts that are being merged Returns: A new dict containing the merged dicts Return type: dict
-
watson.common.datastructures.
module_to_dict
(module, ignore_starts_with='')[source]¶ Load the contents of a module into a dict.
Parameters: ignore_starts_with (string) – Ignore all module keys that begin with this value. Returns: The contents of the module as a dict Return type: dict Example:
# my_module.py contents: # variable = 'value' import my_module a_dict = module_to_dict(my_module) a_dict['variable']