pythonwhat/checks/check_object.py
def is_instance(state, inst, not_instance_msg=None):
    """Check whether an object is an instance of a certain class.

    ``is_instance()`` can currently only be used when chained from ``check_object()``, the function that is
    used to 'zoom in' on the object of interest.

    Args:
        inst (class): The class that the object should have.
        not_instance_msg (str): When specified, this overrides the automatically generated message in case
            the object does not have the expected class.
        state (State): The state that is passed in through the SCT chain (don't specify this).

    :Example:

        Student code and solution code::

            import numpy as np
            arr = np.array([1, 2, 3, 4, 5])

        SCT::

            # Verify the class of arr
            import numpy
            Ex().check_object('arr').is_instance(numpy.ndarray)
    """

    state.assert_is(["object_assignments"], "is_instance", ["check_object"])

    sol_name = state.solution_parts.get("name")
    stu_name = state.student_parts.get("name")

    if not_instance_msg is None:
        not_instance_msg = "Is it a {{inst.__name__}}?"

    if not isInstanceInProcess(sol_name, inst, state.solution_process):
        raise InstructorError(
            "`is_instance()` noticed that `%s` is not a `%s` in the solution process."
            % (sol_name, inst.__name__)
        )

    _msg = state.build_message(not_instance_msg, {"inst": inst})
    feedback = Feedback(_msg, state)
    state.do_test(InstanceProcessTest(stu_name, inst, state.student_process, feedback))

    return state
def is_instance(state, inst, not_instance_msg=None):
    state.assert_is(["object_assignments"], "is_instance", ["check_object"])

    sol_name = state.solution_parts.get("name")
    stu_name = state.student_parts.get("name")

    if not_instance_msg is None:
        not_instance_msg = "Is it a {{inst.__name__}}?"

    if not isInstanceInProcess(sol_name, inst, state.solution_process):
        raise InstructorError(
            "`is_instance()` noticed that `%s` is not a `%s` in the solution process."
            % (sol_name, inst.__name__)
        )

    _msg = state.build_message(not_instance_msg, {"inst": inst})
    feedback = Feedback(_msg, state)
    state.do_test(InstanceProcessTest(stu_name, inst, state.student_process, feedback))

    return state
def is_instance ( state , inst , not_instance_msg = None ) : state . assert_is ( [ "object_assignments" ] , "is_instance" , [ "check_object" ] ) sol_name = state . solution_parts . get ( "name" ) stu_name = state . student_parts . get ( "name" ) if not_instance_msg is None : not_instance_msg = "Is it a {{inst.__name__}}?" if not isInstanceInProcess ( sol_name , inst , state . solution_process ) : raise InstructorError ( "`is_instance()` noticed that `%s` is not a `%s` in the solution process." % ( sol_name , inst . __name__ ) ) _msg = state . build_message ( not_instance_msg , { "inst" : inst } ) feedback = Feedback ( _msg , state ) state . do_test ( InstanceProcessTest ( stu_name , inst , state . student_process , feedback ) ) return state
Check whether an object is an instance of a certain class.

    ``is_instance()`` can currently only be used when chained from ``check_object()``, the function that is
    used to 'zoom in' on the object of interest.

    Args:
        inst (class): The class that the object should have.
        not_instance_msg (str): When specified, this overrides the automatically generated message in case
            the object does not have the expected class.
        state (State): The state that is passed in through the SCT chain (don't specify this).

    :Example:

        Student code and solution code::

            import numpy as np
            arr = np.array([1, 2, 3, 4, 5])

        SCT::

            # Verify the class of arr
            import numpy
            Ex().check_object('arr').is_instance(numpy.ndarray)
Check whether an object is an instance of a certain class .