from typing import Mapping
from typing import Any
from typing import Union
from typing import Sequence
import functools
import operator


def recursive_getitem(d: Mapping[str, Any], keys: Union[str, Sequence[str]]) -> Any:
    """Recursively retrieve an item from a nested dict.

    Credit to: https://stackoverflow.com/a/52260663

    Args:
        d: Mapping of strings to objects.
        keys: Names of the keys under which the object is stored. Can also just be a single string.
    Returns:
        The object stored under the keys.
    Raises:
        KeyError: If one of the keys isnt' found.
    """
    # If only a string, then just just return the item
    if isinstance(keys, str):
        return d[keys]
    else:
        return functools.reduce(operator.getitem, keys, d)