nexusLIMS.utils.dicts#

Dictionary manipulation utilities for NexusLIMS.

Module Contents#

Functions#

get_nested_dict_value_by_path

Get a nested dictionary value by path.

set_nested_dict_value

Set a nested dictionary value by path.

try_getting_dict_value

Try to get a nested dictionary value.

sort_dict

Recursively sort a dictionary by keys.

remove_dtb_element

Remove an element from a DictionaryTreeBrowser by setting it to None.

remove_dict_nones

Delete keys with a value of None in a dictionary, recursively.

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:
  • nest_dict (dict) – A dictionary of dictionaries that is to be queried

  • path (tuple) – A tuple (or other iterable type) that specifies the subsequent keys needed to get to a a value within nest_dict

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:
  • nest_dict (dict) – A dictionary of dictionaries that is to be queried

  • path (tuple) – A tuple (or other iterable type) that specifies the subsequent keys needed to get to a a value within nest_dict

  • value (object) – The value which will be given to the path in the nested dictionary

Returns:

value – The value at the path within the nested dictionary

Return type:

object

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:
  • dict_ (dict) – The dictionary from which to get a value

  • key (str or tuple) – The key to query, or if an iterable container type (tuple, list, etc.) is given, the path into a nested dictionary to follow

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.sort_dict(item)[source]#

Recursively sort a dictionary by keys.

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) – the DictionaryTreeBrowser object to remove the object from

  • path (str) – period-delimited path to a DTB element

Returns:

tree

Return type:

DictionaryTreeBrowser

nexusLIMS.utils.dicts.remove_dict_nones(dictionary: Dict[Any, Any]) Dict[Any, Any][source]#

Delete keys with a value of None in 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:

dict