diff --git a/invokers/run_gridsearch.py b/invokers/run_gridsearch.py index 81246e39..e0ef4ac4 100755 --- a/invokers/run_gridsearch.py +++ b/invokers/run_gridsearch.py @@ -17,19 +17,6 @@ _logger = getLogger(__file__) -def get_config_value_with_fallback(config: dict, config_key: str, fallback): - """ - Get a value from the config, with a default fallback, and a notification explaining this. - """ - try: - return config[config_key] - except KeyError: - _logger.error( - f'Config did not contain any value for "{config_key}", falling back to default value {fallback}' - ) - return fallback - - def main(): _default_output_dir.mkdir(exist_ok=True, parents=False) diff --git a/kymata/io/config.py b/kymata/io/config.py index 14772ffe..82206b92 100644 --- a/kymata/io/config.py +++ b/kymata/io/config.py @@ -2,6 +2,7 @@ import yaml +from invokers.run_gridsearch import _logger from kymata.datasets.data_root import data_root_path from kymata.io.file import PathType, FileType, open_or_use @@ -76,3 +77,31 @@ def get_root_dir(config: dict) -> str: raise ValueError( "The `data_location` parameter in the config file must be either 'cbu' or 'local' or 'cbu-local'." ) + + +def get_config_value_with_fallback(config: dict, config_key: str, fallback): + """ + Retrieve a config value by key, with a fallback option if the key does not exist. + + This function attempts to fetch the value associated with `config_key` from the + provided `config` dictionary. If the key is not present, it logs an error message + and returns the specified `fallback` value. + + Args: + config (dict): A dictionary containing configuration settings. + config_key (str): The key for the desired configuration value. + fallback (any): The value to return if the configuration key is not found. + + Returns: + any: The value associated with `config_key` if it exists; otherwise, the `fallback` value. + + Raises: + None: This function does not raise exceptions; it handles missing keys gracefully. + """ + try: + return config[config_key] + except KeyError: + _logger.error( + f'Config did not contain any value for "{config_key}", falling back to default value {fallback}' + ) + return fallback diff --git a/tests/test_config.py b/tests/test_config.py index 4224a512..afd1c225 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,29 @@ -from kymata.io.config import get_root_dir +from kymata.io.config import get_root_dir, get_config_value_with_fallback def test_import_no_errors(): get_root_dir({"data_location": "cbu"}) + + +def test_key_exists(): + config = {'key1': 'value1', 'key2': 'value2'} + result = get_config_value_with_fallback(config, 'key1', 'default_value') + assert result == 'value1' + + +def test_key_does_not_exist(): + config = {'key1': 'value1'} + result = get_config_value_with_fallback(config, 'key2', 'default_value') + assert result == 'default_value' + + +def test_fallback_is_used(): + config = {} + result = get_config_value_with_fallback(config, 'non_existent_key', 'fallback_value') + assert result == 'fallback_value' + + +def test_fallback_of_none(): + config = {} + result = get_config_value_with_fallback(config, 'non_existent_key', None) + assert result is None