diff options
Diffstat (limited to 'dots_manager/utils.py')
| -rw-r--r-- | dots_manager/utils.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/dots_manager/utils.py b/dots_manager/utils.py new file mode 100644 index 0000000..e8210cc --- /dev/null +++ b/dots_manager/utils.py @@ -0,0 +1,25 @@ +from typing import Callable, Optional, TypeVar, Tuple, ParamSpec, Dict, Any +from functools import reduce + +P = ParamSpec("P") +T = TypeVar("T") + + +def is_some( + callable: Callable[P, Optional[T]], *args: P.args, **kwargs: P.kwargs +) -> Tuple[bool, Optional[T]]: + result = callable(*args, **kwargs) + return result is not None, result + + +def merge_dicts(*dicts: Dict[str, Any]) -> Dict[str, Any]: + def merge(a: Dict[str, Any], b: Dict[str, Any]) -> Dict[str, Any]: + out = dict(a) + for k, v in b.items(): + if k in out and isinstance(out[k], dict) and isinstance(v, dict): + out[k] = merge(out[k], v) + else: + out[k] = v + return out + + return reduce(merge, dicts, {}) |
