def as_enum(enum_type, arg):
    """Check if arg is an instance or key of enum_type, and return that enum"""
    if isinstance(arg, enum_type):
        return arg
    try:
        return enum_type[arg]
    except KeyError:
        raise ValueError('{} is not a valid {} enum'.format(arg, enum_type.
            __name__))
