Utility Functions

Utility functions for the owimetadatabase_preprocessor package.

owimetadatabase_preprocessor.utility.utils.custom_formatwarning(message, category, filename, lineno, line=None)

Return customized warning.

owimetadatabase_preprocessor.utility.utils.dict_generator(dict_: dict[str, Any], keys_: Sequence[str] | None = None, method_: str = 'exclude') dict[str, Any]

Generate a dictionary with the specified keys.

Parameters:
  • dict – Dictionary to be filtered.

  • keys – list of keys to be included or excluded.

  • method – Method to be used for filtering. Options are “exclude” and “include”.

Returns:

Filtered dictionary.

owimetadatabase_preprocessor.utility.utils.compare_if_simple_close(a: Any, b: Any, tol: float = 1e-09) tuple[bool, None | str]

Compare two values and return a boolean and a message.

Parameters:
  • a – First value to be compared.

  • b – Second value to be compared.

  • tol – Tolerance for the comparison.

Returns:

Tuple with a result of comparison and a message if different.

owimetadatabase_preprocessor.utility.utils.check_df_eq(df1: DataFrame, df2: DataFrame, tol: float = 1e-09) bool

Check if two dataframes are equal.

Parameters:
  • df1 – First dataframe to be compared.

  • df2 – Second dataframe to be compared.

  • tol – Tolerance for the comparison.

Returns:

Boolean indicating if the dataframes are equal.

owimetadatabase_preprocessor.utility.utils.deepcompare(a: Any, b: Any, tol: float = 1e-05) tuple[bool, None | str]

Compare two complicated (potentailly nested) objects recursively and return a result and a message.

Parameters:
  • a – First object to be compared.

  • b – Second object to be compared.

  • tol – Tolerance for the comparison.

Returns:

Tuple with a result of comparison and a message if different.

owimetadatabase_preprocessor.utility.utils.fix_nan(obj: Any) Any

Replace “nan” strings with None.

Parameters:

obj – Object to be fixed.

Returns:

Fixed object.

owimetadatabase_preprocessor.utility.utils.fix_outline(data: Any) Any

Fix the outline attribute in the data.

Parameters:

data – Data to be fixed.

Returns:

Fixed data.

owimetadatabase_preprocessor.utility.utils.hex_to_dec(value: str) list[float]
owimetadatabase_preprocessor.utility.utils.hex_to_dec(value: list[str]) list[list[float]]
owimetadatabase_preprocessor.utility.utils.hex_to_dec(value: tuple[str, ...]) list[list[float]]

Convert hex color(s) to normalized [r, g, b, a] floats.

Accepts 6-digit (#rrggbb) or 8-digit (#rrggbbaa) hex strings, with or without leading ‘#’. - If value is a string: returns [r, g, b, a] - If value is a list of strings: returns [[r, g, b, a], …]

Parameters:

value – Hex color string or list of hex color strings.

Returns:

Normalized RGBA list or list of such lists.

Raises:
  • ValueError – If the hex string length is not 6 or 8.

  • TypeError – If the input type is not supported.