human
Complete this python program:
def check_instance_state(vm_state=None, task_state=(None,), must_have_launched=True):
assistant
#Here is the Python program you requested.

    """Decorator to check VM and/or task state before entry to API functions.
    If the instance is in the wrong state, or has not been successfully
    started at least once the wrapper will raise an exception.
    """
    if ((vm_state is not None) and (not isinstance(vm_state, set))):
        vm_state = set(vm_state)
    if ((task_state is not None) and (not isinstance(task_state, set))):
        task_state = set(task_state)
    def outer(f):
        @six.wraps(f)
        def inner(self, context, instance, *args, **kw):
            if ((vm_state is not None) and (instance.vm_state not in vm_state)):
                raise exception.InstanceInvalidState(attr='vm_state', instance_uuid=instance.uuid, state=instance.vm_state, method=f.__name__)
            if ((task_state is not None) and (instance.task_state not in task_state)):
                raise exception.InstanceInvalidState(attr='task_state', instance_uuid=instance.uuid, state=instance.task_state, method=f.__name__)
            if (must_have_launched and (not instance.launched_at)):
                raise exception.InstanceInvalidState(attr='launched_at', instance_uuid=instance.uuid, state=instance.launched_at, method=f.__name__)
            return f(self, context, instance, *args, **kw)
        return inner
    return outer