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) # Get submitted answer or return parse_error if it does not exist a_sub = data['submitted_answers'].get(name, None) if a_sub is None: data['format_errors'][name] = 'No submitted answer.' data['submitted_answers'][name] = None return # Convert to float or complex try: a_sub_parsed = pl.string_to_number(a_sub, allow_complex=allow_complex) if a_sub_parsed is None: raise ValueError('invalid submitted answer (wrong type)') if not np.isfinite(a_sub_parsed): raise ValueError('invalid submitted answer (not finite)') data['submitted_answers'][name] = pl.to_json(a_sub_parsed) except Exception: if allow_complex: data['format_errors'][ name] = 'Invalid format. The submitted answer could not be interpreted as a double-precision floating-point or complex number.' else: data['format_errors'][ name] = 'Invalid format. The submitted answer could not be interpreted as a double-precision floating-point number.' data['submitted_answers'][name] = None
def parse(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') # 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) if a_sub is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(No submitted answer)' invalid_format = True elif not a_sub: data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(Invalid blank entry)' invalid_format = True else: a_sub_parsed = pl.string_to_number(a_sub, allow_complex=False) if a_sub_parsed is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(Invalid format)' invalid_format = True elif not np.isfinite(a_sub_parsed): data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(Invalid format - not finite)' invalid_format = True else: data['submitted_answers'][each_entry_name] = pl.to_json( a_sub_parsed) A[i, j] = a_sub_parsed 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 }).strip() data['submitted_answers'][name] = None else: data['submitted_answers'][name] = pl.to_json(A)
def parse(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') # 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) if a_sub is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(No submitted answer)' invalid_format = True elif not a_sub: data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(Invalid blank entry)' invalid_format = True else: a_sub_parsed = pl.string_to_number(a_sub, allow_complex=False) if a_sub_parsed is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(Invalid format)' invalid_format = True elif not np.isfinite(a_sub_parsed): data['submitted_answers'][each_entry_name] = None data['format_errors'][ each_entry_name] = '(Invalid format - not finite)' invalid_format = True else: data['submitted_answers'][each_entry_name] = pl.to_json( a_sub_parsed) A[i, j] = a_sub_parsed if invalid_format: data['format_errors'][ name] = 'At least one of the entries has invalid format (empty entries or not a double precision floating point number)' data['submitted_answers'][name] = None else: data['submitted_answers'][name] = pl.to_json(A)
def parse(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') # 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) if a_sub is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(No submitted answer)' invalid_format = True elif not a_sub: data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(Invalid blank entry)' invalid_format = True else: a_sub_parsed = pl.string_to_number(a_sub, allow_complex=False) if a_sub_parsed is None: data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(Invalid format)' invalid_format = True elif not np.isfinite(a_sub_parsed): data['submitted_answers'][each_entry_name] = None data['format_errors'][each_entry_name] = '(Invalid format - not finite)' invalid_format = True else: data['submitted_answers'][each_entry_name] = pl.to_json(a_sub_parsed) A[i, j] = a_sub_parsed if invalid_format: data['format_errors'][name] = 'At least one of the entries has invalid format (empty entries or not a double precision floating point number)' data['submitted_answers'][name] = None else: data['submitted_answers'][name] = pl.to_json(A)
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) # Get submitted answer or return parse_error if it does not exist a_sub = data['submitted_answers'].get(name, None) if a_sub is None: data['format_errors'][name] = 'No submitted answer.' data['submitted_answers'][name] = None return if a_sub.strip() == '': data['format_errors'][name] = get_format_string( allow_complex, allow_fractions, 'the submitted answer was blank.') data['submitted_answers'][name] = None return # support FANCY division characters a_sub = a_sub.replace(u'\u2215', '/') # unicode / a_sub = a_sub.replace(u'\u00F7', '/') # division symbol, because why not if a_sub.count('/') == 1: # Specially handle fractions. if allow_fractions: a_sub_splt = a_sub.split('/') try: a_parse_l = pl.string_to_number(a_sub_splt[0], allow_complex=allow_complex) a_parse_r = pl.string_to_number(a_sub_splt[1], allow_complex=allow_complex) if a_parse_l is None or not np.isfinite(a_parse_l): raise ValueError( 'the numerator could not be interpreted as a decimal number.' ) if a_parse_r is None or not np.isfinite(a_parse_r): raise ValueError( 'the denominator could not be interpreted as a decimal number.' ) a_frac = a_parse_l / a_parse_r if not np.isfinite(a_frac): raise ValueError( 'The submitted answer is not a finite number.') data['submitted_answers'][name] = pl.to_json(a_frac) except ZeroDivisionError: data['format_errors'][name] = get_format_string( allow_complex, allow_fractions, 'your fraction resulted in a division by zero.') data['submitted_answers'][name] = None except Exception as error: data['format_errors'][name] = get_format_string( allow_complex, allow_fractions, error) data['submitted_answers'][name] = None else: data['format_errors'][name] = get_format_string( allow_complex, allow_fractions, 'fractional answers are not allowed.') data['submitted_answers'][name] = None else: # Not a fraction, just convert to float or complex try: a_sub_parsed = pl.string_to_number(a_sub, allow_complex=allow_complex) if a_sub_parsed is None: raise ValueError('invalid submitted answer (wrong type)') if not np.isfinite(a_sub_parsed): raise ValueError('invalid submitted answer (not finite)') data['submitted_answers'][name] = pl.to_json(a_sub_parsed) except Exception: data['format_errors'][name] = get_format_string( allow_complex, allow_fractions) data['submitted_answers'][name] = None