Example #1
0
 def create_unit_hint(cls, from_unit, to_unit):
     converted_value = convert(from_unit, 1, to_unit)
     if converted_value.magnitude > 0.001:
         value = format_quantity(converted_value)
         return TextHint(text='{} is {}'.format(format_value(from_unit, 1), value))
     else:
         converted_value = convert(to_unit, 1, from_unit)
         value = format_quantity(converted_value)
         return TextHint(text='{} is {}'.format(format_value(to_unit, 1), value))
Example #2
0
    def explanation(self):
        correct_question = [x for x in self.question.answers if x.correct and x.unit != self.unit]
        if len(correct_question) == 1:
            converted_value = convert.convert(self.unit, self.value, correct_question[0].unit)

            if converted_value.magnitude > 0.001:
                return convert.format_quantity(converted_value)
            else:
                # the value is below the required precision so we cannot use it
                return None
        else:
            return None
Example #3
0
    def explanation(self):
        min_unit = None
        for question in self.question.answers:
            normalized = convert.to_normalized(question.unit, 1)
            if min_unit is None or convert.to_normalized(min_unit, 1) > normalized:
                min_unit = question.unit

        if min_unit and min_unit != self.unit:
            converted_value = convert.convert(self.unit, self.value, min_unit)
            if converted_value.magnitude > 0.001:
                return convert.format_quantity(converted_value)
            else:
                # the value is below the required precision so we cannot use it
                return None
        else:
            return None