Skip to content

Commit 5a1f83b

Browse files
committed
duplicate some func.
1 parent db87d82 commit 5a1f83b

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/pytask_parallel/config.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from __future__ import annotations
33

44
import os
5+
from typing import Any
6+
from typing import Callable
57

6-
from pytask import get_first_non_none_value
78
from pytask import hookimpl
89
from pytask_parallel.backends import PARALLEL_BACKENDS_DEFAULT
910
from pytask_parallel.callbacks import delay_callback
@@ -14,7 +15,7 @@
1415
@hookimpl
1516
def pytask_parse_config(config, config_from_cli, config_from_file):
1617
"""Parse the configuration."""
17-
config["n_workers"] = get_first_non_none_value(
18+
config["n_workers"] = _get_first_non_none_value(
1819
config_from_cli,
1920
config_from_file,
2021
key="n_workers",
@@ -24,15 +25,15 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
2425
if config["n_workers"] == "auto":
2526
config["n_workers"] = max(os.cpu_count() - 1, 1)
2627

27-
config["delay"] = get_first_non_none_value(
28+
config["delay"] = _get_first_non_none_value(
2829
config_from_cli,
2930
config_from_file,
3031
key="delay",
3132
default=0.1,
3233
callback=delay_callback,
3334
)
3435

35-
config["parallel_backend"] = get_first_non_none_value(
36+
config["parallel_backend"] = _get_first_non_none_value(
3637
config_from_cli,
3738
config_from_file,
3839
key="parallel_backend",
@@ -46,3 +47,31 @@ def pytask_post_parse(config):
4647
"""Disable parallelization if debugging is enabled."""
4748
if config["pdb"] or config["trace"]:
4849
config["n_workers"] = 1
50+
51+
52+
def _get_first_non_none_value(
53+
*configs: dict[str, Any],
54+
key: str,
55+
default: Any | None = None,
56+
callback: Callable[..., Any] | None = None,
57+
) -> Any:
58+
"""Get the first non-None value for a key from a list of dictionaries.
59+
60+
This function allows to prioritize information from many configurations by changing
61+
the order of the inputs while also providing a default.
62+
63+
Examples
64+
--------
65+
>>> _get_first_non_none_value({"a": None}, {"a": 1}, key="a")
66+
1
67+
>>> _get_first_non_none_value({"a": None}, {"a": None}, key="a", default="default")
68+
'default'
69+
>>> _get_first_non_none_value({}, {}, key="a", default="default")
70+
'default'
71+
>>> _get_first_non_none_value({"a": None}, {"a": "b"}, key="a")
72+
'b'
73+
74+
"""
75+
callback = (lambda x: x) if callback is None else callback # noqa: E731
76+
processed_values = (callback(config.get(key)) for config in configs)
77+
return next((value for value in processed_values if value is not None), default)

0 commit comments

Comments
 (0)