Ejemplo n.º 1
0
def test_data_frame(
    state,
    name,
    columns=None,
    undefined_msg=None,
    not_data_frame_msg=None,
    undefined_cols_msg=None,
    incorrect_msg=None,
):
    """Test a pandas dataframe.
    """

    expand_msg = (
        ""
        if undefined_msg or not_data_frame_msg or undefined_cols_msg or incorrect_msg
        else None
    )

    child = check_df(
        state,
        name,
        undefined_msg,
        not_instance_msg=not_data_frame_msg,
        expand_msg=expand_msg,
    )

    # if columns not set, figure them out from solution
    if columns is None:
        columns = getColumnsInProcess(name, child.solution_process)

    for col in columns:
        colstate = check_keys(child, col, missing_msg=undefined_cols_msg)
        has_equal_value(colstate, incorrect_msg=incorrect_msg)
Ejemplo n.º 2
0
def test_args(state, arg_names, arg_defaults, nb_args_msg, arg_names_msg,
              arg_defaults_msg):

    MSG_NUM_ARGS = "You should define {{parent[typestr]}} with {{sol_len}} arguments, instead got {{stu_len}}."
    MSG_BAD_ARG_NAME = "The {{parent[ordinal]}} {{parent[part]}} should be called `{{sol_part[name]}}`, instead got `{{stu_part[name]}}`."
    MSG_BAD_DEFAULT = (
        "The {{parent[part]}} `{{stu_part[name]}}` should have no default.")
    MSG_INC_DEFAULT = (
        "The {{parent[part]}} `{{stu_part[name]}}` does not have the correct default."
    )

    MSG_NO_VARARG = "Have you specified an argument to take a `*` argument and named it `{{sol_part['*args'][name]}}`?"
    MSG_NO_KWARGS = "Have you specified an argument to take a `**` argument and named it `{{sol_part['**kwargs'][name]}}`?"
    MSG_VARARG_NAME = "Have you specified an argument to take a `*` argument and named it `{{sol_part[name]}}`?"
    MSG_KWARG_NAME = "Have you specified an argument to take a `**` argument and named it `{{sol_part[name]}}`?"

    if arg_names or arg_defaults:
        # test number of args
        has_equal_part_len(state, "_spec1_args", nb_args_msg or MSG_NUM_ARGS)

        # iterate over each arg, testing name and default
        for ii in range(len(state.solution_parts["_spec1_args"])):
            # get argument state
            arg_state = check_part_index(state, "_spec1_args", ii, "argument",
                                         "NO MISSING MSG")
            # test exact name
            has_equal_part(arg_state, "name", arg_names_msg
                           or MSG_BAD_ARG_NAME)

            if arg_defaults:
                # test whether is default
                has_equal_part(arg_state, "is_default", arg_defaults_msg
                               or MSG_BAD_DEFAULT)
                # test default value, use if to prevent running a process no default
                if arg_state.solution_parts["is_default"]:
                    has_equal_value(
                        arg_state,
                        incorrect_msg=arg_defaults_msg or MSG_INC_DEFAULT,
                        append=True,
                    )

        # test *args and **kwargs
        if state.solution_parts["*args"]:
            vararg = check_part(state, "*args", "", missing_msg=MSG_NO_VARARG)
            has_equal_part(vararg, "name", MSG_VARARG_NAME)

        if state.solution_parts["**kwargs"]:
            kwarg = check_part(state,
                               "**kwargs",
                               "",
                               missing_msg=MSG_NO_KWARGS)
            has_equal_part(kwarg, "name", MSG_KWARG_NAME)
Ejemplo n.º 3
0
def arg_test(state, name, do_eval, missing_msg, incorrect_msg):
    arg_state = check_args(state, name=name, missing_msg=missing_msg)

    append = incorrect_msg is None

    if isinstance(do_eval, bool):
        if do_eval:
            has_equal_value(arg_state,
                            incorrect_msg=incorrect_msg,
                            append=append,
                            copy=False)
        else:
            has_equal_ast(arg_state,
                          incorrect_msg=incorrect_msg,
                          append=append)
Ejemplo n.º 4
0
def test_object(
    state,
    name,
    eq_condition="equal",
    eq_fun=None,
    do_eval=True,
    undefined_msg=None,
    incorrect_msg=None,
):

    expand_msg = "" if undefined_msg or incorrect_msg else None
    child = check_object(state, name, undefined_msg, expand_msg=expand_msg)

    if do_eval:
        has_equal_value(child, incorrect_msg)
Ejemplo n.º 5
0
def test_expression_result(state,
                           extra_env=None,
                           context_vals=None,
                           incorrect_msg=None,
                           expr_code=None,
                           pre_code=None,
                           error_msg=None,
                           **kwargs):
    has_equal_value(state,
                    incorrect_msg=incorrect_msg,
                    error_msg=error_msg,
                    extra_env=extra_env,
                    context_vals=context_vals,
                    expr_code=expr_code,
                    pre_code=pre_code,
                    **kwargs)
Ejemplo n.º 6
0
def test_object_after_expression(state,
                                 name,
                                 extra_env=None,
                                 context_vals=None,
                                 undefined_msg=None,
                                 incorrect_msg=None,
                                 expr_code=None,
                                 pre_code=None,
                                 **kwargs):
    state.highlight = state.ast_dispatcher(
        "object_assignments", state.student_ast).get(name, {}).get("highlight")
    has_equal_value(state,
                    incorrect_msg=incorrect_msg,
                    error_msg=undefined_msg,
                    undefined_msg=undefined_msg,
                    extra_env=extra_env,
                    context_vals=context_vals,
                    pre_code=pre_code,
                    name=name,
                    expr_code=expr_code,
                    **kwargs)