From 86916008cc0432f0e9cbdda6ed081bd44644e876 Mon Sep 17 00:00:00 2001 From: Andrew Wilson Date: Wed, 11 Sep 2024 10:15:40 +1200 Subject: [PATCH] Move an IDW helper into a new module, as it's not specifically YAML-related --- src/profiles/idw/idw_utils.py | 45 +++++++++++++++++++++++++++++++++ src/profiles/idw/yaml_helper.py | 42 ------------------------------ 2 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 src/profiles/idw/idw_utils.py diff --git a/src/profiles/idw/idw_utils.py b/src/profiles/idw/idw_utils.py new file mode 100644 index 00000000..0f3df991 --- /dev/null +++ b/src/profiles/idw/idw_utils.py @@ -0,0 +1,45 @@ +"""Helpers for use in the IDW profile.""" + +from typing import Any, Dict, List, Union + + +def replace_micrometer_values( + data: Union[Dict[Any, Any], List[Any]], replacement: str +) -> Union[Dict[Any, Any], List[Any]]: + """Recursively replace micrometer values in a dictionary or list. + + This function searches for string values in dictionaries or lists that end with "µm" + (micrometers) and replaces them with the specified replacement string. + + Args: + data (Union[Dict, List]): The dictionary or list containing values to check and replace. + replacement (str): The replacement string for micrometer values. + + Returns: + Union[Dict, List]: The input data with micrometer values replaced. + """ + if isinstance(data, dict): + for key, value in data.items(): + data[key] = ( + replace_micrometer_values(value, replacement) + if isinstance(value, (dict, list)) + else ( + value[:-2] + replacement + if isinstance(value, str) and value.endswith("µm") + else value + ) + ) + elif isinstance(data, list): + return [ + ( + replace_micrometer_values(item, replacement) + if isinstance(item, (dict, list)) + else ( + item[:-2] + replacement + if isinstance(item, str) and item.endswith("µm") + else item + ) + ) + for item in data + ] + return data diff --git a/src/profiles/idw/yaml_helper.py b/src/profiles/idw/yaml_helper.py index 79c1d0cd..dcafe887 100644 --- a/src/profiles/idw/yaml_helper.py +++ b/src/profiles/idw/yaml_helper.py @@ -47,48 +47,6 @@ ] -def replace_micrometer_values( - data: Union[Dict[Any, Any], List[Any]], replacement: str -) -> Union[Dict[Any, Any], List[Any]]: - """Recursively replace micrometer values in a dictionary or list. - - This function searches for string values in dictionaries or lists that end with "µm" - (micrometers) and replaces them with the specified replacement string. - - Args: - data (Union[Dict, List]): The dictionary or list containing values to check and replace. - replacement (str): The replacement string for micrometer values. - - Returns: - Union[Dict, List]: The input data with micrometer values replaced. - """ - if isinstance(data, dict): - for key, value in data.items(): - data[key] = ( - replace_micrometer_values(value, replacement) - if isinstance(value, (dict, list)) - else ( - value[:-2] + replacement - if isinstance(value, str) and value.endswith("µm") - else value - ) - ) - elif isinstance(data, list): - return [ - ( - replace_micrometer_values(item, replacement) - if isinstance(item, (dict, list)) - else ( - item[:-2] + replacement - if isinstance(item, str) and item.endswith("µm") - else item - ) - ) - for item in data - ] - return data - - class YamlParser: """ A class that provides methods to parse YAML files and construct objects.