]> Nutra Git (v2) - nutratech/cli.git/commitdiff
wip, but `anl` is working somewhat okay now again
authorShane Jaroch <chown_tee@proton.me>
Sun, 25 Feb 2024 20:23:57 +0000 (15:23 -0500)
committerShane Jaroch <chown_tee@proton.me>
Sun, 25 Feb 2024 20:23:57 +0000 (15:23 -0500)
ntclient/core/nutprogbar.py
ntclient/services/analyze.py

index 12deac9308bb5254bd27b83205bce65418d64d70..c9b9e921d1405cebc6ed17c1ff6aa9dcca1b6033 100644 (file)
@@ -10,57 +10,60 @@ def nutrient_progress_bars(
     _food_analyses: list,
     _nutrients: Mapping[int, list],
     # grams: float = 100,
-    width: int = 50,
-) -> dict:
-    """Returns progress bars, colorized, for foods analyses"""
-
-    def tally() -> int:
-        """Tally the progress bars, return n_skipped"""
+    # width: int = 50,
+) -> Mapping[int, float]:
+    """
+    Returns progress bars, colorized, for foods analyses
+    @TODO add option to scale up to 2000 kcal (or configured RDA value)
+    @TODO consider organizing the numbers into a table, with the colored bar in one slot
+    """
+
+    def print_bars() -> int:
+        """Print the progress bars, return n_skipped"""
         n_skipped = 0
-        for nut in nut_percs.items():
+        for nut in nut_amts.items():
             nutr_id, nutr_val = nut
+
             # Skip if nutr_val == 0.0
             if not nutr_val:
                 n_skipped += 1
                 continue
+
             # Print bars
             print_nutrient_bar(nutr_id, nutr_val, _nutrients)
+
         return n_skipped
 
-    for _food_analysis in _food_analyses:
-        print(_food_analysis)
+    # Organize data into a dict<food_id, dict<nutr_id, nutr_val>>
     food_analyses_dict = {
-        x[0]: {y[1]: y[2] for y in _food_analyses if y[0] == x[0]}
+        int(x[0]): {int(y[1]): float(y[2]) for y in _food_analyses if y[0] == x[0]}
+        # NOTE: each analysis is a list of tuples, i.e. (11233, 203, 2.92)
         for x in _food_analyses
     }
 
-    # print(food_ids)
-    # print(food_analyses)
-
+    # Tally the nutrient totals
     nut_amts = {}
-
     for food_id, grams in _food_amts.items():
         ratio = grams / 100.0
         analysis = food_analyses_dict[food_id]
         for nutrient_id, amt in analysis.items():
             if nutrient_id not in nut_amts:
-                nut_amts[nutrient_id] = amt * ratio
+                nut_amts[int(nutrient_id)] = amt * ratio
             else:
-                nut_amts[nutrient_id] += amt * ratio
-
-    nut_percs = {}
-
-    for nutrient_id, amt in nut_amts.items():
-        # TODO: if not rda, show raw amounts?
-        if isinstance(_nutrients[nutrient_id][1], float):
-            # print(type(_nutrients[nutrient_id][1]), _nutrients[nutrient_id])
-            nut_percs[nutrient_id] = round(amt / _nutrients[nutrient_id][1], 3)
-        else:
-            print(type(_nutrients[nutrient_id][1]), _nutrients[nutrient_id])
-            continue
-
-    tally()
-    return nut_percs
+                nut_amts[int(nutrient_id)] += amt * ratio
+
+    # nut_percs = {}
+    # for nutrient_id, amt in nut_amts.items():
+    #     # TODO: if not rda, show raw amounts?
+    #     if isinstance(_nutrients[nutrient_id][1], float):
+    #         # print(type(_nutrients[nutrient_id][1]), _nutrients[nutrient_id])
+    #         nut_percs[nutrient_id] = round(amt / _nutrients[nutrient_id][1], 3)
+    #     else:
+    #         print(type(_nutrients[nutrient_id][1]), _nutrients[nutrient_id])
+    #         continue
+
+    print_bars()
+    return nut_amts
 
 
 def print_nutrient_bar(
index e1ea96df62a11d57b9a55e47ced5ed492a2bcf0b..c9969a496f1e837802b894795a0693719ba62c2a 100644 (file)
@@ -198,7 +198,7 @@ def day_analyze(day_csv_paths: Sequence[str], rda_csv_path: str = str()) -> tupl
                 if CLI_CONFIG.debug:
                     substr = "{0} {1}".format(_rda, _nutrient[2]).ljust(12)
                     print("INJECT RDA: {0} -->  {1}".format(substr, _nutrient[4]))
-    nutrients = {x[0]: x for x in nutrients_lists}
+    nutrients = {int(x[0]): x for x in nutrients_lists}
 
     # Analyze foods
     foods_analysis = {}