eoio.utils.dict_tools module#
eoio.utils.dict_tools - dictionary utility functions
- eoio.utils.dict_tools.change_type(test_dict) None[source]#
Convert value types from str to other recognised types in dictionary.
Recognised types include:
int
float
list
- datetime objects (valid string formats specified at links below)
https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat https://docs.python.org/3/library/datetime.html#datetime.time.fromisoformat
Uses eoio.utils.formatters.val_format to convert values
- Parameters:
test_dict – dictionary with values to changes
- eoio.utils.dict_tools.clean_dict(test_dict: dict, keys: list | str) None[source]#
Remove specified key/s from a dictionary
- Parameters:
test_dict – input dictionary from which to remove keys
keys – key/s to remove from the dictionary
- eoio.utils.dict_tools.create_parent_key(input_dict: dict, delim='_', to_ignore: list | str | None = None, to_add: list | str | None = None) None[source]#
Combine keys with common text under a new parent key in dictionary
- Parameters:
input_dict – dictionary to modify
delim – delimiter to use when splitting the keys, defaults to _
to_ignore – list or str of any text values to ignore as first values
to_add – list or str of any text to add to first values (things you want as a key, starting from first level with no shared keys)
- eoio.utils.dict_tools.dict_merge(input_dicts: List[dict], include_duplicates=False) dict[source]#
Merge two (or more) dictionaries together, extending common key values if dictionary values differ
- Parameters:
input_dicts – dictionaries to be merged
include_duplicates – whether or not to include duplicate key values
- Returns:
Merged dictionary.
- eoio.utils.dict_tools.empty_dict(test_dict) bool[source]#
Return whether there are empty values within a dictionary
- Parameters:
test_dict – dictionary through which to iterate
- Returns:
Truewhen empty values are present, otherwiseFalse.
- eoio.utils.dict_tools.get_dict_path(input_dict: dict, value: str, new_list: list | None = None) list[source]#
Return list of keys to get to value in input dictionary. Empty list returned if value isn’t present in dictionary
- Parameters:
input_dict – input dictionary through which to search for the value given
value – key to look for in the input dictionary
new_list – optional list to append key path to
- Returns:
List containing the key path to
value.
- eoio.utils.dict_tools.get_nested_value(input_dict: dict, keys: list)[source]#
Return a single value in a nested dictionary at the path loosely defined by the keys.
Note Useful if there are multiple values return by ‘get_value’ as you can filter the nested dictionaries that being searched
Example dictionary to search through and appropriate keys example_dict =
{“Satellite_1”: {“Bands”: {“B01”: {“Wavelength”: 10}, “B02”: { “Wavelength”: 20}}}, “Satellite_2”: {“Bands”: {“B01”: {“Wavelength”: 30}, “B02”: { “Wavelength”: 40}}}}
- example_keys =
[“Satellite_2”, “B01”, “Wavelength”]
get_nested_value(example_dict, example_keys) == 30
- Parameters:
input_dict – input dictionary through which to search
keys – list of keys ordered from out to in
- eoio.utils.dict_tools.get_value(test_dict, key, multiple=False, default: Any | None = None)[source]#
Return dictionary values associated with the specified key
- Parameters:
multiple – if multiple keys have same name within the dictionaries, specifying True will return all as a list
test_dict – input dictionary in which to search for the key-value pair/s
key – key to use to search through dictionary
- Returns:
Matching value or values associated with
key.
- eoio.utils.dict_tools.get_value_gen(test_dict: dict, key: str) Generator[source]#
Get generator function of dictionary values associated with the specified key
- Parameters:
test_dict – input iterator in which to search for the key-values pair/s
key – key to use to search through dictionary
- Returns:
Generator yielding key-value pairs matching
key.
- eoio.utils.dict_tools.key_exists(test_dict: dict, keys: str | list, include: str = 'any')[source]#
Determine whether a key (or multiple keys) are present in a nested iterable.
Default (‘any’ input) return True if any of the listed keys are in the iterable, else False. If ‘all’ input selected for ‘include’, return True if all listed keys are in iterable, else False.
- Parameters:
test_dict – iterable through which to search
keys – keys to search for in iterable
include – whether to search for ‘all’ keys or ‘any’ keys
- Returns:
Truewhen the requested key or keys are present, otherwiseFalse.
- eoio.utils.dict_tools.key_in_dict(test_dict, other_dict) bool[source]#
Return whether a key is jointly present in two input dictionaries.
Note: does not search through nested dictionary keys
- Parameters:
test_dict – first dictionary
other_dict – second dictionary
- Returns:
Truewhen any key is shared, otherwiseFalse.
- eoio.utils.dict_tools.key_present(test: dict | list, keys: str | list) bool[source]#
Determine whether a key is present in a nested iterable
- Parameters:
test – iterable through which to search
keys – keys to search for in iterable
- Returns:
Truewhen any requested key is present, otherwiseFalse.
- eoio.utils.dict_tools.list_keys(test_dict, tags=False, new_list=None)[source]#
List all keys in nested dictionary, useful when searching for keys in a Dataset dictionary
- Parameters:
test_dict – dictionary to search
tags – whether or not to include keys with ‘@’ or ‘#’ in the list
new_list – list of keys to append to, default None creates a new empty list to return
- Returns:
Sorted list of unique keys found in the nested dictionary.
- eoio.utils.dict_tools.make_value_key(input_iterable: list | dict, key: str) dict | list[source]#
Convert a list of dictionaries into a dictionary using value denoted by the key as a key name.
Example
input_iterable = [{“ID”: “name”, “value”: “John”, “units”: None}, {“ID”: “age”, “value”: “27”, “units”: “year”}] key = “ID” output = {“name”: {“value”: “John”, “units”: None}, “age”: {“value”: “27”, “units”: “year”}}
- Parameters:
input_iterable – dictionary or list to reformat
key – key of the value to use as a key for the dictionaries in a list
- eoio.utils.dict_tools.multiple_keys_present(test: dict | list, keys: str | list) bool[source]#
Determine whether all keys are present in a nested iterable
- Parameters:
test – iterable through which to search
keys – keys to search for in iterable
- Returns:
Truewhen all requested keys are present, otherwiseFalse.
- eoio.utils.dict_tools.pop_vals(test_dict, key_list, d)[source]#
Remove specified key-value pairs from dictionary and assign to a second dictionary
- Parameters:
test_dict – dictionary from which to remove the values from
key_list – keys of the values you wish to transfer to the second dictionary
d – second dictionary in which key-value pairs will be transferred
- eoio.utils.dict_tools.remove_parent_tag(input_dict: dict) None[source]#
Remove text common between a key and the key one level up from it in a nested dictionary
- Parameters:
input_dict – input dictionary to modify
- eoio.utils.dict_tools.remove_tag(test_dict: dict, tag: str, ignore: list | str | None = None, in_list: dict | None = None) None[source]#
Remove specified tag/key from dictionary and move the values up a level in the dictionary.
Note: if other keys are also present at the same level in the dictionary, tag isn’t removed unless other keys are specified as ignore
- Parameters:
test_dict – dictionary in which tags are to be removed
tag – key to remove from dictionary
ignore – list or str of keys to ignore
in_list – dictionary used within function to determine whether the tag is in the nested iterable or not
- eoio.utils.dict_tools.remove_tag_in_key(test_dict: dict, tag: str) None[source]#
Remove keys in nested dict containing the string tag
- Parameters:
test_dict – nested dictionary
tag – string tag to search for in keys