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

Function check_args

pythonwhat/checks/check_funcs.py:197–265  ·  view source on GitHub ↗

Check whether a function argument is specified. This function can follow ``check_function()`` in an SCT chain and verifies whether an argument is specified. If you want to go on and check whether the argument was correctly specified, you can can continue chaining with ``has_equal_value(

(state, name, missing_msg=None)

Source from the content-addressed store, hash-verified

195
196
197def check_args(state, name, missing_msg=None):
198 """Check whether a function argument is specified.
199
200 This function can follow ``check_function()`` in an SCT chain and verifies whether an argument is specified.
201 If you want to go on and check whether the argument was correctly specified, you can can continue chaining with
202 ``has_equal_value()`` (value-based check) or ``has_equal_ast()`` (AST-based check)
203
204 This function can also follow ``check_function_def()`` or ``check_lambda_function()`` to see if arguments have been
205 specified.
206
207 Args:
208 name (str): the name of the argument for which you want to check if it is specified. This can also be
209 a number, in which case it refers to the positional arguments. Named arguments take precedence.
210 missing_msg (str): If specified, this overrides the automatically generated feedback message in case
211 the student did specify the argument.
212 state (State): State object that is passed from the SCT Chain (don't specify this).
213
214 :Examples:
215
216 Student and solution code::
217
218 import numpy as np
219 arr = np.array([1, 2, 3, 4, 5])
220 np.mean(arr)
221
222 SCT::
223
224 # Verify whether arr was correctly set in np.mean
225 # has_equal_value() checks the value of arr, used to set argument a
226 Ex().check_function('numpy.mean').check_args('a').has_equal_value()
227
228 # Verify whether arr was correctly set in np.mean
229 # has_equal_ast() checks the expression used to set argument a
230 Ex().check_function('numpy.mean').check_args('a').has_equal_ast()
231
232 Student and solution code::
233
234 def my_power(x):
235 print("calculating sqrt...")
236 return(x * x)
237
238 SCT::
239
240 Ex().check_function_def('my_power').multi(
241 check_args('x') # will fail if student used y as arg
242 check_args(0) # will still pass if student used y as arg
243 )
244
245 """
246 if missing_msg is None:
247 missing_msg = "Did you specify the {{part}}?"
248
249 if name in ["*args", "**kwargs"]: # for check_function_def
250 return check_part(state, name, name, missing_msg=missing_msg)
251 else:
252 if isinstance(name, list): # dealing with args or kwargs
253 if name[0] == "args":
254 arg_str = "{} argument passed as a variable length argument".format(

Callers 6

arg_testFunction · 0.90
test_check_wiki_exampleFunction · 0.85

Calls 2

check_partFunction · 0.85
check_part_indexFunction · 0.85

Tested by 6

arg_testFunction · 0.72
test_check_wiki_exampleFunction · 0.68