from functools import reduce


def deepgetattr(obj, attr, default=None, sep="."):
    """Recurse through an attribute chain to get the ultimate value."""

    return reduce(lambda o, key: o.get(key, default), attr.split(sep), obj)