Example #1
0
    def test_subunits(self):
        """
        Test weight subunit calculations
        """

        tmp = AbstractWeight(10)
        self.assertEqual(tmp.g, 10000)

        tmp = AbstractWeight(2, 'lb')
        self.assertEqual(tmp.oz, 32)
Example #2
0
    def test_conversion_subunits(self):
        """
        Test the weight conversion with "subunits" (grams, ounces)
        """

        tmp = AbstractWeight(100.1, 'g')
        self.assertEqual(tmp.kg, Decimal(0.1001).quantize(FOURPLACES))

        tmp = AbstractWeight(100, 'g')
        self.assertEqual(tmp.kg, Decimal(0.1).quantize(FOURPLACES))

        tmp = AbstractWeight(1000, 'g')
        self.assertEqual(tmp.kg, 1)

        tmp = AbstractWeight(1.5, 'oz')
        self.assertEqual(tmp.lb, Decimal(0.0938).quantize(FOURPLACES))

        tmp = AbstractWeight(2, 'oz')
        self.assertEqual(tmp.lb, Decimal(0.125).quantize(FOURPLACES))

        tmp = AbstractWeight(4, 'oz')
        self.assertEqual(tmp.lb, Decimal(0.25).quantize(FOURPLACES))

        tmp = AbstractWeight(8, 'oz')
        self.assertEqual(tmp.lb, Decimal(0.5).quantize(FOURPLACES))

        tmp = AbstractWeight(16, 'oz')
        self.assertEqual(tmp.lb, 1)
Example #3
0
    def get_nutritional_values(self, use_metric=True):
        """
        Sums the nutritional info for the ingredient in the MealItem

        :param use_metric Flag that controls the units used
        """
        nutritional_info = {'energy': 0,
                            'protein': 0,
                            'carbohydrates': 0,
                            'carbohydrates_sugar': 0,
                            'fat': 0,
                            'fat_saturated': 0,
                            'fibres': 0,
                            'sodium': 0}
        # Calculate the base weight of the item
        if self.get_unit_type() == MEALITEM_WEIGHT_GRAM:
            item_weight = self.amount
        else:
            item_weight = (self.amount
                           * self.weight_unit.amount
                           * self.weight_unit.gram)

        nutritional_info['energy'] += self.ingredient.energy * item_weight / 100
        nutritional_info['protein'] += self.ingredient.protein * item_weight / 100
        nutritional_info['carbohydrates'] += self.ingredient.carbohydrates * item_weight / 100

        if self.ingredient.carbohydrates_sugar:
            nutritional_info['carbohydrates_sugar'] += \
                self.ingredient.carbohydrates_sugar * item_weight / 100

        nutritional_info['fat'] += self.ingredient.fat * item_weight / 100

        if self.ingredient.fat_saturated:
            nutritional_info['fat_saturated'] += self.ingredient.fat_saturated * item_weight / 100

        if self.ingredient.fibres:
            nutritional_info['fibres'] += self.ingredient.fibres * item_weight / 100

        if self.ingredient.sodium:
            nutritional_info['sodium'] += self.ingredient.sodium * item_weight / 100

        # If necessary, convert weight units
        if not use_metric:
            for key, value in nutritional_info.items():

                # Energy is not a weight!
                if key == 'energy':
                    continue

                # Everything else, to ounces
                nutritional_info[key] = AbstractWeight(value, 'g').oz

        nutritional_info['energy_kilojoule'] = Decimal(nutritional_info['energy']) * Decimal(4.184)

        # Only 2 decimal places, anything else doesn't make sense
        for i in nutritional_info:
            nutritional_info[i] = Decimal(nutritional_info[i]).quantize(TWOPLACES)

        return nutritional_info
Example #4
0
    def test_conversion(self):
        """
        Test the weight conversion
        """

        tmp = AbstractWeight(10)
        self.assertEqual(tmp.kg, 10)
        self.assertEqual(tmp.lb, Decimal(22.0462).quantize(FOURPLACES))

        tmp = AbstractWeight(10, 'lb')
        self.assertEqual(tmp.lb, 10)
        self.assertEqual(tmp.kg, Decimal(4.5359).quantize(FOURPLACES))

        tmp = AbstractWeight(0.4536)
        self.assertEqual(tmp.lb, Decimal(1))
        self.assertEqual(tmp.kg, Decimal(0.4536).quantize(FOURPLACES))

        tmp = AbstractWeight(80)
        self.assertEqual(tmp.lb, Decimal(176.3698).quantize(FOURPLACES))
        self.assertEqual(tmp.kg, 80)

        tmp = AbstractWeight(80, 'kg')
        self.assertEqual(tmp.lb, Decimal(176.3698).quantize(FOURPLACES))
        self.assertEqual(tmp.kg, 80)
Example #5
0
    def calculate_bmi(self):
        """
        Calculates the user's BMI

        Formula: weight/height^2
        - weight in kg
        - height in m
        """

        # If not all the data is available, return 0, otherwise the result
        # of the calculation below breaks django's template filters
        if not self.weight or not self.height:
            return 0

        weight = self.weight if self.use_metric else AbstractWeight(
            self.weight, 'lb').kg
        return weight / pow(self.height / decimal.Decimal(100), 2)
Example #6
0
    def calculate_basal_metabolic_rate(self, formula=1):
        '''
        Calculates the basal metabolic rate.

        Currently only the Mifflin-St.Jeor formula is supported
        '''
        factor = 5 if self.gender == self.GENDER_MALE else -161
        weight = self.weight if self.use_metric else AbstractWeight(self.weight, 'lb').kg

        try:
            rate = ((10 * weight)  # in kg
                    + (decimal.Decimal(6.25) * self.height)  # in cm
                    - (5 * self.age)  # in years
                    + factor)
        # Any of the entries is missing
        except TypeError:
            rate = 0

        return decimal.Decimal(str(rate)).quantize(TWOPLACES)
Example #7
0
    def test_sum(self):
        """
        Tests adding two abstract weights
        """
        weight1 = AbstractWeight(80, 'kg')
        weight2 = AbstractWeight(10, 'kg')
        sum = weight1 + weight2
        self.assertEqual(sum.kg, 90)

        weight1 = AbstractWeight(80, 'kg')
        weight2 = AbstractWeight(10, 'lb')
        sum = weight1 + weight2
        self.assertEqual(sum.kg, Decimal(84.5359).quantize(FOURPLACES))

        weight1 = AbstractWeight(80, 'lb')
        weight2 = AbstractWeight(10, 'lb')
        sum = weight1 + weight2
        self.assertEqual(sum.lb, Decimal(90))