pythonwhat/checks/check_funcs.py
def check_part_index(state, name, index, part_msg, missing_msg=None, expand_msg=None):
    """Return child state with indexed name part as its ast tree.

    ``index`` can be:

    - an integer, in which case the student/solution_parts are indexed by position.
    - a string, in which case the student/solution_parts are expected to be a dictionary.
    - a list of indices (which can be integer or string), in which case the student parts are indexed step by step.
    """

    if missing_msg is None:
        missing_msg = "Are you sure you defined the {{part}}? "
    if expand_msg is None:
        expand_msg = "Did you correctly specify the {{part}}? "

    # create message
    ordinal = get_ord(index + 1) if isinstance(index, int) else ""
    fmt_kwargs = {"index": index, "ordinal": ordinal}
    fmt_kwargs.update(part=render(part_msg, fmt_kwargs))

    append_message = {"msg": expand_msg, "kwargs": fmt_kwargs}

    # check there are enough parts for index
    has_part(state, name, missing_msg, fmt_kwargs, index)

    # get part at index
    stu_part = state.student_parts[name]
    sol_part = state.solution_parts[name]

    if isinstance(index, list):
        for ind in index:
            stu_part = stu_part[ind]
            sol_part = sol_part[ind]
    else:
        stu_part = stu_part[index]
        sol_part = sol_part[index]

    assert_ast(state, sol_part, fmt_kwargs)

    # return child state from part
    return part_to_child(stu_part, sol_part, append_message, state)
def check_part_index(state, name, index, part_msg, missing_msg=None, expand_msg=None):
    """Return child state with indexed name part as its ast tree.

    ``index`` can be:

    - an integer, in which case the student/solution_parts are indexed by position.
    - a string, in which case the student/solution_parts are expected to be a dictionary.
    - a list of indices (which can be integer or string), in which case the student parts are indexed step by step.
    """

    if missing_msg is None:
        missing_msg = "Are you sure you defined the {{part}}? "
    if expand_msg is None:
        expand_msg = "Did you correctly specify the {{part}}? "

    # create message
    ordinal = get_ord(index + 1) if isinstance(index, int) else ""
    fmt_kwargs = {"index": index, "ordinal": ordinal}
    fmt_kwargs.update(part=render(part_msg, fmt_kwargs))

    append_message = {"msg": expand_msg, "kwargs": fmt_kwargs}

    # check there are enough parts for index
    has_part(state, name, missing_msg, fmt_kwargs, index)

    # get part at index
    stu_part = state.student_parts[name]
    sol_part = state.solution_parts[name]

    if isinstance(index, list):
        for ind in index:
            stu_part = stu_part[ind]
            sol_part = sol_part[ind]
    else:
        stu_part = stu_part[index]
        sol_part = sol_part[index]

    assert_ast(state, sol_part, fmt_kwargs)

    # return child state from part
    return part_to_child(stu_part, sol_part, append_message, state)
def check_part_index ( state , name , index , part_msg , missing_msg = None , expand_msg = None ) : if missing_msg is None : missing_msg = "Are you sure you defined the {{part}}? " if expand_msg is None : expand_msg = "Did you correctly specify the {{part}}? " # create message ordinal = get_ord ( index + 1 ) if isinstance ( index , int ) else "" fmt_kwargs = { "index" : index , "ordinal" : ordinal } fmt_kwargs . update ( part = render ( part_msg , fmt_kwargs ) ) append_message = { "msg" : expand_msg , "kwargs" : fmt_kwargs } # check there are enough parts for index has_part ( state , name , missing_msg , fmt_kwargs , index ) # get part at index stu_part = state . student_parts [ name ] sol_part = state . solution_parts [ name ] if isinstance ( index , list ) : for ind in index : stu_part = stu_part [ ind ] sol_part = sol_part [ ind ] else : stu_part = stu_part [ index ] sol_part = sol_part [ index ] assert_ast ( state , sol_part , fmt_kwargs ) # return child state from part return part_to_child ( stu_part , sol_part , append_message , state )
Return child state with indexed name part as its ast tree.

    ``index`` can be:

    - an integer, in which case the student/solution_parts are indexed by position.
    - a string, in which case the student/solution_parts are expected to be a dictionary.
    - a list of indices (which can be integer or string), in which case the student parts are indexed step by step.
Return child state with indexed name part as its ast tree .