import functools


def rgetattr(obj, attr, *args):
    """Recursive GetAttr to look for nested attributes."""

    def _getattr(obj, attr):
        """Extract the nested value. If the value supports `all()` we return the first valid object."""
        value = getattr(obj, attr, *args)

        return value

    return functools.reduce(_getattr, [obj] + attr.split("."))