wadi.utils module

wadi.utils.check_arg(arg, valid_args)

Function that checks if an argument is an element of a list of valid arguments.

Parameters:
  • arg (str) – The string to be checked.

  • valid_args (iterable) – List or other iterable with valid arguments.

Returns:

result – The first element of a list with the elements in valid_args that produced a match for arg.

Return type:

str

Raises:

IndexError – When no match was found.

Notes

Uses a regular expression that checks if the argument appears at the start of a string (case-insensitive).

wadi.utils.check_arg_list(arg_list, valid_args)

Function that checks if the elements of a list of arguments are in a list of valid arguments by calling the function check_arg.

Parameters:
  • arg_list (str or iterable) – List or other iterable with arguments to be checked. If a str type is passed then it is converted to a list.

  • valid_args (iterable) – List or other iterable with valid arguments.

Returns:

result – List with elements from valid_args that produced a match for the elements in arg_list.

Return type:

list

Raises:

IndexError – When no match was found for any of the elements in arg_list.

wadi.utils.check_if_nested_list(n_list, min_elements=2)

Function that checks if all elements of a list are lists with a minimum number of elements.

Parameters:
  • n_list (list) – The list to be checked.

  • min_elements (int) – The minimum number of elements that the list(s) within n_list must consist of.

Raises:
  • TypeError – When n_list or any of its elements is not a list.

  • ValueError – When a list within n_list contains less than min_elements elements.

wadi.utils.parse_name_and_units(s)

This function attempts to extract the feature name and units from a string. It tries to split the string at the first space character. The first item before the space is the feature name (verbatim). If the part after the space contains any parentheses, brackets or accolades these are stripped off and the remaining part is considered to contain the units. For example, it will correctly recognize the following formats: ‘Ca’, ‘Mg mg/l’, ‘Na (mg/l)’, ‘NO3 [mg N/l]’, ‘SO4 {mg/l S}’. The units for ‘Ca tot mg/l’ will be incorrectly inferred because the string contains two spaces.

Parameters:

s (str) – A string from which the name and units must be extracted.

Returns:

  • name (str) – The feature name extracted from ‘s’. For the example strings above, the resulting names will be ‘Ca’, ‘Mg’, ‘Na’, ‘NO3’, ‘SO4’ and ‘Ca’.

  • units (str) – The units extracted from ‘s’. For the example strings above, the corresponding units will be ‘’, ‘mg/l’, ‘mg/l’, ‘mg N/l’, ‘mg/l S’ and ‘tot mg/l’.

class wadi.utils.StringList(initlist=None)

Bases: UserList

Class with convenience methods for lists of strings.

__init__(initlist=None)

Class initialization method. Ensures that all items are of type str.

Parameters:

initlist (list) – List of items to add to the StringList.

replace_strings(r_dict)

This method modifies the elements of the StringList by performing a search and replace based on the elements in r_dict.

Parameters:

r_dict (dict) – Dictionary of which the keys are the strings to search for and the values are the strings to replace the search values with.

Raises:

TypeError – When r_dict is not a dictionary.

strip()

This method removes all leading or trailing whitespace from the string items in the list.

strip_parentheses()

This method modifies the elements of the StringList by removing all characters between parentheses and the parentheses themselves.

tidy_strings()

This method modifies the elements of the StringList by (i) turning all characters to lowercase (ii) stripping all non-ASCII characters and (iii) removing all characters that are not letters, numbers or whitespace.

wadi.utils.valid_kwargs(f, **kwargs)

This function checks if one or more function arguments are valid keyword arguments for callable ‘f’.

Parameters:
  • f (callable function) – Function for which the keyword arguments must be checked.

  • kwargs (dict) – Dictionary of keyword arguments.

Returns:

result – Dictionary with only the valid keyword arguments passed in kwargs.

Return type:

dict

wadi.utils.fuzzy_min_score(s)

This function calculates the minimum score required for a valid match in fuzzywuzzy’s extractOne function. The minimum score depends on the length of ‘s’ and is calculated based on the string lengths and scores in the DEFAULT_MINSCORES dictionary.

Parameters:

s (str) – String for which the minimum score must be determined.

Returns:

result – The minimum score for ‘s’.

Return type:

float

wadi.utils._wadi_style_warning(message)

This function formats the warning messages created when warnings.warn is called from within the _warn method of WadiBaseClass. Note that this function has prescribed kwargs, see https://docs.python.org/3/library/warnings.html#warnings.formatwarning

Parameters:

message (str) – The warning message to be displayed.