-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
115 lines (104 loc) · 4.41 KB
/
timer.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
#!/usr/bin/python3
"""This is the main module for the Rubik's shuffler and timer project.
It calls directly or indirectly all other modules."""
import time
import sys
from shuffle import *
def print_ascii_cube():
"""Prints ascii art of a Rubik's cube."""
with open('cube.ascii', 'r') as _:
ascii_cube = _.readlines()
for line in ascii_cube:
print(line, end='')
def inspection_time(inspection_seconds):
"""Lets the user start a countdown for cube inspection time."""
_ = input('---\nInspection time: {} seconds. Press ENTER to start inspection (or `q` to quit)!'.format(inspection_seconds))
if _ == 'q':
exit()
while inspection_seconds:
print(inspection_seconds)
inspection_seconds -= 1
time.sleep(1)
print('Inspection time over.')
def time_solve(inspection_seconds):
"""Lets the user start a timer for cube solving."""
if inspection_seconds:
inspection_time(inspection_seconds)
_ = input('---\nPress ENTER to start timer and solve (or `q` to quit)...')
start_time = time.time()
if _ == 'q':
exit()
input('Started!\nTiming... Press ENTER to stop!')
end_time = time.time()
solve_time = round(end_time - start_time, 3)
print('Stopped.')
print('Solve time: {}'.format(solve_time))
return solve_time
def save_solve_to_session(solve_time, session):
"""Stores solve to session array as a tuple containing date/time and `solve_time`."""
solve = (time.ctime(), solve_time)
session.append(solve)
def display_session_solves(session_solves):
"""Prints the sessions solve times and average, tagging the best, worst and latest ones."""
if session_solves:
all_times = [solve[1] for solve in session_solves]
best = min(all_times)
worst = max(all_times)
total = 0
print('---\nSession results:')
for i, solve in enumerate(session_solves):
index = ' '
tag = ''
if len(session_solves) > 1:
index = str(i + 1) + '.'
if solve[1] == best:
tag += ' <- Best'
if solve[1] == worst:
tag += ' <- Worst'
if i == len(session_solves) - 1:
tag += ' (Last solve)'
print('{} {}\t{} seconds{}'.format(index, solve[0], solve[1], tag))
total += solve[1]
if len(session_solves) > 1:
average = round(total/len(all_times), 3)
print('Average solve: {}'.format(average))
def display_last_5_running_average(session_solves):
"""Prints the running average (not including best & worst) of the last 5 solves."""
if len(session_solves) >= 5:
last_5 = [solve[1] for solve in session_solves[-5:]]
last_5.remove(min(last_5))
last_5.remove(max(last_5))
running_average = round(sum(last_5)/len(last_5), 4)
print('Running average (not including best & worst) of last 5 solves: {}'.format(running_average))
def solve_cycle_unit(session_solves, inspection_seconds, shuffle_move_count, shuffle_move_spacing):
"""Displays current session results and cube shuffling instructions, then times a solve."""
#print_ascii_cube()
if session_solves:
display_session_solves(session_solves)
display_last_5_running_average(session_solves)
shuffle(shuffle_move_count, shuffle_move_spacing)
solve_time = time_solve(inspection_seconds)
save_solve_to_session(solve_time, session_solves)
def main(inspection_seconds, shuffle_move_count, shuffle_move_spacing):
"""Starts a session with an eternal loop of cube solvings."""
print('Lets cube!')
print_ascii_cube()
session_solves = []
while True:
solve_cycle_unit(session_solves, inspection_seconds, shuffle_move_count, shuffle_move_spacing)
if __name__ == '__main__':
INSPECTION_SECONDS = 0
SHUFFLE_MOVES = 30
SHUFFLE_MOVE_SPACE_FREQ = 10
try:
if len(sys.argv) > 1:
INSPECTION_SECONDS = abs(int(sys.argv[1]))
if len(sys.argv) > 2:
SHUFFLE_MOVES = abs(int(sys.argv[2]))
if len(sys.argv) > 3:
SHUFFLE_MOVE_SPACE_FREQ = abs(int(sys.argv[3]))
except:
print_ascii_cube()
print('Usage: $./timer.py [seconds for inspection] [amount of shuffle moves] [shuffle move space frequency]')
exit()
main(INSPECTION_SECONDS, SHUFFLE_MOVES, SHUFFLE_MOVE_SPACE_FREQ)