def getattr_keypath(obj, key_path, default=None):
    """
    Get attribute by its key path.

    >>> class a(object):
    ...     class b(object):
    ...         c = 2
    ...
    >>> getattr_keypath(a, "b.c")
    2

    Args:
        obj: Target object.
        key_path: Key path separated by dot.
        default: Fallback value for a non-existing keypath.
    Returns:
        Value corresponding with given key path.
    """
    split_key_path = key_path.split(".")
    for part in split_key_path:
        if hasattr(obj, part):
            obj = getattr(obj, part)
        else:
            return default
    return obj