def extract_id(source):
    """
    Attempts to extract an ID from the argument, first by looking for an
    attribute and then by using dictionary access. If both fail, the argument
    is returned.
    """
    try:
        return source.id
    except AttributeError:
        pass

    try:
        return source["id"]
    except (TypeError, KeyError) as err:
        pass

    return source