def _set_default(obj, key, val, required=True):
    """
    Sets val to obj.key if val is None and obj.key is set.
    Raises Attribute error is neither are set and required is True.

    :returns: val
    """
    if not val:
        val = getattr(obj, key, None)
    if not val and required:
        msg = '{} is not set'.format(key)
        raise AttributeError(msg)
    return val