scripts/body_calc.py
scripts/body_calc.pyBrowse 4 files
2,203 tokens
6,425 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env python32"""3body_calc.py — All-in-one fitness calculator.4 5Subcommands:6 bmi <weight_kg> <height_cm>7 tdee <weight_kg> <height_cm> <age> <M|F> <activity 1-5>8 1rm <weight> <reps>9 macros <tdee_kcal> <cut|maintain|bulk>10 bodyfat <M|F> <neck_cm> <waist_cm> [hip_cm] <height_cm>11 12No external dependencies — stdlib only.13"""14import sys15import math16 17 18def bmi(weight_kg, height_cm):19 h = height_cm / 10020 val = weight_kg / (h * h)21 if val < 18.5:22 cat = "Underweight"23 elif val < 25:24 cat = "Normal weight"25 elif val < 30:26 cat = "Overweight"27 else:28 cat = "Obese"29 print(f"BMI: {val:.1f} — {cat}")30 print()31 print("Ranges:")32 print(" Underweight : < 18.5")33 print(" Normal : 18.5 – 24.9")34 print(" Overweight : 25.0 – 29.9")35 print(" Obese : 30.0+")36 37 38def tdee(weight_kg, height_cm, age, sex, activity):39 if sex.upper() == "M":40 bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age + 541 else:42 bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age - 16143 44 multipliers = {45 1: ("Sedentary (desk job, no exercise)", 1.2),46 2: ("Lightly active (1-3 days/week)", 1.375),47 3: ("Moderately active (3-5 days/week)", 1.55),48 4: ("Very active (6-7 days/week)", 1.725),49 5: ("Extremely active (athlete + physical job)", 1.9),50 }51 52 label, mult = multipliers.get(activity, ("Moderate", 1.55))53 total = bmr * mult54 55 print(f"BMR (Mifflin-St Jeor): {bmr:.0f} kcal/day")56 print(f"Activity: {label} (x{mult})")57 print(f"TDEE: {total:.0f} kcal/day")58 print()59 print("Calorie targets:")60 print(f" Aggressive cut (-750): {total - 750:.0f} kcal/day")61 print(f" Fat loss (-500): {total - 500:.0f} kcal/day")62 print(f" Mild cut (-250): {total - 250:.0f} kcal/day")63 print(f" Maintenance : {total:.0f} kcal/day")64 print(f" Lean bulk (+250): {total + 250:.0f} kcal/day")65 print(f" Bulk (+500): {total + 500:.0f} kcal/day")66 67 68def one_rep_max(weight, reps):69 if reps < 1:70 print("Error: reps must be at least 1.")71 sys.exit(1)72 if reps == 1:73 print(f"1RM = {weight:.1f} (actual single)")74 return75 76 epley = weight * (1 + reps / 30)77 brzycki = weight * (36 / (37 - reps)) if reps < 37 else 078 lombardi = weight * (reps ** 0.1)79 avg = (epley + brzycki + lombardi) / 380 81 print(f"Estimated 1RM ({weight} x {reps} reps):")82 print(f" Epley : {epley:.1f}")83 print(f" Brzycki : {brzycki:.1f}")84 print(f" Lombardi : {lombardi:.1f}")85 print(f" Average : {avg:.1f}")86 print()87 print("Training percentages off average 1RM:")88 for pct, rep_range in [89 (100, "1"), (95, "1-2"), (90, "3-4"), (85, "4-6"),90 (80, "6-8"), (75, "8-10"), (70, "10-12"),91 (65, "12-15"), (60, "15-20"),92 ]:93 print(f" {pct:>3}% = {avg * pct / 100:>7.1f} (~{rep_range} reps)")94 95 96def macros(tdee_kcal, goal):97 goal = goal.lower()98 if goal in {"cut", "lose", "deficit"}:99 cals = tdee_kcal - 500100 p, f, c = 0.40, 0.30, 0.30101 label = "Fat Loss (-500 kcal)"102 elif goal in {"bulk", "gain", "surplus"}:103 cals = tdee_kcal + 400104 p, f, c = 0.30, 0.25, 0.45105 label = "Lean Bulk (+400 kcal)"106 else:107 cals = tdee_kcal108 p, f, c = 0.30, 0.30, 0.40109 label = "Maintenance"110 111 prot_g = cals * p / 4112 fat_g = cals * f / 9113 carb_g = cals * c / 4114 115 print(f"Goal: {label}")116 print(f"Daily calories: {cals:.0f} kcal")117 print()118 print(f" Protein : {prot_g:>6.0f}g ({p * 100:.0f}%) = {prot_g * 4:.0f} kcal")119 print(f" Fat : {fat_g:>6.0f}g ({f * 100:.0f}%) = {fat_g * 9:.0f} kcal")120 print(f" Carbs : {carb_g:>6.0f}g ({c * 100:.0f}%) = {carb_g * 4:.0f} kcal")121 print()122 print(f"Per meal (3 meals): P {prot_g / 3:.0f}g | F {fat_g / 3:.0f}g | C {carb_g / 3:.0f}g")123 print(f"Per meal (4 meals): P {prot_g / 4:.0f}g | F {fat_g / 4:.0f}g | C {carb_g / 4:.0f}g")124 125 126def bodyfat(sex, neck_cm, waist_cm, hip_cm, height_cm):127 sex = sex.upper()128 if sex == "M":129 if waist_cm <= neck_cm:130 print("Error: waist must be larger than neck."); sys.exit(1)131 bf = 86.010 * math.log10(waist_cm - neck_cm) - 70.041 * math.log10(height_cm) + 36.76132 else:133 if (waist_cm + hip_cm) <= neck_cm:134 print("Error: waist + hip must be larger than neck."); sys.exit(1)135 bf = 163.205 * math.log10(waist_cm + hip_cm - neck_cm) - 97.684 * math.log10(height_cm) - 78.387136 137 print(f"Estimated body fat: {bf:.1f}%")138 139 if sex == "M":140 ranges = [141 (6, "Essential fat (2-5%)"),142 (14, "Athletic (6-13%)"),143 (18, "Fitness (14-17%)"),144 (25, "Average (18-24%)"),145 ]146 default = "Obese (25%+)"147 else:148 ranges = [149 (14, "Essential fat (10-13%)"),150 (21, "Athletic (14-20%)"),151 (25, "Fitness (21-24%)"),152 (32, "Average (25-31%)"),153 ]154 default = "Obese (32%+)"155 156 cat = default157 for threshold, label in ranges:158 if bf < threshold:159 cat = label160 break161 162 print(f"Category: {cat}")163 print("Method: US Navy circumference formula")164 165 166def usage():167 print(__doc__)168 sys.exit(1)169 170 171def main():172 if len(sys.argv) < 2:173 usage()174 175 cmd = sys.argv[1].lower()176 177 try:178 if cmd == "bmi":179 bmi(float(sys.argv[2]), float(sys.argv[3]))180 181 elif cmd == "tdee":182 tdee(183 float(sys.argv[2]), float(sys.argv[3]),184 int(sys.argv[4]), sys.argv[5], int(sys.argv[6]),185 )186 187 elif cmd in {"1rm", "orm"}:188 one_rep_max(float(sys.argv[2]), int(sys.argv[3]))189 190 elif cmd == "macros":191 macros(float(sys.argv[2]), sys.argv[3])192 193 elif cmd == "bodyfat":194 sex = sys.argv[2]195 if sex.upper() == "M":196 bodyfat(sex, float(sys.argv[3]), float(sys.argv[4]), 0, float(sys.argv[5]))197 else:198 bodyfat(sex, float(sys.argv[3]), float(sys.argv[4]), float(sys.argv[5]), float(sys.argv[6]))199 200 else:201 print(f"Unknown command: {cmd}")202 usage()203 204 except (IndexError, ValueError) as e:205 print(f"Error: {e}")206 usage()207 208 209if __name__ == "__main__":210 main()Referenced from SKILL.md
SKILL.mdView in source ↗
Source excerpt starting at line 209.SKILL.mdView in source ↗209- `python scripts/body_calc.py bmi <weight_kg> <height_cm>`210- `python scripts/body_calc.py tdee <weight_kg> <height_cm> <age> <M|F> <activity 1-5>`211- `python scripts/body_calc.py 1rm <weight> <reps>`212- `python scripts/body_calc.py macros <tdee_kcal> <cut|maintain|bulk>`213- `python scripts/body_calc.py bodyfat <M|F> <neck_cm> <waist_cm> [hip_cm] <height_cm>`
Source excerpt starting at line 251.251| Food details | USDA | `GET /fdc/v1/food/{fdcId}` |252| BMI / TDEE / 1RM / macros | offline | `python scripts/body_calc.py` |