コード例 #1
0
 def test_getitem(self):
     """
     Test getitem method
     """
     D = DynamicArray()
     D.add(56)
     self.assertEqual(D[0], 56)
コード例 #2
0
def nutrition_product(lst_nutr):
    """
    :param lst_nutr: the nutrition json dict
    :return: the information of carbohydrates, calories, fat, proteins
    """
    D = DynamicArray()
    dct_nutrition = dict()
    for i in range(len(lst_nutr)):
        for j in lst_nutr[i]:
            if j == 'carbohydrates':
                dct_nutrition[j] = lst_nutr[i][j]
            if j == 'calories':
                dct_nutrition[j] = lst_nutr[i][j]

            if j == 'fat':
                dct_nutrition[j] = lst_nutr[i][j]

            if j == 'proteins':
                dct_nutrition[j] = lst_nutr[i][j]

    for i in dct_nutrition:
        del dct_nutrition[i]['name']
        del dct_nutrition[i]['measurement']
        del dct_nutrition[i]['per_serving']
    D.add(dct_nutrition)
    return D
コード例 #3
0
 def test_str(self):
     """
     Test str method
     """
     D = DynamicArray()
     D.add('apple')
     self.assertEqual(str(D), 'apple ')
コード例 #4
0
 def test_lenght(self):
     """
     Tests the lenghth
     """
     D = DynamicArray()
     D.add('orange')
     D.add('appple')
     self.assertEqual(len(D), 2)
コード例 #5
0
 def test_chagesize(self):
     """
     Test change size method
     """
     D = DynamicArray()
     D.add(12)
     self.assertEqual(D._changesize(2), None)
     self.assertEqual(D._capacity, 2)
コード例 #6
0
def get_product_names(json_f):
    """
    :param json_f: the file with json dict
    :return: names of products found
    """
    D = DynamicArray()
    with open(json_f, 'r') as main_file:
        data = json.load(main_file)
    for i in data['products']:
        for el in data['products'][i]:
            if el == 'name':
                D.add(data['products'][i][el])
    return D
コード例 #7
0
def count_nutrition(grams, product_dict):
    """
    :param grams: grams consumed
    :param product_dict: the json dict of the product
    :return: product name, number of: carbohydrates, fat, protein, calories
    """
    D = DynamicArray()
    lst = []
    grams_consumed = grams / 100
    # print(grams_consumed)
    carbs = 0
    fat = 0
    protein = 0
    calories = 0
    try:
        if product_dict['carbohydrates']:
            carb_100g = product_dict['carbohydrates']['per_100g']
            carbs = carb_100g * grams_consumed
            carbs = round(carbs)
            # D.add(carbs)
            lst.append(carbs)
    except KeyError:
        # D.add(0)
        lst.append(0)

    try:
        if product_dict['proteins']:
            protein_100g = product_dict['proteins']['per_100g']
            protein = protein_100g * grams_consumed
            protein = round(protein)
            # D.add(protein)
            lst.append(protein)
    except KeyError:
        # D.add(0)
        lst.append(0)

    try:
        if product_dict['fat']:
            fat_100g = product_dict['fat']['per_100g']
            fat = fat_100g * grams_consumed
            fat = round(fat)
            # D.add(fat)
            lst.append(fat)
    except KeyError:
        # D.add(0)
        lst.append(0)

    try:
        if product_dict['calories']:
            calories_100g = product_dict['calories']['per_100g']
            calories = calories_100g * grams_consumed
            calories = round(calories)
            # D.add(calories)
            lst.append(calories)
    except KeyError:
        lst.append(0)

    return lst
コード例 #8
0
 def test_delete(self):
     """
     Test delete method
     """
     D = DynamicArray()
     D.add('fish')
     D.add('meat')
     self.assertEqual(D.delete_item('g'), "value not found")
     self.assertIsNone(D.delete_item('fish'), None)
コード例 #9
0
 def test_init(self):
     """
     Tests init of class
     """
     D = DynamicArray()
     D.add('pasta')
     self.assertEqual(D._size, 1)
     self.assertEqual(D._capacity, 1)
     D.add('pizza')
     self.assertEqual(D._size, 2)
     self.assertEqual(D._capacity, 2)
コード例 #10
0
def search_by_name(name_food):
    """
    :param name_food: the name of chosen product
    :return: the nutrition information of the product
    """
    with open("d.json", 'r') as f:
        data = json.load(f)
    D = DynamicArray()
    for i in data['products']:
        for el in data['products'][i]:
            if el == 'name':
                if data['products'][i][el] == name_food:
                    D.add(data['products'][i])
    D2 = DynamicArray()
    for i in range(len(D)):
        for j in D[i]:
            if j == 'details':
                D2.add(D[i][j])
    nutr = DynamicArray()
    for i in range(len(D2)):
        for j in D2[i]:
            if j == 'nutrition_label':
                nutr.add(D2[i][j])
    return nutr
コード例 #11
0
 def test_add(self):
     """
     Test add method
     """
     D = DynamicArray()
     self.assertEqual(D.add('d'), None)