def test_data_frame(name, columns=None, undefined_msg=None, not_data_frame_msg=None, undefined_cols_msg=None, incorrect_msg=None, state=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(name, undefined_msg, not_instance_msg=not_data_frame_msg, expand_msg=expand_msg, state=state) # if columns not set, figure them out from solution if columns is None: columns = getColumnsInProcess(name, child.solution_process) if columns is None: raise InstructorError( "Something went wrong in figuring out the columns for %s in the solution process" % name) for col in columns: colstate = check_keys(col, missing_msg=undefined_cols_msg, state=child) has_equal_value(incorrect_msg=incorrect_msg, state=colstate)
def test_args(arg_names, arg_defaults, nb_args_msg, arg_names_msg, arg_defaults_msg, child, quiet_child): MSG_NUM_ARGS = "FMT:You should define {parent[typestr]} with {sol_len} arguments, instead got {stu_len}." MSG_BAD_ARG_NAME = "FMT:The {parent[ordinal]} {parent[part]} should be called `{sol_part[name]}`, instead got `{stu_part[name]}`." MSG_BAD_DEFAULT = "FMT:The {parent[part]} `{stu_part[name]}` should have no default." MSG_INC_DEFAULT = "FMT:The {parent[part]} `{stu_part[name]}` does not have the correct default." MSG_NO_VARARG = "FMT:Have you specified an argument to take a `*` argument and named it `{sol_part[*args][name]}`?" MSG_NO_KWARGS = "FMT:Have you specified an argument to take a `**` argument and named it `{sol_part[**kwargs][name]}`?" MSG_VARARG_NAME = "FMT:Have you specified an argument to take a `*` argument and named it `{sol_part[name]}`?" MSG_KWARG_NAME = "FMT: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('_spec1_args', nb_args_msg or MSG_NUM_ARGS, state=quiet_child) # iterate over each arg, testing name and default for ii in range(len(child.solution_parts['_spec1_args'])): # get argument state arg_state = check_part_index('_spec1_args', ii, 'argument', "NO MISSING MSG", expand_msg="", state=child) # test exact name has_equal_part('name', arg_names_msg or MSG_BAD_ARG_NAME, arg_state) if arg_defaults: # test whether is default has_equal_part('is_default', arg_defaults_msg or MSG_BAD_DEFAULT, arg_state) # test default value, use if to prevent running a process no default if arg_state.solution_parts['is_default']: has_equal_value(incorrect_msg=arg_defaults_msg or MSG_INC_DEFAULT, error_msg="error message", append=True, state=arg_state) # test *args and **kwargs if child.solution_parts['*args']: vararg = check_part('*args', "", missing_msg=MSG_NO_VARARG, expand_msg="", state=child) has_equal_part('name', MSG_VARARG_NAME, state=vararg) if child.solution_parts['**kwargs']: kwarg = check_part('**kwargs', "", missing_msg=MSG_NO_KWARGS, expand_msg="", state=child) has_equal_part('name', MSG_KWARG_NAME, state=kwarg)
def arg_test(name, do_eval, missing_msg, incorrect_msg, state): arg_state = check_args(name=name, missing_msg=missing_msg, state=state) append = incorrect_msg is None if isinstance(do_eval, bool): if do_eval: has_equal_value(incorrect_msg=incorrect_msg, append=append, copy=False, state=arg_state) else: has_equal_ast(incorrect_msg=incorrect_msg, append=append, state=arg_state)
def test_object(name, eq_condition="equal", eq_fun=None, do_eval=True, undefined_msg=None, incorrect_msg=None, state=None): expand_msg = "" if undefined_msg or incorrect_msg else None child = check_object(name, undefined_msg, expand_msg=expand_msg, state=state) if do_eval: has_equal_value(incorrect_msg, state=child)
def test_expression_result(extra_env=None, context_vals=None, incorrect_msg=None, expr_code=None, pre_code=None, error_msg=None, state=None, **kwargs): has_equal_value(incorrect_msg=incorrect_msg, error_msg=error_msg, extra_env=extra_env, context_vals=context_vals, expr_code=expr_code, pre_code=pre_code, state=state, **kwargs)
def test_object_after_expression(name, extra_env=None, context_vals=None, undefined_msg=None, incorrect_msg=None, expr_code=None, pre_code=None, state=None, **kwargs): state.highlight = state.student_object_assignments.get(name, {}).get('highlight') has_equal_value(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, state=state, **kwargs)