From 03646d1e891271339ca256fadd15eaa8ac678911 Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Mon, 2 Jun 2025 15:54:32 -0700 Subject: Make it a python package --- dots_manager/utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 dots_manager/utils.py (limited to 'dots_manager/utils.py') 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, {}) -- cgit v1.2.3-70-g09d2