Esempio n. 1
0
def parse(element_html, data):
    element = lxml.html.fragment_fromstring(element_html)
    name = pl.get_string_attrib(element, 'answers-name')
    allow_fractions = pl.get_boolean_attrib(element, 'allow-fractions',
                                            ALLOW_FRACTIONS_DEFAULT)

    # Get true answer
    a_tru = pl.from_json(data['correct_answers'].get(name, None))
    if a_tru is None:
        return
    a_tru = np.array(a_tru)
    if a_tru.ndim != 2:
        raise ValueError('true answer must be a 2D array')
    else:
        m, n = np.shape(a_tru)
        A = np.empty([m, n])

    # Create an array for the submitted answer to be stored in data['submitted_answer'][name]
    # used for display in the answer and submission panels
    # Also creates invalid error messages
    invalid_format = False
    for i in range(m):
        for j in range(n):
            each_entry_name = name + str(n * i + j + 1)
            a_sub = data['submitted_answers'].get(each_entry_name, None)
            value, newdata = pl.string_fraction_to_number(a_sub,
                                                          allow_fractions,
                                                          allow_complex=False)
            if value is not None:
                A[i, j] = value
                data['submitted_answers'][each_entry_name] = newdata[
                    'submitted_answers']
            else:
                invalid_format = True
                data['format_errors'][each_entry_name] = newdata[
                    'format_errors']
                data['submitted_answers'][each_entry_name] = None

    if invalid_format:
        with open('pl-matrix-component-input.mustache', 'r',
                  encoding='utf-8') as f:
            data['format_errors'][name] = chevron.render(
                f, {
                    'format_error': True,
                    'allow_fractions': allow_fractions
                }).strip()
        data['submitted_answers'][name] = None
    else:
        data['submitted_answers'][name] = pl.to_json(A)
Esempio n. 2
0
def parse(element_html, data):
    element = lxml.html.fragment_fromstring(element_html)
    name = pl.get_string_attrib(element, 'answers-name')
    allow_complex = pl.get_boolean_attrib(element, 'allow-complex',
                                          ALLOW_COMPLEX_DEFAULT)
    allow_fractions = pl.get_boolean_attrib(element, 'allow-fractions',
                                            ALLOW_FRACTIONS_DEFAULT)

    a_sub = data['submitted_answers'].get(name, None)
    value, newdata = pl.string_fraction_to_number(a_sub, allow_fractions,
                                                  allow_complex)

    if value is not None:
        data['submitted_answers'][name] = newdata['submitted_answers']
    else:
        data['format_errors'][name] = get_format_string(
            allow_complex, allow_fractions, newdata['format_errors'])
        data['submitted_answers'][name] = None
Esempio n. 3
0
def parse(element_html, data):
    element = lxml.html.fragment_fromstring(element_html)
    name = pl.get_string_attrib(element, 'answers-name')
    allow_complex = pl.get_boolean_attrib(element, 'allow-complex', ALLOW_COMPLEX_DEFAULT)
    allow_fractions = pl.get_boolean_attrib(element, 'allow-fractions', ALLOW_FRACTIONS_DEFAULT)
    allow_blank = pl.get_boolean_attrib(element, 'allow-blank', ALLOW_BLANK_DEFAULT)
    blank_value = pl.get_string_attrib(element, 'blank-value', str(BLANK_VALUE_DEFAULT))

    a_sub = data['submitted_answers'].get(name, None)
    if allow_blank and a_sub is not None and a_sub.strip() == '':
        a_sub = blank_value
    value, newdata = pl.string_fraction_to_number(a_sub, allow_fractions, allow_complex)

    if value is not None:
        data['submitted_answers'][name] = newdata['submitted_answers']
    else:
        data['format_errors'][name] = get_format_string(allow_complex, allow_fractions, newdata['format_errors'])
        data['submitted_answers'][name] = None