Example #1
0
    def _convert_state_v37_dict_to_v38_dict(cls, question_state_dict):
        """Converts from version 37 to 38. Version 38 adds a customization arg
        for the Math interactions that allows creators to specify the letters
        that would be displayed to the learner.

        Args:
            question_state_dict: dict. A dict where each key-value pair
                represents respectively, a state name and a dict used to
                initialize a State domain object.

        Returns:
            dict. The converted question_state_dict.
        """
        if question_state_dict['interaction']['id'] in (
                'AlgebraicExpressionInput', 'MathEquationInput'):
            variables = set()
            for group in question_state_dict[
                    'interaction']['answer_groups']:
                for rule_spec in group['rule_specs']:
                    rule_input = rule_spec['inputs']['x']
                    for variable in expression_parser.get_variables(
                            rule_input):
                        # Replacing greek letter names with greek symbols.
                        if len(variable) > 1:
                            variable = (
                                constants.GREEK_LETTER_NAMES_TO_SYMBOLS[
                                    variable])
                        variables.add(variable)

            customization_args = question_state_dict[
                'interaction']['customization_args']
            customization_args.update({
                'customOskLetters': {
                    'value': sorted(variables)
                }
            })

        return question_state_dict
 def test_get_variables(self):
     """Tests for get_variables method."""
     self.assertItemsEqual(expression_parser.get_variables('a^2.3'), ['a'])
     self.assertItemsEqual(
         expression_parser.get_variables('abs(alpha)'), ['alpha'])
     self.assertItemsEqual(
         expression_parser.get_variables('alpha/gamma'), ['alpha', 'gamma'])
     self.assertEqual(expression_parser.get_variables('A + 2/3'), ['A'])
     self.assertItemsEqual(
         expression_parser.get_variables('alphabetagamma'),
         ['alpha', 'beta', 'gamma'])
     self.assertItemsEqual(
         expression_parser.get_variables('betalphaa'),
         ['a', 'p', 'beta', 'l', 'h'])
     self.assertItemsEqual(
         expression_parser.get_variables('a+a*a/aa^a-a'), ['a'])
     self.assertItemsEqual(expression_parser.get_variables(
         'sqrt(3+x^y)/abs(gamma)'), ['y', 'x', 'gamma'])
     self.assertItemsEqual(
         expression_parser.get_variables('a=3+4'), ['a'])
     self.assertItemsEqual(expression_parser.get_variables(
         '(a-2)^beta = alpha/gamma'), ['a', 'alpha', 'beta', 'gamma'])
     self.assertItemsEqual(
         expression_parser.get_variables('4=abs(-4)'), [])
     self.assertItemsEqual(
         expression_parser.get_variables('a^pi + e/2'), ['a', 'pi', 'e'])
     self.assertItemsEqual(
         expression_parser.get_variables('pi-3.14e'), ['pi', 'e'])
     self.assertItemsEqual(
         expression_parser.get_variables('epi'), ['pi', 'e'])