-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypingtest.py
104 lines (88 loc) · 2.87 KB
/
typingtest.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
#run this from the command line only
import msvcrt
import time
import multiprocessing
import random
import wikipedia #sorry, i'm lazy
from os import system, name
#do something about the global variables - options file?
time_limit = 60
print_width = 50
# https://www.geeksforgeeks.org/clear-screen-python/
def clear_screen():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
def test_user_on_character(goal_char):
input_char = ''
while input_char != goal_char:
input_char = msvcrt.getch().decode()
# and then return and reset timer, restart forcibly
# make sure it all gets printed nice
# and display wpm when its done
def test_user_on_string(text):
clear_screen()
for index in range(len(text)):
final_index = min(len(text),index+print_width)
print(text[index:final_index])
goal_char = text[index]
test_user_on_character(goal_char)
clear_screen()
print("END OF STRING REACHED") #is there a way to kill the sleep?
def run_function_with_timer(function,*args):
p = multiprocessing.Process(target=function, name="function", args=(*args,)) #messy *args handling
p.start()
time.sleep(time_limit)
p.terminate()
p.join()
def create_word_list():
f = open("resources/1-1000.txt", "r")
wordlist = []
for line in f:
wordlist.append(line.strip()+" ")
f.close()
random.shuffle(wordlist)
#Once again, not a great way to do this
textline = ""
for word in wordlist:
textline += word
return textline
def get_value(message):
value = ""
while not isinstance(value,int):
value = input(message)
try:
value = int(value)
except:
value = ""
return value
def get_string():
print("1. Fixed string")
print("2. 1000 common words")
print("3. Wikipedia")
index = get_value("Input mode:")
#I know there's a better way to do this but I don't care.
if index == 1:
message = "That way, Python will recognise it as a tuple with a single element, as intended. Currently, Python is interpreting your code as just a string. However, it's failing in this particular way because a string is effectively a list of characters."
if index == 2:
message = create_word_list()
if index == 3:
message = get_wikipedia()
return message
def get_wikipedia():
raw_text = wikipedia.summary(wikipedia.random())
clean_text = raw_text.encode("ascii","ignore").decode("ascii") #bruh
return clean_text.replace("//","")
if __name__ == '__main__':
message = get_string()
run_function_with_timer(test_user_on_string,message)
#Plan:
#3 strikes out
#60 second time limit
#display active 50 characters
#option to get text from wikipedia
#or just random words from common word dictionary
#TODO:
#fix flickering text
#display stats