-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
45 lines (34 loc) · 959 Bytes
/
utils.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
"""
Utility functions for hangman.
"""
import msvcrt
import sys
import time
class TimeoutExpired(Exception):
"""
TimeoutExpired is raised when user doesn't input anything until given number of seconds.
"""
pass
def input_with_timeout(prompt: str, timeout: int, timer=time.monotonic) -> str | None:
"""
Time limited input function.
Only works on windows for now.
"""
sys.stdout.write(prompt)
sys.stdout.flush()
endtime = timer() + timeout
result = []
while timer() < endtime:
if msvcrt.kbhit():
result.append(msvcrt.getwche())
if result[-1] == "\r":
return "".join(result[:-1])
time.sleep(0.04) # just to yield to other processes/threads
raise TimeoutExpired
if __name__ == "__main__":
try:
answer = input_with_timeout("heya", 5)
except TimeoutExpired:
print("welp")
else:
print("this you?", answer)