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

Change run_mode to as_kwargs #4

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ Hydraclick provides several configuration options to customize your CLI:
- `config_path`: Path to the configuration directory. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/)
- `config_name`: Name of the configuration file. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/)
- `version_base`: Base version of the configuration. Passed to [`hydra.main()`](https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/)
- `run_mode`: Mode to run the function (`"config"` or `"kwargs"`).
- `"config"`: Pass the configuration as a single `OmegaConf.DictCondig` object.
- `"kwargs"`: Resolve the `OmegaConf.DictConfig` objet into a python `dict` and pass it as keyword arguments.
- `as_kwargs`: The mode in which to run the function. If `True`, the function is run with the
configuration as keyword arguments. In this case the configuration is converted to a dictionary
before passing it to the function. If `False`, pass the configuration as a single `OmegaConf.DictConfig` object.
Defaults to `False`.
- `preprocess_config`: Function to preprocess the configuration. It takes a `DictConfig` object and returns a `DictConfig` object.
- `print_config`: Whether to print the configuration before execution.
- `resolve`: Whether to resolve the configuration.
Expand Down
29 changes: 17 additions & 12 deletions src/hydraclick/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

def wrap_kwargs_and_config(
function: Callable,
run_mode: str = "config", # "config" | "kwargs"
as_kwargs: bool = False,
print_config: bool = True,
preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
resolve: bool = True,
Expand All @@ -57,9 +57,10 @@ def wrapper(config):
raise e
if print_config:
display_config(config, logger=_logger)
if run_mode == "config":
if not as_kwargs:
return function(config)
return function(**config)
conf_dict = OmegaConf.to_container(config, resolve=resolve)
return function(**conf_dict)

return wrapper

Expand Down Expand Up @@ -165,7 +166,7 @@ def command_api(
config_path: str | Path | None = None,
config_name: str | None = "config",
version_base: str | None = None,
run_mode: str = "config",
as_kwargs: bool = False,
preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
print_config: bool = True,
resolve: bool = True,
Expand All @@ -183,8 +184,10 @@ def command_api(
(without the `.yaml` or `.yml` extension). Defaults to `"config"`.
version_base (str | None, optional): The base version of the configuration. \
Defaults to `None`.
run_mode (str, optional): The mode in which to run the function. Can be `"config"` \
or `"kwargs"`. Defaults to `"config"`.
as_kwargs (bool, optional): The mode in which to run the function. \
If `True`, the function is run with the configuration as keyword arguments. In \
this case the configuration is converted to a dictionary before passing it to the \
function. Defaults to `False`.
preprocess_config (Callable[[DictConfig], DictConfig] | None, optional): A function \
to preprocess the configuration before passing it to the main function. \
Defaults to `None`.
Expand Down Expand Up @@ -276,7 +279,7 @@ def click_compatible(
):
nonlocal \
print_config, \
run_mode, \
as_kwargs, \
preprocess_config, \
resolve, \
config_path, \
Expand All @@ -287,7 +290,7 @@ def click_compatible(
if show_config:
print_config = False
true_func = wrap_kwargs_and_config(
function, run_mode, print_config, preprocess_config, resolve
function, as_kwargs, print_config, preprocess_config, resolve
)
hydra_args = build_hydra_args(
hydra_help,
Expand Down Expand Up @@ -321,7 +324,7 @@ def hydra_command(
config_path: str | Path | None = None,
config_name: str | None = "config",
version_base: str | None = None,
run_mode: str = "config",
as_kwargs: bool = False,
preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
print_config: bool = True,
resolve: bool = True,
Expand All @@ -337,8 +340,10 @@ def hydra_command(
(without the `.yaml` or `.yml` extension). Defaults to `"config"`.
version_base (str | None, optional): The base version of the configuration. \
Defaults to `None`.
run_mode (str, optional): The mode in which to run the function. Can be `"config"` \
or `"kwargs"`. Defaults to `"config"`.
as_kwargs (bool, optional): The mode in which to run the function. \
If `True`, the function is run with the configuration as keyword arguments. In \
this case the configuration is converted to a dictionary before passing it to the \
function. Defaults to `False`.
preprocess_config (Callable[[DictConfig], DictConfig] | None, optional): A function to \
preprocess the configuration before passing it to the main function. \
Defaults to `None`.
Expand Down Expand Up @@ -371,7 +376,7 @@ def decorator(function: Callable):
config_name=config_name,
version_base=version_base,
use_flogging=use_flogging,
run_mode=run_mode,
as_kwargs=as_kwargs,
print_config=print_config,
preprocess_config=preprocess_config,
resolve=resolve,
Expand Down
Loading