Skip to content

Instantly share code, notes, and snippets.

@madig
Last active May 21, 2024 21:12
Show Gist options
  • Save madig/6ff3b78c53f095d4d1114f67fc845a6d to your computer and use it in GitHub Desktop.
Save madig/6ff3b78c53f095d4d1114f67fc845a6d to your computer and use it in GitHub Desktop.
Calorie counting
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"\n",
"# Configure Jupyter to display the assigned value after an assignment\n",
"%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"@dataclass\n",
"class Food:\n",
" \"\"\"Something that can be eaten.\n",
"\n",
" Units are per 100g or 100ml.\n",
" \"\"\"\n",
"\n",
" carbs: int\n",
" fats: int\n",
" proteins: int\n",
"\n",
" @property\n",
" def kcal(self):\n",
" return round(self.carbs * 4 + self.fats * 9 + self.proteins * 4)\n",
"\n",
" def __repr__(self):\n",
" return f\"Food(carbs={round(self.carbs)}, fats={round(self.fats)}, proteins={round(self.proteins)}) -> {self.kcal} kcal\"\n",
"\n",
" def __mul__(self, other):\n",
" assert isinstance(other, (int, float))\n",
" return Food(\n",
" self.carbs * other,\n",
" self.fats * other,\n",
" self.proteins * other,\n",
" )\n",
"\n",
" def __add__(self, other):\n",
" assert isinstance(other, Food)\n",
" return Food(\n",
" self.carbs + other.carbs,\n",
" self.fats + other.fats,\n",
" self.proteins + other.proteins,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"def reduce_rice_g_to_compensate_for_kcal(rice: Food, other_kcal: int) -> int:\n",
" return round(other_kcal / rice.calories * 100)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Food(carbs=7, fats=4, proteins=1) -> 71 kcal"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sweet_potato = Food(20.7, 0.15, 2.0)\n",
"yasmin_rice = Food(85, 1.1, 6.4)\n",
"ms_pitta = Food(24.7, 1.3, 6)\n",
"\n",
"banana = Food(23, 0.3, 1.2)\n",
"banana_skinless = Food(20.3, 0.1, 1.2)\n",
"blueberries = Food(14.5, 0.3, 0.7)\n",
"conference_pear = Food(10, 0.1, 0.3)\n",
"kiwi = Food(6.2, 0.7, 0.6)\n",
"lidl_conference_pear = Food(10.9, 0.1, 0.3)\n",
"lidl_red_apple = Food(11.5, 0.5, 0.5)\n",
"morrisons_market_st_large_orange_200g = Food(16.4, 0.4, 1.6)\n",
"orange = Food(11.8, 0.1, 1.2)\n",
"# orange_peeled = Food(8.2, 0.2, 0.8)\n",
"pineapple = Food(13, 0.1, 0.5)\n",
"pink_lady_apple = Food(12, 0.5, 0.6)\n",
"strawberries = Food(7.7, 0.3, 0.7)\n",
"red_grapes = Food(17, 0.1, 0.6)\n",
"green_grapes = Food(15.2, 0.2, 0.7)\n",
"\n",
"salted_butter = Food(0.6, 82.2, 0.6)\n",
"skimmed_milk = Food(4.8, 1.7, 3.6)\n",
"whole_milk = Food(4.8, 3.6, 3.6)\n",
"yoguhrt = Food(6.4, 1.8, 4.3)\n",
"fage_5p_yogurt = Food(3, 5, 9)\n",
"ms_organic_hummous = Food(8.8, 12.2, 6.5)\n",
"pure_nature_greek_yoghurt = Food(4.6, 10, 4)\n",
"\n",
"cashew = Food(20, 48, 21)\n",
"oatmeal = Food(59, 8, 13)\n",
"mc_dougalls_self_raising_flour = Food(67.9, 1.4, 9.9)\n",
"sainsburys_baking_powder = Food(1.6, 0, 0) * 20\n",
"sainsburys_golden_caster_cane_sugar = Food(100, 0, 0)\n",
"\n",
"lidl_beef_10p = Food(0, 9.4, 19.8)\n",
"morrisons_beef_12p = Food(0, 11.8, 20.9)\n",
"morrisons_beef_15p = Food(0.2, 15.6, 18.6)\n",
"sainsburys_beef_12p = Food(0, 11.8, 25)\n",
"chicken_breast = Food(0, 1.5, 30)\n",
"sainsbury_scottish_salmon_fillets = Food(1.2, 17.3, 22.3)\n",
"morrison_large_egg = Food(0, 5.4, 7.5)\n",
"lean_beef_casserole_steak = Food(0.8, 4.3, 33.9)\n",
"tesco_salmon_pieces = Food(0, 13.1, 19.4)\n",
"tesco_beef_15p = Food(0, 14.5, 19.7)\n",
"medium_egg = Food(0.3, 4.7, 6.8)\n",
"casserole_steak = Food(0.8, 4.3, 33.9)\n",
"sainsburys_ranch_steak = Food(0, 5.1, 22.5)\n",
"\n",
"spinach = Food(0.2, 0.7, 3.2)\n",
"chantenay_carrots = Food(6, 0.5, 0.5)\n",
"carrot = Food(9.6, 0.2, 0.9)\n",
"zucchini = Food(2.3, 0.2, 1.3)\n",
"sweet_pepper = Food(4.1, 0.2, 0.8)\n",
"frozen_mixed_veg = Food(5, 0.6, 2.1)\n",
"celery = Food(1.4, 0.1, 0.5)\n",
"peas = Food(11.2, 0.7, 5.5)\n",
"\n",
"bulk_dextrose = Food(91, 0, 0)\n",
"bulk_dextrose_serving = bulk_dextrose * 0.25\n",
"myprotein_whey_powder = Food(6, 7.6, 72)\n",
"myprotein_whey_powder_serving = myprotein_whey_powder * 0.25\n",
"pbn_serving = Food(2.3, 1.8, 23)\n",
"philippines_whey_serving = Food(4, 1, 24)\n",
"serious_gainz_serving = Food(77.7, 4.7, 26.4)\n",
"\n",
"kitkat_2fingers = Food(12.4, 5.1, 1.4)\n",
"schokobons_piece = Food(3, 2.1, 0.5)\n",
"kinder_chocolate_piece = Food(6.7, 4.4, 1.1)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Breakfast: Food(carbs=120, fats=21, proteins=23) -> 760 kcal\n",
"Lunch: Food(carbs=161, fats=42, proteins=61) -> 1264 kcal\n",
"Shake: Food(carbs=48, fats=4, proteins=9) -> 266 kcal\n",
"Dinner: Food(carbs=161, fats=42, proteins=61) -> 1264 kcal\n",
"Snacks: Food(carbs=21, fats=0, proteins=2) -> 95 kcal\n"
]
},
{
"data": {
"text/plain": [
"Food(carbs=513, fats=108, proteins=157) -> 3649 kcal"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Beef day\n",
"\n",
"breakfast = (\n",
" banana_skinless * 1.2 + oatmeal * 1.3 + pure_nature_greek_yoghurt + blueberries * 1\n",
")\n",
"\n",
"lunch = (\n",
" morrisons_beef_15p * 2.5\n",
" + sweet_potato * 1\n",
" + yasmin_rice * 1.5\n",
" + spinach * 0.5\n",
" + sweet_pepper * 0.75\n",
" + carrot * 1\n",
" # + salted_butter * 0.025\n",
")\n",
"\n",
"shake = blueberries + banana_skinless * 1.2 + skimmed_milk * 2\n",
"\n",
"dinner = (\n",
" morrisons_beef_15p * 2.5\n",
" + sweet_potato * 1\n",
" + yasmin_rice * 1.5\n",
" + spinach * 0.5\n",
" + sweet_pepper * 0.75\n",
" + carrot * 1\n",
" # + salted_butter * 0.025\n",
")\n",
"\n",
"snacks = (\n",
" orange * 1.8\n",
" # + medium_egg * 2\n",
" # + kiwi * 1\n",
" # + green_grapes * 0.5\n",
" # + kiwi * 1.1\n",
" # + tesco_salmon_pieces * 1\n",
" # + sweet_potato * 0.2\n",
" # + Food(3.4, 1.7, 1.9) *2# Prawn Tempura\n",
" # + banana\n",
" # + kinder_chocolate_piece\n",
" # + conference_pear\n",
" # + schokobons_piece * 2\n",
" # + morrisons_market_st_large_orange_200g * 1\n",
" # + ms_pitta\n",
" # + kitkat_2fingers\n",
" # + strawberries * 1.4\n",
" # + conference_pear\n",
" # + bulk_dextrose_serving\n",
" # + pineapple * 0.4\n",
")\n",
"\n",
"print(\"Breakfast:\", breakfast)\n",
"print(\"Lunch: \", lunch)\n",
"print(\"Shake: \", shake)\n",
"print(\"Dinner: \", dinner)\n",
"print(\"Snacks: \", snacks)\n",
"breakfast + lunch + shake + dinner + snacks"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Food(carbs=0, fats=26, proteins=115) -> 693 kcal"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sainsburys_ranch_steak * 2.55 * 2"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Food(carbs=476, fats=85, proteins=196) -> 3449 kcal"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Salmon day\n",
"\n",
"(\n",
" # Breakfast\n",
" (banana + oatmeal * 1.2 + whole_milk * 2 + blueberries * 1)\n",
" # Lunch\n",
" + (\n",
" tesco_salmon_pieces * 2.2\n",
" + sweet_potato * 1.0\n",
" + yasmin_rice * 1.5\n",
" + spinach * 0.5\n",
" + salted_butter * 0.035\n",
" )\n",
" # Shake\n",
" + (blueberries + pbn_serving + whole_milk * 2)\n",
" # Dinner\n",
" + (\n",
" lean_beef_casserole_steak * 2\n",
" + sweet_potato * 1.0\n",
" + yasmin_rice * 1.5\n",
" + spinach * 0.5\n",
" + chantenay_carrots * 1.07\n",
" + sweet_pepper * 0.5\n",
" + salted_butter * 0.035\n",
" )\n",
" # Snacks\n",
" + lidl_red_apple * 0.5\n",
" + kitkat_2fingers\n",
" + kinder_chocolate_piece\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Food(carbs=23, fats=10, proteins=10) -> 221 kcal"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Pancakes\n",
"\n",
"pancakes = (\n",
" mc_dougalls_self_raising_flour * 2\n",
" + whole_milk * 2.05\n",
" + morrison_large_egg * 3\n",
" + salted_butter * 0.3\n",
" + sainsburys_baking_powder * 0.075\n",
" + sainsburys_golden_caster_cane_sugar * 0.15\n",
" + morrison_large_egg * 3\n",
")\n",
"\n",
"one_pan_pancake = pancakes * (1 / 7)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment