Skip to content

hdx.utilities.dictandlist

Dict and List utilities.

merge_two_dictionaries

def merge_two_dictionaries(a: Dict,
                           b: Dict,
                           merge_lists: bool = False) -> Dict

[view_source]

Merges b into a and returns merged result.

NOTE: tuples and arbitrary objects are not handled as it is totally ambiguous what should happen

Arguments:

  • a Dict - dictionary to merge into
  • b Dict - dictionary to merge from
  • merge_lists bool - Whether to merge lists (True) or replace lists (False). Default is False.

Returns:

  • Dict - Merged dictionary

merge_dictionaries

def merge_dictionaries(dicts: ListTuple[Dict],
                       merge_lists: bool = False) -> Dict

[view_source]

Merges all dictionaries in dicts into a single dictionary and returns result.

Arguments:

  • dicts ListTuple[Dict] - Dictionaries to merge into the first one in the list
  • merge_lists bool - Whether to merge lists (True) or replace lists (False). Default is False.

Returns:

  • Dict - Merged dictionary

dict_diff

def dict_diff(d1: Dict, d2: Dict, no_key: str = "<KEYNOTFOUND>") -> Dict

[view_source]

Compares two dictionaries.

Arguments:

  • d1 Dict - First dictionary to compare
  • d2 Dict - Second dictionary to compare
  • no_key str - What value to use if key is not found Defaults to ''.

Returns:

  • Dict - Comparison dictionary

dict_of_lists_add

def dict_of_lists_add(dictionary: Dict, key: Any, value: Any) -> None

[view_source]

Add value to a list in a dictionary by key.

Arguments:

  • dictionary Dict - Dictionary to which to add values
  • key Any - Key within dictionary
  • value Any - Value to add to list in dictionary

Returns:

None

dict_of_sets_add

def dict_of_sets_add(dictionary: Dict, key: Any, value: Any) -> None

[view_source]

Add value to a set in a dictionary by key.

Arguments:

  • dictionary Dict - Dictionary to which to add values
  • key Any - Key within dictionary
  • value Any - Value to add to set in dictionary

Returns:

None

dict_of_dicts_add

def dict_of_dicts_add(dictionary: Dict, parent_key: Any, key: Any,
                      value: Any) -> None

[view_source]

Add key value pair to a dictionary within a dictionary by key.

Arguments:

  • dictionary Dict - Dictionary to which to add values
  • parent_key Any - Key within parent dictionary
  • key Any - Key within dictionary
  • value Any - Value to add to set in dictionary

Returns:

None

list_distribute_contents_simple

def list_distribute_contents_simple(
        input_list: ListTuple,
        function: Callable[[Any], Any] = lambda x: x) -> List

[view_source]

Distribute the contents of a list eg. [1, 1, 1, 2, 2, 3] -> [1, 2, 3, 1, 2, 1]. List can contain complex types like dictionaries in which case the function can return the appropriate value eg. lambda x: x[KEY]

Arguments:

  • input_list ListTuple - List to distribute values
  • function Callable[[Any], Any] - Return value to use for distributing. Defaults to lambda x: x.

Returns:

  • List - Distributed list

list_distribute_contents

def list_distribute_contents(
        input_list: ListTuple,
        function: Callable[[Any], Any] = lambda x: x) -> List

[view_source]

Distribute the contents of a list eg. [1, 1, 1, 2, 2, 3] -> [1, 2, 1, 2, 1, 3]. List can contain complex types like dictionaries in which case the function can return the appropriate value eg. lambda x: x[KEY]

Arguments:

  • input_list ListTuple - List to distribute values
  • function Callable[[Any], Any] - Return value to use for distributing. Defaults to lambda x: x.

Returns:

  • List - Distributed list

extract_list_from_list_of_dict

def extract_list_from_list_of_dict(list_of_dict: ListTuple[Dict],
                                   key: Any) -> List

[view_source]

Extract a list by looking up key in each member of a list of dictionaries.

Arguments:

  • list_of_dict ListTuple[Dict] - List of dictionaries
  • key Any - Key to find in each dictionary

Returns:

  • List - List containing values returned from each dictionary

key_value_convert

