MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / check_function

Function check_function

pythonwhat/checks/check_function.py:39–172  ·  view source on GitHub ↗

Check whether a particular function is called. ``check_function()`` is typically followed by: - ``check_args()`` to check whether the arguments were specified. In turn, ``check_args()`` can be followed by ``has_equal_value()`` or ``has_equal_ast()`` to assert that the arguments

(
    state,
    name,
    index=0,
    missing_msg=None,
    params_not_matched_msg=None,
    expand_msg=None,
    signature=True,
)

Source from the content-addressed store, hash-verified

37
38
39def check_function(
40 state,
41 name,
42 index=0,
43 missing_msg=None,
44 params_not_matched_msg=None,
45 expand_msg=None,
46 signature=True,
47):
48 """Check whether a particular function is called.
49
50 ``check_function()`` is typically followed by:
51
52 - ``check_args()`` to check whether the arguments were specified.
53 In turn, ``check_args()`` can be followed by ``has_equal_value()`` or ``has_equal_ast()``
54 to assert that the arguments were correctly specified.
55 - ``has_equal_value()`` to check whether rerunning the function call coded by the student
56 gives the same result as calling the function call as in the solution.
57
58 Checking function calls is a tricky topic. Please visit the
59 `dedicated article <articles/checking_function_calls.html>`_ for more explanation,
60 edge cases and best practices.
61
62 Args:
63 name (str): the name of the function to be tested. When checking functions in packages, always
64 use the 'full path' of the function.
65 index (int): index of the function call to be checked. Defaults to 0.
66 missing_msg (str): If specified, this overrides an automatically generated feedback message in case
67 the student did not call the function correctly.
68 params_not_matched_msg (str): If specified, this overrides an automatically generated feedback message
69 in case the function parameters were not successfully matched.
70 expand_msg (str): If specified, this overrides any messages that are prepended by previous SCT chains.
71 signature (Signature): Normally, check_function() can figure out what the function signature is,
72 but it might be necessary to use ``sig_from_params()`` to manually build a signature and pass this along.
73 state (State): State object that is passed from the SCT Chain (don&#x27;t specify this).
74
75 :Examples:
76
77 Student code and solution code::
78
79 import numpy as np
80 arr = np.array([1, 2, 3, 4, 5])
81 np.mean(arr)
82
83 SCT::
84
85 # Verify whether arr was correctly set in np.mean
86 Ex().check_function('numpy.mean').check_args('a').has_equal_value()
87
88 # Verify whether np.mean(arr) produced the same result
89 Ex().check_function('numpy.mean').has_equal_value()
90 """
91
92 append_missing = missing_msg is None
93 append_params_not_matched = params_not_matched_msg is None
94 if missing_msg is None:
95 missing_msg = MISSING_MSG
96 if expand_msg is None:

Callers 3

test_functionFunction · 0.90
test_function_v2Function · 0.90
test_check_wiki_exampleFunction · 0.85

Calls 6

part_to_childFunction · 0.90
get_mapped_nameFunction · 0.85
bind_argsFunction · 0.85
findMethod · 0.80
get_manual_sigsMethod · 0.80
to_childMethod · 0.80

Tested by 3

test_functionFunction · 0.72
test_function_v2Function · 0.72
test_check_wiki_exampleFunction · 0.68