/
main.py
executable file
·210 lines (194 loc) · 10.8 KB
/
main.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python3
"""Application that reads out yoga moves to you."""
__version__="0.01"
__author__="Shulin Ye"
import collections
import colorama
import datetime
from enum import Enum
import random
import sys
import time
import dijkstras
import utils
import moves
import stretches
import strengthaerobics
colorama.init()
def routine(li : list, imbalance : list, playLast=True, **kwargs) -> "Move":
"""Plays a list of moves. if playLast = False, returns last move instead of playing it
If KeyboardInterrupt, tries to return the move it's currently on"""
try:
li = iter(li)
current = next(li)
for nextmove in li:
current = current(imbalance=imbalance, nextMove=nextmove, **kwargs)
except KeyboardInterrupt:
sys.stdout.write(colorama.Style.RESET_ALL)
raise
if not playLast: return current
return current(imbalance=imbalance,**kwargs)
def get_me_to(pose, *moves, imbalance = None, **kwargs):
if imbalance is None: imbalance = []
try:
pose = routine(dijkstras.dijkstra(pose, *moves, imbalance=imbalance), imbalance=imbalance, **kwargs)
except (TimeoutError, ValueError):
pose = pose(imbalance = imbalance, **kwargs)
sys.stdout.write(colorama.Style.RESET_ALL)
except KeyboardInterrupt:
sys.stdout.write(colorama.Style.RESET_ALL)
return pose
def fixImbalance(pose : "Move", imbalance : list, maxImbalance : int = 2, maxTime=60, **kwargs) -> "Move":
"""Might try to fix the imbalance. Might not. Depends on how big the imbalance is.
(Set maxImbalance to 1 to ensure imbalance gets fixed)
if KeyboardInterrupt: Immediately return whatever pose it was on."""
fixImbalanceChance = len(imbalance)/maxImbalance
if random.random() < fixImbalanceChance:
end = time.time() + maxTime
while imbalance and time.time() < end:
try:
pose = get_me_to(pose, *imbalance, imbalance=imbalance, **kwargs)
except KeyboardInterrupt:
sys.stdout.write(colorama.Style.RESET_ALL)
return pose
return pose
def main(**kwargs):
defaults = {
"time": 30,
"difficulty": 1,
"initial_move": "child",
"warmup": True,
"cooldown": True,
"aerobics": 0,
"strength": 0,
"target": "plank",
"verbose":1,
"memory":5,
"outfile": None,
}
defaults.update(kwargs)
if defaults["outfile"]:
f = open(defaults["outfile"], "a")
f.write(str(datetime.datetime.now()) + "\n")
f.write(str(defaults) + "\n")
else:
f = None
utils.speak('Beginning in')
utils.countdown(3)
if defaults["verbose"] >= 2:
print(utils.wrapper.fill(str(defaults)))
elif defaults["verbose"] >= 1:
print("Workout length:", defaults['time'], "minutes.", "Beginning in:")
# setup
total_time = defaults['time']*60
movesGraph = moves.generateMoves(difficulty=defaults["difficulty"])
stretches.defineStretches(movesGraph, difficulty=defaults["difficulty"])
start = time.time()
end = start + total_time
class Times(Enum):
warmup_end = start + min(max(45,total_time//15),300)
halfway = start + total_time//2.4 - 30
cooldown_begin = (end - max(60, total_time//5)) if defaults['cooldown'] else end
imbalance = []
prev = collections.deque([],defaults["memory"])
try:
pose = movesGraph[defaults['initial_move']]
except KeyError:
pose = movesGraph['child']
try:
#warmup
if defaults["warmup"]:
pose = pose(time=min(30,max(15, total_time//120+7)), imbalance=imbalance, prev=prev, verbose=defaults["verbose"], f=f)
while time.time() < Times.warmup_end.value:
pose = pose(imbalance=imbalance, extended=True, early=True, prev=prev, verbose=defaults["verbose"], f=f) #start slower
#get me to my target:
moves.linkMain(movesGraph, defaults['difficulty'])
if defaults['aerobics']:
strengthaerobics.linkAerobics(movesGraph, defaults["difficulty"], defaults["aerobics"])
if defaults['strength']:
strengthaerobics.linkStrength(movesGraph, defaults["difficulty"], defaults["strength"])
if defaults['aerobics']:
strengthaerobics.linkStrengthAerobics(movesGraph, defaults["difficulty"], defaults["strength"], defaults["aerobics"])
if defaults['warmup']:
pose = fixImbalance(pose, imbalance, maxTime=max(45,total_time//12.5), prev=prev, verbose=defaults['verbose'], f=f)
imbalance = moves.unlinkWarmup(movesGraph, imbalance=imbalance, difficulty=defaults["difficulty"])
try:
target = movesGraph[defaults['target']]
except KeyError:
target = movesGraph['plank']
pose = get_me_to(pose, target, imbalance=imbalance, playLast=False, prev=prev, verbose=defaults["verbose"], f=f)
if defaults["warmup"]:
utils.tee("Warmup Over: " + utils.prettyTime(time.time() - start), f, say="Alright, warmup over.")
pose = pose(imbalance=imbalance, prev=prev, verbose=defaults["verbose"], f=f)
#starting main part of workout
while time.time() < Times.halfway.value:
pose = fixImbalance(pose, imbalance, maxImbalance=10 + total_time//600, maxTime=max(60,total_time//12), prev=prev, verbose=defaults["verbose"], f=f)
pose = pose(imbalance=imbalance, prev=prev, verbose=defaults["verbose"], f=f)
#add harder poses in here
if defaults["difficulty"] >= 1:
moves.linkHarder(movesGraph, defaults["difficulty"])
if defaults["strength"]:
strengthaerobics.linkStrengthHarder(movesGraph, defaults["difficulty"], defaults["strength"])
pose = fixImbalance(pose, imbalance, maxTime=max(60, total_time//10), prev=prev, verbose=defaults["verbose"], f=f)
try:
pose = get_me_to(pose, movesGraph[defaults['target']], imbalance=imbalance, prev=prev, verbose=defaults["verbose"], f=f)
except KeyError:
pass
if defaults["verbose"] >= 1:
utils.tee("Halfway point: " + utils.prettyTime(time.time()-start), f, say="We have reached the halway point")
#end adding harder poses
harder = defaults["difficulty"] >= 1
while time.time() < Times.cooldown_begin.value:
extendedChance = (time.time() - start)/total_time
extended = random.random() < extendedChance
pose = fixImbalance(pose, imbalance, maxImbalance=8+total_time//800, maxTime=max(110,total_time//10), prev=prev, verbose=defaults["verbose"], \
f=f, harder=harder)
pose = pose(harder=harder, imbalance = imbalance, extended=extended, prev=prev, verbose=defaults["verbose"], f=f)
moves.linkEnding(movesGraph)
while time.time() < (end - max(60, total_time//10)):
pose = fixImbalance(pose, imbalance, maxImbalance=max(1,total_time//800), maxTime=max(120, total_time//8), prev=prev, verbose=defaults["verbose"], f=f)
pose = pose(imbalance = imbalance, prev=prev, verbose=defaults["verbose"], f=f)
if defaults["cooldown"]:
pose = fixImbalance(pose, imbalance, maxImbalance=1, maxTime=max(75, total_time//10+15), prev=prev, verbose=defaults["verbose"], f=f)
utils.tee("Cooldown begins: " + utils.prettyTime(time.time() - start), f, say="Cooldown begins")
stretches.linkCooldown(movesGraph, difficulty=defaults["difficulty"])
if defaults["strength"]: strengthaerobics.linkStrengthCooldown(movesGraph,difficulty=defaults["difficulty"], strength = defaults["strength"])
if defaults["aerobics"]: strengthaerobics.linkAerobicsCooldown(movesGraph,difficulty=defaults["difficulty"], aerobics = defaults["aerobics"])
pose = get_me_to(pose, movesGraph['wheel'], imbalance=imbalance, prev=prev, verbose=defaults['verbose'], f=f)
pose = fixImbalance(pose, imbalance, maxImbalance=1, maxTime=max(60, total_time//10), prev=prev, verbose=defaults['verbose'], f=f)
while time.time() < (end-max(30, total_time//10)) if defaults["cooldown"] else end:
#pose = pose(imbalance=imbalance, extended=True, prev=prev, verbose=defaults['verbose'], f=f)
pose = fixImbalance(pose, imbalance, maxImbalance=1, maxTime=max(30, total_time//10), prev=prev, verbose=defaults['verbose'], f=f)
if defaults['cooldown']:
moves.linkSavasana(movesGraph, difficulty=defaults['difficulty'])
pose = fixImbalance(pose, imbalance, maxImbalance=1, maxTime=max(30, total_time//10), prev=prev, verbose=defaults['verbose'], f=f)
pose = get_me_to(pose, movesGraph['savasana'], imbalance=imbalance, prev=prev, verbose=defaults['verbose'], f=f)
except (KeyboardInterrupt, BrokenPipeError):
moves.linkSavasana(movesGraph, difficulty=defaults['difficulty'])
pose = get_me_to(pose, movesGraph['savasana'], imbalance=imbalance, prev=prev, verbose=defaults['verbose'], f=f)
finally:
final_time = utils.prettyTime(time.time() - start)
utils.tee('\nTotal Time: %s' % final_time, f=f, say='Done! Total time was %s' % final_time.replace('(','').replace(')',''))
if f: f.close()
sys.stdout.write(colorama.Style.RESET_ALL)
print(utils.wrapper.fill(str(imbalance)))
return imbalance
if __name__== '__main__':
import argparse
parser = argparse.ArgumentParser(usage = './main.py [options]')
parser.add_argument('--version', action='version', version='yoga %s' % __version__)
parser.add_argument('-t', '--time', help='time (in minutes)', default=30, type=int)
parser.add_argument('-a', '--aerobics', help='Insert aerobics moves', action='count', default=0)
parser.add_argument('-s', '--strength', help='Insert strength moves', action='count', default=0)
parser.add_argument('-d', '--difficulty', help='Difficulty: larger number=harder', default=1, type=int, choices=[-1,0,1,2])
parser.add_argument('-w', '--skip-warmup', action='store_false', dest='warmup', help='skips warmup period')
parser.add_argument('-c', '--skip-cooldown', action='store_false', dest='cooldown', help='skips cooldown')
parser.add_argument('-i', '--initial-move', default='child', choices=['child', 'seatedMeditation', 'lieOnBack', 'mountain'])
parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument('--debug', action='store_true', help='Debug mode: all delays removed.')
parser.add_argument('-m', '--memory', default=5, type=int, help='How many previous moves shall I remember? (default: 5)')
parser.add_argument('--target', default='plank', choices=['plank', 'boat'])
parser.add_argument('-o', '--outfile', help='File to write log to')
args = parser.parse_args()
utils.DEBUG = args.debug
main(**vars(args))