def key_value_convert(dictin: Dict,
                      keyfn: Callable[[Any], Any] = lambda x: x,
                      valuefn: Callable[[Any], Any] = lambda x: x,
                      dropfailedkeys: bool = False,
                      dropfailedvalues: bool = False,
                      exception: Exception = ValueError) -> Dict

[view_source]

Convert keys and/or values of dictionary using functions passed in as parameters.

Arguments:

  • dictin Dict - Input dictionary
  • keyfn Callable[[Any], Any] - Function to convert keys. Defaults to lambda x: x
  • valuefn Callable[[Any], Any] - Function to convert values. Defaults to lambda x: x
  • dropfailedkeys bool - Whether to drop dictionary entries where key conversion fails. Defaults to False.
  • dropfailedvalues bool - Whether to drop dictionary entries where value conversion fails. Defaults to False.
  • exception Exception - The exception to expect if keyfn or valuefn fail. Defaults to ValueError.

Returns:

  • Dict - New dictionary with converted keys and/or values

integer_key_convert

def integer_key_convert(dictin: Dict, dropfailedkeys: bool = False) -> Dict

[view_source]

Convert keys of dictionary to integers.

Arguments:

  • dictin Dict - Input dictionary
  • dropfailedkeys bool - Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:

  • Dict - Dictionary with keys converted to integers

integer_value_convert

def integer_value_convert(dictin: Dict,
                          dropfailedvalues: bool = False) -> Dict

[view_source]

Convert values of dictionary to integers.

Arguments:

  • dictin Dict - Input dictionary
  • dropfailedvalues bool - Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:

  • Dict - Dictionary with values converted to integers

float_value_convert

def float_value_convert(dictin: Dict, dropfailedvalues: bool = False) -> Dict

[view_source]

Convert values of dictionary to floats.

Arguments:

  • dictin Dict - Input dictionary
  • dropfailedvalues bool - Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:

  • Dict - Dictionary with values converted to floats

avg_dicts

def avg_dicts(dictin1: Dict, dictin2: Dict, dropmissing: bool = True) -> Dict

[view_source]

Create a new dictionary from two dictionaries by averaging values.

Arguments:

  • dictin1 Dict - First input dictionary
  • dictin2 Dict - Second input dictionary
  • dropmissing bool - Whether to drop keys missing in one dictionary. Defaults to True.

Returns:

  • Dict - Dictionary with values being average of 2 input dictionaries

read_list_from_csv

def read_list_from_csv(url: str,
                       headers: Union[int, ListTuple[int], ListTuple[str],
                                      None] = None,
                       dict_form: bool = False,
                       **kwargs: Any) -> List[ListDict]

[view_source]

Read a list of rows in dict or list form from a csv. The headers argument is either a row number or list of row numbers (in case of multi- line headers) to be considered as headers (rows start counting at 1), or the actual headers defined as a list of strings. If not set, all rows will be treated as containing values.

Arguments:

  • url str - URL or path to read from
  • headers Union[int, ListTuple[int], ListTuple[str], None] - Row number of headers. Defaults to None.
  • dict_form bool - Return dict (requires headers parameter) or list for each row. Defaults to False (list)
  • **kwargs - Other arguments to pass to Tabulator Stream

Returns:

  • List[ListDict] - List of rows in dict or list form

write_list_to_csv

def write_list_to_csv(filepath: str,
                      rows: List[ListTupleDict],
                      headers: Union[int, ListTuple[str], None] = None,
                      columns: Union[ListTuple[int], ListTuple[str],
                                     None] = None,
                      encoding: Optional[str] = None) -> None

[view_source]

Write a list of rows in dict or list form to a csv. (The headers argument is either a row number (rows start counting at 1), or the actual headers defined as a list of strings. If not set, all rows will be treated as containing values.)

Arguments:

  • filepath str - Path to write to
  • rows List[ListTupleDict] - List of rows in dict or list form
  • headers Union[int, ListTuple[str], None] - Headers to write. Defaults to None.
  • columns Union[ListTuple[int], ListTuple[str], None] - Columns to write. Defaults to all.
  • encoding Optional[str] - Encoding to use. Defaults to None (infer encoding).

Returns:

None

args_to_dict

def args_to_dict(args: str) -> Dict

[view_source]

Convert command line arguments in a comma separated string to a dictionary.

Arguments:

  • args str - Command line arguments

Returns:

  • Dict - Dictionary of arguments