Skip to content

Commit

Permalink
Move an IDW helper into a new module, as it's not specifically YAML-r…
Browse files Browse the repository at this point in the history
…elated
  • Loading branch information
andrew-uoa committed Sep 10, 2024
1 parent ea4a533 commit 8691600
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
45 changes: 45 additions & 0 deletions src/profiles/idw/idw_utils.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 0 additions & 42 deletions src/profiles/idw/yaml_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8691600

Please sign in to comment.