nexusLIMS.utils.dicts#
Dictionary manipulation utilities for NexusLIMS.
Module Contents#
Functions#
Get a nested dictionary value by path. |
|
Set a nested dictionary value by path. |
|
Try to get a nested dictionary value. |
|
Recursively sort a dictionary by keys. |
|
Remove an element from a DictionaryTreeBrowser by setting it to None. |
|
Delete keys with a value of |
API#
- nexusLIMS.utils.dicts.get_nested_dict_value_by_path(nest_dict, path)[source]#
Get a nested dictionary value by path.
Get the value from within a nested dictionary structure by traversing into the dictionary as deep as that path found and returning that value.
Uses python-benedict for robust nested dictionary operations.
- Parameters:
- Returns:
value – The value at the path within the nested dictionary; if there’s no value there, return None
- Return type:
object or None
- nexusLIMS.utils.dicts.set_nested_dict_value(nest_dict, path, value)[source]#
Set a nested dictionary value by path.
Set a value within a nested dictionary structure by traversing into the dictionary as deep as that path found and changing it to
value.Uses python-benedict for robust nested dictionary operations.
- Parameters:
- Returns:
value – The value at the path within the nested dictionary
- Return type:
- nexusLIMS.utils.dicts.try_getting_dict_value(dict_, key)[source]#
Try to get a nested dictionary value.
This method will try to get a value from a dictionary (potentially nested) and fail silently if the value is not found, returning None.
- Parameters:
- Returns:
val – The value of the dictionary specified by
key. If the dictionary does not have a key, returns None without raising an error- Return type:
object or None
- nexusLIMS.utils.dicts.remove_dtb_element(tree, path)[source]#
Remove an element from a DictionaryTreeBrowser by setting it to None.
Helper method that sets a specific leaf of a DictionaryTreeBrowser to None. Use with
remove_dict_nones()to fully remove the desired DTB element after setting it to None (after converting DTB to dictionary).- Parameters:
tree (
DictionaryTreeBrowser) – theDictionaryTreeBrowserobject to remove the object frompath (str) – period-delimited path to a DTB element
- Returns:
tree
- Return type:
- nexusLIMS.utils.dicts.remove_dict_nones(dictionary: Dict[Any, Any]) Dict[Any, Any][source]#
Delete keys with a value of
Nonein a dictionary, recursively.Taken from https://stackoverflow.com/a/4256027.
- Parameters:
dictionary – The dictionary, with keys that have None values removed
- Returns:
The same dictionary, but with “Nones” removed
- Return type: