-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·183 lines (150 loc) · 4.75 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
#!/usr/bin/env python3
import time
import os
import sys
# Do an initial OS check for imports
OPERATING_SYS = os.name
if OPERATING_SYS == 'nt':
from msvcrt import getch
elif OPERATING_SYS == 'posix':
from readchar import readchar
else:
print("Your OS is not supported", file=sys.stderr)
sys.exit(1)
def determine_read():
"""
returns the correct read key method to use based on the OS.
"""
if os.name == "nt":
return ord(getch())
elif os.name == "posix":
return ord(readchar())
# Store the associated key, its start time, total time, and whether it was
# pressed or not.
# {'key':
# {'total_time': float,
# 'start_time': float,
# 'key_pressed': bool
# }
# }
key_dict = {}
space_counter = 0
def main():
"""
Main execution of the program.
"""
global space_counter
get_actions()
begin_timing()
print("\n\nTotal time for individual actions")
for key in key_dict:
print(f"{chr(key)}", "{:.6g}".format(key_dict[key]['total_time']))
print("Counter: ", space_counter)
# Check if we're restarting or quitting
is_restarting()
def is_restarting():
"""
Determines if we are restarting the program or exiting.
"""
global space_counter
print("Press ENTER to run again OR ESC to quit...")
key = determine_read()
if key == 13: # ENTER KEY
key_dict.clear()
space_counter = 0
print("\n")
main()
elif key == 27: # ESC
return
else:
is_restarting()
def get_actions():
"""
Retrieve keys through input to represent timers.
"""
print("Enter as many keys as you want to represent an action (ESC to finish)\n")
while True:
key = determine_read()
if key == 27: # ESC
if not key_dict:
print("You haven't added any keys yet.")
continue
break
elif key == 32: # SPACE
print("Can not add SPACE key as a timer because that key is reserved for counting.\n")
continue
key_dict[key] = {
'total_time': 0,
'start_time': 0,
'key_pressed': False
}
print("Added", chr(key))
def print_pressed_keys():
"""
Prints any active timers
"""
print("==================")
print("Active timers:")
print("==================")
for key in key_dict:
if key_dict[key]['key_pressed']:
print(chr(key))
print("==================")
print("Counter: ", space_counter)
print("==================")
def is_timing():
"""
Checks if there are active keys
"""
for key in key_dict:
if key_dict[key]['key_pressed']:
return False
return True
def begin_timing():
"""
The timer portion of the program.
Uses the difference in time.time() to calculate time in between presses.
"""
global space_counter
print("""\n1. Press the key you want and the timer will begin for that action.
2. Press the same key to stop the timer for that action.
3. Press the SPACE key to increment a counter.
4. Press ESC to quit.""")
print("5. Press 9 to check if there are active timers.\n")
while True:
key = determine_read()
if key == 27 and is_timing():
break
elif key == 27 and not is_timing():
print("You still have these timers active")
print_pressed_keys()
continue
elif key == 57:
print_pressed_keys()
continue
elif key == 32: # SPACE KEY
space_counter += 1
print("Counter increased:", space_counter)
continue
if key in key_dict and not key_dict[key]['key_pressed']:
# If the key is being pressed for the first time,
# we get the start time and set our timer to start.
start_time = time.time()
key_dict[key]['start_time'] = start_time
key_dict[key]['key_pressed'] = True
print("Started timing for", chr(key))
elif key in key_dict and key_dict[key]['key_pressed']:
# If the key is being pressed for a second time,
# we stop our timer and calculate the total time it
# took in between presses.
start_time = key_dict[key]['start_time']
end_time = time.time()
key_dict[key]['total_time'] += end_time - start_time
# Print the time, but format it to 6 sig figs
print(f"\nTime for {chr(key)}", "{:.6g}".format(key_dict[key]['total_time']))
# Set our timer to False to stop timing
key_dict[key]['key_pressed'] = False
else:
print(f"{chr(key)} is an invalid action!")
if __name__ == "__main__":
main()