hdx.utilities.dictandlist
Dict and List utilities.
merge_two_dictionaries
def merge_two_dictionaries(a: Dict,
b: Dict,
merge_lists: bool = False) -> Dict
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 intob
Dict - dictionary to merge frommerge_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
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 listmerge_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
Compares two dictionaries.
Arguments:
d1
Dict - First dictionary to compared2
Dict - Second dictionary to compareno_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
Add value to a list in a dictionary by key.
Arguments:
dictionary
Dict - Dictionary to which to add valueskey
Any - Key within dictionaryvalue
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
Add value to a set in a dictionary by key.
Arguments:
dictionary
Dict - Dictionary to which to add valueskey
Any - Key within dictionaryvalue
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
Add key value pair to a dictionary within a dictionary by key.
Arguments:
dictionary
Dict - Dictionary to which to add valuesparent_key
Any - Key within parent dictionarykey
Any - Key within dictionaryvalue
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
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 valuesfunction
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
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 valuesfunction
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
Extract a list by looking up key in each member of a list of dictionaries.
Arguments:
list_of_dict
ListTuple[Dict] - List of dictionarieskey
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
Convert keys and/or values of dictionary using functions passed in as parameters.
Arguments:
dictin
Dict - Input dictionarykeyfn
Callable[[Any], Any] - Function to convert keys. Defaults to lambda x: xvaluefn
Callable[[Any], Any] - Function to convert values. Defaults to lambda x: xdropfailedkeys
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
Convert keys of dictionary to integers.
Arguments:
dictin
Dict - Input dictionarydropfailedkeys
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
Convert values of dictionary to integers.
Arguments:
dictin
Dict - Input dictionarydropfailedvalues
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
Convert values of dictionary to floats.
Arguments:
dictin
Dict - Input dictionarydropfailedvalues
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
Create a new dictionary from two dictionaries by averaging values.
Arguments:
dictin1
Dict - First input dictionarydictin2
Dict - Second input dictionarydropmissing
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]
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 fromheaders
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
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 torows
List[ListTupleDict] - List of rows in dict or list formheaders
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
Convert command line arguments in a comma separated string to a dictionary.
Arguments:
args
str - Command line arguments
Returns:
Dict
- Dictionary of arguments