summaryrefslogtreecommitdiff
path: root/dots_manager/utils.py
blob: e8210cc0ccb8baf35acd7624f62ae181a633892f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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, {})