Esempio n. 1
0
def is_instance(inst, not_instance_msg="FMT:Is it a {inst.__name__}?", name=None, state=None):
    rep = Reporter.active_reporter

    sol_name = name or state.solution_parts.get('name')
    stu_name = name or state.student_parts.get('name')

    if not isInstanceInProcess(sol_name, inst, state.solution_process):
        raise ValueError("%r is not a %s in the solution environment" % (sol_name, type(inst)))

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

    return state
Esempio n. 2
0
def is_instance(inst, not_instance_msg=None, state=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'])

    rep = Reporter.active_reporter

    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)
    rep.do_test(
        InstanceProcessTest(stu_name, inst, state.student_process, feedback))

    return state
Esempio n. 3
0
def is_instance(inst,
                not_instance_msg="FMT:Is it a {inst.__name__}?",
                name=None,
                state=None):
    rep = Reporter.active_reporter

    sol_name = name or state.solution_parts.get('name')
    stu_name = name or state.student_parts.get('name')

    if not isInstanceInProcess(sol_name, inst, state.solution_process):
        raise ValueError("%r is not a %s in the solution environment" %
                         (sol_name, type(inst)))

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

    return state