コード例 #1
0
def test_template(template):
    """Tests if the creation of a template ends up with a valid template. Returns 1/0 for success/failure."""
    got_trough_test = 0  # 1 if template got through test, and 0 if not.
    # Make numbers, check condition, check calculations
    random_domain = template.random_domain
    # Efficiency note: it might be faster to pass the domain list, instead of getting them from template every time.
    answer = template.answer
    question = template.question_text
    solution = template.solution
    conditions = template.conditions
    conditions = remove_unnecessary(conditions)

    variable_dict = generate_valid_numbers(question, random_domain, "", False)
    inserted_conditions = string_replace(conditions, variable_dict)
    if len(conditions) > 1:
        conditions_pass = sympify(latex_to_sympy(inserted_conditions))
    else:
        conditions_pass = True
    if conditions_pass:
        answer = string_replace(answer, variable_dict)
        solution = string_replace(solution, variable_dict)

        try:
            answer = parse_answer(answer, random_domain)
            parse_solution(solution, random_domain)  # Checks if solution can be parsed
            got_trough_test = 1
        except Exception:
            pass
        if answer == 'error':
            got_trough_test = 0
    return got_trough_test
コード例 #2
0
def make_answer_context_dict(form_values):
    """Returns context dict for use on the answer page"""
    user_answer = form_values['user_answer']
    user_answer = user_answer.replace(',','.')
    user_answer = user_answer.replace('..', ',')  # A cheeky way to circumvent removal of ','
    template_type = form_values['template_type']
    template_specific = form_values['template_specific']
    q = Template.objects.get(pk=form_values['primary_key'])
    variable_dictionary = form_values['variable_dictionary'].split('§')
    replacing_words = form_values['replacing_words']
    random_domain = q.random_domain

    if template_type != 'blanks':
        answer = q.answer.replace('\\\\', '\\')
        answer = answer.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
        answer = answer.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
        answer = replace_variables_from_array(variable_dictionary, answer)
        #answer = add_phantom_minus(answer)
    else:
        #answer = get_values_from_position(template_specific, q.solution.replace('\\\\', '\\'))
        answer = get_fillin_answers(q.fill_in.replace('\\\\', '\\'))
        answer = answer.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
        answer = answer.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
        answer = replace_variables_from_array(variable_dictionary, answer)

    original_user_answer = user_answer.replace('§', '\\text{ og }')
    #answer = remove_pm_and_add_parenthesis(answer) # Replace with new parenthesis parsing
    answer = parse_answer(answer, random_domain)
    answer = parenthesis_removal(answer)
    answer = answer.replace('`', '')
    answer = answer.split('§')
    solution = str(q.question_text.replace('\\\\', '\\')) + "§" + str(q.solution.replace('\\\\', '\\'))
    #solution = add_phantom_minus(solution)
    solution = solution.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
    solution = solution.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
    solution = replace_variables_from_array(variable_dictionary, solution)
    #solution = remove_pm_and_add_parenthesis(solution)
    solution = parse_solution(solution, random_domain)
    solution = parenthesis_removal(solution)
    if len(replacing_words) > 0:
        solution = replace_words(solution, replacing_words)['sentence']
    user_answer = user_answer.split('§')  # If a string doesn't contain the character it returns a list with 1 element
    # We format both the user answer and the answer the same way.
    user_answer = [after_equal_sign(x) for x in user_answer]  # Only get the values after last equal sign.
    #user_answer = calculate_array(user_answer, random_domain)
    answer = [after_equal_sign(x) for x in answer]
    #answer = calculate_array(answer, random_domain)
    answer_text = "\\text{Du har svart }" + original_user_answer + \
                      "\\text{. Det er feil. Svaret er: }" + '\\text{ og }'.join(answer)

    correct_answer = check_answer(user_answer, answer, template_type, q.margin_of_error)  # Check if the user answered correctly.

    if correct_answer:
        answer_text = "\\text{Du har svart riktig!}"

    answer_text = latex_exceptions(answer_text)

    graph = q.graph
    if graph:
        graph = json.loads(graph)

    for x in range(0, len(graph)):
            graph[x] = replace_variables_from_array(variable_dictionary, graph[x])
            graph[x] = parse_solution(graph[x], q.random_domain)

    if graph != None and graph != '':  # to prevent error if none
        graph = json.dumps(graph)
    context_dict = {'title': "Oppgavegen", 'answer': str(answer_text),
                    'solution': solution, 'user_won': correct_answer, 'graph': graph,
                    'graph_color' : q.graph_color, 'graph_settings': q.graph_settings}
    return context_dict