Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move config-checking code out of invoker #386

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions invokers/run_gridsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions kymata/io/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
26 changes: 25 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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
Loading