def serialize_assessments(assessments_root, oa_block): """ Serialize the assessment modules for an OpenAssessment XBlock. Args: assessments_root (lxml.etree.Element): The <assessments> XML element. oa_block (OpenAssessmentXBlock): The XBlock with configuration to serialize. Returns: None """ for assessment_dict in update_assessments_format( oa_block.rubric_assessments): assessment = etree.SubElement(assessments_root, 'assessment') # Set assessment attributes, defaulting to empty values assessment.set('name', unicode(assessment_dict.get('name', ''))) if 'must_grade' in assessment_dict: assessment.set('must_grade', unicode(assessment_dict['must_grade'])) if 'must_be_graded_by' in assessment_dict: assessment.set('must_be_graded_by', unicode(assessment_dict['must_be_graded_by'])) if assessment_dict.get('start') is not None: assessment.set('start', unicode(assessment_dict['start'])) if assessment_dict.get('due') is not None: assessment.set('due', unicode(assessment_dict['due'])) if assessment_dict.get('algorithm_id') is not None: assessment.set('algorithm_id', unicode(assessment_dict['algorithm_id'])) # Training examples examples = assessment_dict.get('examples', []) if not isinstance(examples, list): examples = [] serialize_training_examples(examples, assessment)
def serialize_assessments(assessments_root, oa_block): """ Serialize the assessment modules for an OpenAssessment XBlock. Args: assessments_root (lxml.etree.Element): The <assessments> XML element. oa_block (OpenAssessmentXBlock): The XBlock with configuration to serialize. Returns: None """ for assessment_dict in update_assessments_format(oa_block.rubric_assessments): assessment = etree.SubElement(assessments_root, 'assessment') # Set assessment attributes, defaulting to empty values assessment.set('name', unicode(assessment_dict.get('name', ''))) if 'must_grade' in assessment_dict: assessment.set('must_grade', unicode(assessment_dict['must_grade'])) if 'must_be_graded_by' in assessment_dict: assessment.set('must_be_graded_by', unicode(assessment_dict['must_be_graded_by'])) if assessment_dict.get('start') is not None: assessment.set('start', unicode(assessment_dict['start'])) if assessment_dict.get('due') is not None: assessment.set('due', unicode(assessment_dict['due'])) if assessment_dict.get('algorithm_id') is not None: assessment.set('algorithm_id', unicode(assessment_dict['algorithm_id'])) if assessment_dict.get('required') is not None: assessment.set('required', unicode(assessment_dict['required'])) # Training examples examples = assessment_dict.get('examples', []) if not isinstance(examples, list): examples = [] serialize_training_examples(examples, assessment)
def serialize_examples_to_xml_str(assessment): """ Serializes the OpenAssessment XBlock's training examples into an XML unicode string. Args: assessment (dict): Dictionary representation of an Assessment Module's configuration. If this contains a list of examples, the examples will be returned serialized. Returns: A unicode string of the XML serialized examples. """ examples = update_assessments_format([assessment])[0].get('examples', []) if not isinstance(examples, list): examples = [] examples_root = etree.Element('examples') serialize_training_examples(examples, examples_root) return etree.tostring(examples_root, pretty_print=True, encoding='unicode')