Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

lest Inputs Write a function Checklnputs that checks if three inputs parameters

ID: 3601511 • Letter: L

Question

lest Inputs Write a function Checklnputs that checks if three inputs parameters are the correct data types. The output of Checklnputs is a logical row array errorCode with 4 values. If all the tests pass, then errorCode consists of only of false values. If the 1st input parameter is not a string the 1st array element in errorCode is set true. If the 2nd input parameter is not a numeric, the 2nd array element in errorCode is set true. If the 3rd input parameter is not a logical variable, the 3rd array element in errorCode is set true. The 4th array element in errorCode is set true if three input parameters are not present. For example >> name-"Joe: gpa-3; en rollStatus=t rue ; %Ali datatypes correct errorCode - CheckInputs (name, gpa, enrollStatus) errorCode - 1x4 logical array >> names]; gpa-3 ; en rollStatusrue; % First datatype incorrect errorCodeCheckInputs (name, gpa, enrollStatus) errorCode 1x4 logical array incorrect >> name-' Joe'; gpa-3 ; enrollStatus=1 % Third datatype errorCode = Check!nputs(name, gpa, enrol!Status) enrollStatus errorCode 1x4 logical array 0 0 1 0 gpa) % Absent input argument >> errorCode = Check!nputs (name, errorCode = 1x4 logical array

Explanation / Answer

ef returns(*accepted_return_type_tuple):

    '''

    Validates the return type. Since there's only ever one

    return type, this makes life simpler. Along with the

    accepts() decorator, this also only does a check for

    the top argument. For example you couldn't check

    (<type 'tuple'>, <type 'int'>, <type 'str'>).

    In that case you could only check if it was a tuple.

    '''

    def return_decorator(validate_function):

        # No return type has been specified.

        if len(accepted_return_type_tuple) == 0:

            raise TypeError('You must specify a return type.')

     @functools.wraps(validate_function)

        def decorator_wrapper(*function_args):

            # More than one return type has been specified.

            if len(accepted_return_type_tuple) > 1:

                raise TypeError('You must specify one return type.')

            # Since the decorator receives a tuple of arguments

            # and the is only ever one object returned, we'll just

            # grab the first parameter.

            accepted_return_type = accepted_return_type_tuple[0]

            # We'll execute the function, and

            # take a look at the return type.

            return_value = validate_function(*function_args)

            return_value_type = type(return_value)

            if return_value_type is not accepted_return_type:

                raise InvalidReturnType(return_value_type,

                                        validate_function.__name__)

            return return_value

        return decorator_wrapper

    return return_decorator