Example #1
0
 def test_parse_quantity_is_inverse_of_get_db_str(self):
     for U in unit.units:
         for u in U:
             for amount in TestQuantity.amounts:
                 with self.subTest(amount=amount, unit=u):
                     try:
                         quantity.parse_quantity(
                             quantity.Quantity(amount, u).get_db_str())
                     except ValueError as e:
                         raise self.failureException(
                             f'test raised {e.__class__.__name__} unexpectedly'
                         ) from e
Example #2
0
    def nutrition(self):
        nutrition_list = []
        for ingredient in self.recipe_ingredients:
            for nutrient in ingredient.nutrition:
                if nutrient['nutrient'] not in map(
                        operator.itemgetter('nutrient'), nutrition_list):
                    nutrient['sources'] = []
                    nutrient['sources'].append({
                        'name': ingredient.meta.name,
                        'quantity': nutrient['quantity'],
                        'id': ingredient.meta.id
                    })
                    nutrition_list.append(nutrient)
                else:
                    for nutrition in nutrition_list:
                        if nutrition['nutrient'] == nutrient['nutrient']:
                            aggregate_nutrient = self.recipe_ingredients[
                                0].sum_nutrient(nutrition['quantity'],
                                                nutrient['quantity'])
                            nutrition['quantity'] = str(aggregate_nutrient)
                            if 'sources' not in nutrition:
                                nutrition['sources'] = []
                            else:
                                nutrition['sources'].append({
                                    'name':
                                    ingredient.meta.name,
                                    'quantity':
                                    nutrient['quantity'],
                                    'id':
                                    ingredient.meta.id
                                })
        for nutrition in nutrition_list:
            for source in nutrition['sources']:
                if len(nutrition['sources']) > 1:
                    ratio = parse_quantity(
                        source['quantity']) / parse_quantity(
                            nutrition['quantity'])
                    source['relative'] = int(float(ratio.amount * 100))
                    source['quantity'] = parse_quantity(
                        source['quantity']).decimal
                else:
                    source['quantity'] = parse_quantity(
                        source['quantity']).decimal

        for nutrition in nutrition_list:
            nutrition['quantity'] = parse_quantity(
                nutrition['quantity']).decimal
        return nutrition_list
Example #3
0
 def get_context(self, name, value, attrs):
     context = super().get_context(name, value, attrs)
     try:
         context['widget']['value'] = parse_quantity(
             context['widget']['value'])
     except AttributeError:
         context['widget']['value'] = Quantity(1, 'serving')
     context['widget']['section'] = self.section
     return context
Example #4
0
 def setUp(self) -> None:
     self.quantity1 = quantity.parse_quantity('1/3 g')
     self.quantity2 = quantity.parse_quantity('2/3 g')
     self.quantity3 = quantity.parse_quantity('3 g')
     self.quantity4 = quantity.parse_quantity('1 g')
     self.quantity5 = quantity.parse_quantity('4 g')
     self.quantity6 = quantity.parse_quantity('4/3 g')
Example #5
0
 def nutrition(self):
     nutrition_list = []
     # try:
     nutrients = self.meta.nutrient
     if not nutrients:
         try:
             self.meta.load_nutrient()
             self.meta.save()
             nutrients = self.meta.nutrient
         except Exception as e:
             logger.exception(e)
     # except KeyError:
     #     # nutrients = self.meta.nutrient
     #     # if 'quantity' in nutrients:
     #     #     nutrients['quantity'] = parse_quantity(
     #     #         nutrients['quantity']).decimal
     #     # else:
     #     #     return nutrition_list
     #     # nutrition_list.append(nutrients)
     #     return nutrition_list
     for nutrient in nutrients:
         if nutrient['nutrient'] not in map(operator.itemgetter('nutrient'),
                                            nutrition_list):
             nutrient['quantity'] = parse_quantity(
                 nutrient['quantity']).mul_quantity(self.quantity).decimal
             nutrition_list.append(nutrient)
         else:
             nutrient['quantity'] = parse_quantity(
                 nutrient['quantity']).mul_quantity(self.quantity).decimal
             i = nutrition_list.index(
                 next(
                     filter(
                         lambda index: index.get('nutrient') == nutrient[
                             'nutrient'], nutrition_list)))
             nutrition_list[i]['quantity'] = str(
                 self.sum_nutrient(nutrition_list[i]['quantity'],
                                   nutrient['quantity']))
     return nutrition_list
Example #6
0
 def test_with_div_different_unit(self):
     result = self.quantity5 / quantity.parse_quantity('2000 mg')
     self.assertEqual(result, quantity.parse_quantity('2 g'))
Example #7
0
 def test_with_div_same_unit(self):
     result = self.quantity6 / self.quantity3
     self.assertEqual(result, quantity.parse_quantity('4/9 g'))
Example #8
0
 def test_with_mul_different_unit(self):
     result = self.quantity4 * quantity.parse_quantity('3000 mg')
     self.assertEqual(result, self.quantity3)
Example #9
0
 def test_add_with_different_unit(self):
     summation = quantity.parse_quantity('0.002 kg') + self.quantity4
     self.assertEqual(summation, quantity.parse_quantity('0.003 kg'))
Example #10
0
 def sum_nutrient(first_nutrient: str, second_nutrient: str) -> Quantity:
     result = parse_quantity(first_nutrient) + \
         parse_quantity(second_nutrient)
     return result.decimal