-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkout.py
More file actions
134 lines (110 loc) · 4.08 KB
/
Copy pathworkout.py
File metadata and controls
134 lines (110 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sqlite3
# ==========================
# 1️⃣ DATABASE INITIALIZATION
# ==========================
def init_db():
conn = sqlite3.connect("workouts.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY,
date TEXT NOT NULL
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS workouts (
id INTEGER PRIMARY KEY,
session_id INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS exercises (
id INTEGER PRIMARY KEY,
workout_id INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY (workout_id) REFERENCES workouts (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS sets (
id INTEGER PRIMARY KEY,
exercise_id INTEGER NOT NULL,
reps INTEGER,
weight REAL,
duration_sec INTEGER,
FOREIGN KEY (exercise_id) REFERENCES exercises (id)
);
""")
conn.commit()
return conn, cursor
# ==========================
# 2️⃣ INSERTION HELPER FUNCS
# ==========================
def add_session(cursor, date):
cursor.execute("INSERT INTO sessions (date) VALUES (?)", (date,))
return cursor.lastrowid
def add_workout(cursor, session_id, name):
cursor.execute("INSERT INTO workouts (session_id, name) VALUES (?,?)", (session_id, name))
return cursor.lastrowid
def add_exercise(cursor, workout_id, name):
cursor.execute("INSERT INTO exercises (workout_id, name) VALUES (?,?)", (workout_id, name))
return cursor.lastrowid
def add_set(cursor, exercise_id, reps=None, weight=None, duration_sec=None):
cursor.execute("""
INSERT INTO sets (exercise_id, reps, weight, duration_sec) VALUES (?,?,?,?)
""", (exercise_id, reps, weight, duration_sec))
# ==========================
# 3️⃣ QUERY FUNCTION
# ==========================
def get_workout(cursor, session_id):
cursor.execute("""
SELECT e.name, s.reps, s.weight, s.duration_sec
FROM workouts w
JOIN exercises e ON w.id = e.workout_id
JOIN sets s ON e.id = s.exercise_id
WHERE w.session_id = ?;
""", (session_id,))
results = cursor.fetchall()
return results
# ==========================
# 4️⃣ MAIN CLI
# ==========================
def main():
conn, cursor = init_db()
# Session
date = input("Enter session date (YYYY-MM-DD): ")
session_id = add_session(cursor, date)
# Workout
workout_name = input("Enter workout name (e.g., 'Upper Body', 'Leg Day'): ").strip()
workout_id = add_workout(cursor, session_id, workout_name)
while True:
exercise_name = input("\nEnter exercise name (or 'done' to finish): ").strip()
if exercise_name.lower() == 'done':
break
exercise_id = add_exercise(cursor, workout_id, exercise_name)
while True:
mode = input(f"Enter 'reps' for repetitions, 'duration' for a timed exercise, or 'next' for next exercise: ").strip().lower()
if mode == 'next':
break
if mode == 'reps':
reps = int(input("Enter number of repetitions: "))
weight = float(input("Enter weight (negative for assisted): "))
add_set(cursor, exercise_id, reps=reps, weight=weight)
elif mode == 'duration':
duration_sec = int(input("Enter duration in seconds: "))
weight = float(input("Enter weight (negative for assisted, or 0 if none): "))
add_set(cursor, exercise_id, weight=weight, duration_sec=duration_sec)
conn.commit()
# Print results
results = get_workout(cursor, session_id)
print("\nYour Session Results:\n----------------------")
for name, reps, weight, duration_sec in results:
if duration_sec:
print(f"Exercise: {name}, Duration: {duration_sec} seconds, Weight: {weight}")
else:
print(f"Exercise: {name}, Reps: {reps}, Weight: {weight}")
conn.close()
if __name__ == '__main__':
main()