def has_key(key, key_missing_msg=MSG_KEY_MISSING, 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 isDefinedCollInProcess(sol_name, key, state.solution_process): raise NameError("Not all keys you specified are actually keys in %s in the solution process" % sol_name) # check if key available _msg = state.build_message(key_missing_msg, {'key': key}) rep.do_test(DefinedCollProcessTest(stu_name, key, state.student_process, Feedback(_msg, state.highlight))) return state
def has_key(key, key_missing_msg=MSG_KEY_MISSING, 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 isDefinedCollInProcess(sol_name, key, state.solution_process): raise NameError( "Not all keys you specified are actually keys in %s in the solution process" % sol_name) # check if key available _msg = state.build_message(key_missing_msg, {'key': key}) rep.do_test( DefinedCollProcessTest(stu_name, key, state.student_process, Feedback(_msg, state.highlight))) return state
def has_key(key, key_missing_msg=MSG_KEY_MISSING, state=None): """Check whether an object (dict, DataFrame, etc) has a key. ``has_key()`` can currently only be used when chained from ``check_object()``, the function that is used to 'zoom in' on the object of interest. Args: key (str): Name of the key that the object should have. key_missing_msg (str): When specified, this overrides the automatically generated message in case the key does not exist. state (State): The state that is passed in through the SCT chain (don't specify this). :Example: Student code and solution code:: x = {'a': 2} SCT:: # Verify that x contains a key a Ex().check_object('x').has_key('a') """ rep = Reporter.active_reporter sol_name = state.solution_parts.get('name') stu_name = state.student_parts.get('name') if not isDefinedCollInProcess(sol_name, key, state.solution_process): raise NameError( "Not all keys you specified are actually keys in %s in the solution process" % sol_name) # check if key available _msg = state.build_message(key_missing_msg, {'key': key}) rep.do_test( DefinedCollProcessTest(stu_name, key, state.student_process, Feedback(_msg, state))) return state
def check_keys(key, missing_msg=None, expand_msg=None, state=None): """Check whether an object (dict, DataFrame, etc) has a key. ``check_keys()`` can currently only be used when chained from ``check_object()``, the function that is used to 'zoom in' on the object of interest. Args: key (str): Name of the key that the object should have. missing_msg (str): When specified, this overrides the automatically generated message in case the key does not exist. expand_msg (str): If specified, this overrides any messages that are prepended by previous SCT chains. state (State): The state that is passed in through the SCT chain (don't specify this). :Example: Student code and solution code:: x = {'a': 2} SCT:: # Verify that x contains a key a Ex().check_object('x').check_keys('a') # Verify that x contains a key a and a is correct. Ex().check_object('x').check_keys('a').has_equal_value() """ state.assert_is(['object_assignments'], 'is_instance', ['check_object', 'check_df']) if missing_msg is None: missing_msg = "There is no {{ 'column' if 'DataFrame' in parent.typestr else 'key' }} `'{{key}}'`." if expand_msg is None: expand_msg = "Did you correctly set the {{ 'column' if 'DataFrame' in parent.typestr else 'key' }} `'{{key}}'`? " rep = Reporter.active_reporter sol_name = state.solution_parts.get('name') stu_name = state.student_parts.get('name') if not isDefinedCollInProcess(sol_name, key, state.solution_process): raise InstructorError( "`check_keys()` couldn't find key `%s` in object `%s` in the solution process." % (key, sol_name)) # check if key available _msg = state.build_message(missing_msg, {'key': key}) rep.do_test( DefinedCollProcessTest(stu_name, key, state.student_process, Feedback(_msg, state))) def get_part(name, key, highlight): if isinstance(key, str): slice_val = ast.Str(s=key) else: slice_val = ast.parse(str(key)).body[0].value expr = ast.Subscript(value=ast.Name(id=name, ctx=ast.Load()), slice=ast.Index(value=slice_val), ctx=ast.Load()) ast.fix_missing_locations(expr) return {'node': expr, 'highlight': highlight} stu_part = get_part(stu_name, key, state.student_parts.get('highlight')) sol_part = get_part(sol_name, key, state.solution_parts.get('highlight')) append_message = {'msg': expand_msg, 'kwargs': {'key': key}} child = part_to_child(stu_part, sol_part, append_message, state) return child