Esempio n. 1
0
 def check_user_answer(self, user_answer):
     if sn_round(user_answer) == self.calculated_answer:
         return True
     elif sn_round(user_answer) == self.calculate_answer():
         return True
     else:
         return False
Esempio n. 2
0
 def variables_randomized(self) -> dict:
     """Return { variable_name: randomized_value }"""
     vars = dict()
     if self.variables_as_lists:
         for name, value in self.variables_as_lists.items():
             len_value = len(value)
             if len_value == 4:
                 if value[3]:
                     vars[name] = int(
                         sn_round(random.uniform(value[1], value[2])))
                 else:
                     vars[name] = sn_round(
                         random.uniform(value[1], value[2]))
             elif len_value == 3:
                 vars[name] = sn_round(random.uniform(value[1], value[2]))
             elif len_value == 2:
                 vars[name] = sn_round(random.uniform(value[0], value[1]))
             else:
                 vars[name] = sn_round(value[0])
         return vars
Esempio n. 3
0
 def variables_as_floats(self) -> dict:
     """Return { variable: value }"""
     if self.use_vars:
         return self.use_vars
     elif self.variables_with_values:
         return {
             key: sn_round(value[0])
             for key, value in self.variables_as_lists.items()
         }
     else:
         return dict()
Esempio n. 4
0
    def calculate_answer(self):
        """Compute the answer to a problem.

        Calculate the answer to a problem using the default values for all
        variables.

        """
        if self.use_vars:
            answer_template = Template(self.answer)
            answer = evaluate_answer(
                answer_template.safe_substitute(**self.use_vars))
        elif self.variables_with_values:
            answer_template = Template(self.answer)
            answer = evaluate_answer(
                answer_template.safe_substitute(**self.variables_as_floats))
        else:
            answer = evaluate_answer(self.answer)
        answer_rounded = sn_round(answer)
        return answer_rounded
Esempio n. 5
0
def update_problem_variables(apps, schema_editor):
    """Data migration."""
    try:
        for problem in Problem.objects.all():
            if problem.variable_names and problem.variable_default_values:
                current_variables = dict(
                    zip(problem.variable_names.split(','),
                        problem.variable_default_values.split(',')))
                current_variables = {
                    key.strip(): sn_round(float(value.strip()))
                    for key, value in current_variables.items()
                }
                problem.variables_with_values = ', '.join([
                    '{name}[{default_value}]'.format(name=key,
                                                     default_value=value)
                    for key, value in current_variables.items()
                ])
                problem.save()
    except OperationalError:
        """Suppress this error when the data migration is reapplied."""
        pass
    except AttributeError:
        """Suppress this error when the data migration is reapplied."""
        pass
Esempio n. 6
0
def validate_math_expression(math_expression: str):
    try:
        number = evaluate_answer(math_expression)
        sn_round(number)
    except:
        raise ValidationError('Not a number or mathematical expression.')
Esempio n. 7
0
 def clean_user_answer(self) -> float:
     """https://docs.djangoproject.com/en/2.2/ref/forms/validation/"""
     user_answer = evaluate_answer(self.cleaned_data['user_answer'])
     user_answer_rounded = sn_round(user_answer)
     return user_answer_rounded