generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
454 lines (433 loc) · 16.3 KB
/
run.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""This is the Zen of Typing main app script"""
# Standard library imports
from __future__ import print_function, unicode_literals
import random
import sys
import textwrap
import time
# Third-party imports
import pyjokes
from PyInquirer import prompt
from examples import custom_style_2
# dictionary of target texts
texts = {
"dry": "Don't Repeat Yourself (DRY)",
"oop": "Object-Oriented Programming (OOP)",
"python": "The History of Python: Version 3",
"sunscreen": "Everybody’s Free (To Wear Sunscreen)",
"zen": "The Zen of Python",
"jokes": 'pyjokes: "programmer jokes as a service"'
}
# code block adapted from
# https://stackoverflow.com/questions/287871/how-to-print-colored-text-to-the-terminal/39452138#39452138
colours = {
'CEND': '\33[0m',
'CBOLD': '\33[1m',
'CITALIC': '\33[3m',
'CURL': '\33[4m',
'CBLINK': '\33[5m',
'CBLINK2': '\33[6m',
'CSELECTED': '\33[7m',
'CURL2': '\33[21m',
'CNORMAL': '\33[22m',
'CURLSTOP': '\33[24m',
'CBLINKSTOP': '\33[25m',
'CBLACK': '\33[30m',
'CRED': '\33[31m',
'CGREEN': '\33[32m',
'CYELLOW': '\33[33m',
'CBLUE': '\33[34m',
'CVIOLET': '\33[35m',
'CVIOLETBLINK': '\33[35;5m',
'CBEIGE': '\33[36m',
'CWHITE': '\33[37m',
'CBLACKBG': '\33[40m',
'CREDBG': '\33[41m',
'CGREENBG': '\33[42m',
'CYELLOWBG': '\33[43m',
'CBLUEBG': '\33[44m',
'CVIOLETBG': '\33[45m',
'CBEIGEBG': '\33[46m',
'CWHITEBG': '\33[47m',
'CGREY': '\33[90m',
'CRED2': '\33[91m',
'CGREEN2': '\33[92m',
'CYELLOW2': '\33[93m',
'CBLUE2': '\33[94m',
'CVIOLET2': '\33[95m',
'CBEIGE2': '\33[96m',
'CWHITE2': '\33[97m',
'CGREYBG': '\33[100m',
'CREDBG2': '\33[101m',
'CGREENBG2': '\33[102m',
'CYELLOWBG2': '\33[103m',
'CBLUEBG2': '\33[104m',
'CVIOLETBG2': '\33[105m',
'CBEIGEBG2': '\33[106m',
'CWHITEBG2': '\33[107m'
}
# Python logo ASCII art file copied from https://github.com/honno/ascii-art
ascii_art = open('images/ascii-art.txt').read()
print(
f"{colours['CBOLD']}{ascii_art}{colours['CEND']}")
print('')
print(
f"{colours['CBOLD']}{colours['CBLUE']}Welcome to "
f"{colours['CBLINK']}The Zen of Typing!"
f"{colours['CBLINKSTOP']}{colours['CEND']}"
)
print('')
print(
f"{colours['CBOLD']}{colours['CYELLOW']}The only place you can "
f"improve your typing speed and{colours['CEND']}")
print(
f"{colours['CBOLD']}{colours['CYELLOW']}brush up on some programming "
f"principles at the same time...{colours['CEND']}")
print('')
# code block adapted from
# https://github.com/CITGuru/PyInquirer/tree/master/examples
questions = [
{
'type': 'confirm',
'name': 'practice',
'message': 'Would you like some (more) practice before you begin?',
'default': False
},
{
'type': 'list',
'name': 'text',
'message': "Okay, I hope you're ready! Please choose a text:",
'choices': ['DRY', 'Jokes', 'OOP', 'Python', 'Sunscreen', 'Zen',
"Can't decide. Choose one for me!"],
'when': lambda answers: not answers['practice'],
'filter': lambda val: val.lower()
},
{
'type': 'list',
'name': 'lines',
'message': 'How many lines would you like?',
'choices': ['1', '3', '5', 'Give me the whole thing!'],
'when': lambda answers: not answers['practice'],
'filter': lambda val: val.lower()
},
{
'type': 'confirm',
'name': 'secret_password',
'message': 'Do you know the secret password?',
'when': lambda answers: not answers['practice'],
'default': False
},
{
'type': 'input',
'name': 'enter_password',
'message': 'Please enter the secret password:',
'when': lambda answers: not answers['practice'] and
answers['secret_password']
},
{
'type': 'list',
'name': 'mode',
'message': 'Please select a game mode:',
'choices': ['normal mode', 'BEAST MODE'],
'when': lambda answers: (
not answers['practice'] and answers['secret_password'] and
answers['enter_password'] == 'PEP8'
)
}
]
question_restart = [{
'type': 'confirm',
'name': 'restart_game',
'message': 'Would you like to start over and make a new selection?',
'default': False
}]
PW_COUNT = 0
def choose_text(text):
'''
Retrieve typing text content based on selection made by user from
multiple-choice menu options. Since files are being loaded from
disc/API request, which has the potential for problems that might
trigger an exception, code here should be wrapped in a try/except block
'''
try: # load content from file/API
if text == 'jokes':
jokes = pyjokes.get_jokes()
random.shuffle(jokes)
jokes = jokes[:10]
file = '\n'.join(jokes)
else:
file = open(f'texts/{text}.txt').read()
except Exception:
# use a default/fallback quote in case there's
# a problem with loading content from file/API
file = ('The first 90 percent of the code accounts for'
' the first 90 percent of the development time. '
'The remaining 10 percent of the code accounts for'
' the other 90 percent of the development time.')
return file
def get_lines_for_typing(text, lines, mode):
'''
Customise length of typing text content based on user's preference:
return either 1, 3 or 5 lines; or else the entire body of stored text.
'''
try:
lines_for_typing = text.split('\n')
reversed_lines = [line[::-1] for line in lines_for_typing]
if lines == 'all':
if mode:
return reversed_lines
else:
return lines_for_typing
if mode:
return reversed_lines[:int(lines)]
else:
return lines_for_typing[:int(lines)]
except Exception as ex:
print(ex)
class TypingText:
"""Class for enclosing all methods required to run the application"""
def __init__(self):
self.beast = self.started = self.finished = self.running = False
self.text_typed = self.text_to_be_typed = ''
self.start_time = self.total_time = self.typing_accuracy = self.wpm = 0
def calculate_results(self, text_a, text_b):
'''
Monitor user performance on each typing task and provide feedback
upon completion. Results breakdown includes total time taken,
accuracy level and words per minute (wpm). A global variable is also
used as a flag to incrementally reveal a secret password based on
steady increases in the user's typing speed. Farewell message also
communicated to the user should they opt to discontinue the game.
'''
if not self.finished:
# Work out time spent typing...
self.total_time = time.time() - self.start_time
# Work out typing accuracy...
score = 0
for i, char in enumerate(text_a):
try:
if text_b[i] == char:
score += 1
except:
pass
self.typing_accuracy = score / len(text_b) * 100
# Work out WPM...
self.wpm = len(text_b) * 60 / (5 * self.total_time)
print('')
print(
f"{colours['CBOLD']}{colours['CGREEN']}Not bad!"
f"{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CGREEN']}That took you "
f"{str(round(self.total_time, 2))} seconds to type, and you "
f"were {str(round(self.typing_accuracy))}% accurate."
f"{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CGREEN']}Your average typing"
f" speed was {str(round(self.wpm))} words per minute."
f"{colours['CEND']}"
)
global PW_COUNT
if self.wpm >= 20 and PW_COUNT == 0:
PW_COUNT = 1
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}The first character in"
f" the secret password is: P{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Be sure to make a"
f" note of it somewhere!{colours['CEND']}"
)
elif self.wpm >= 30 and PW_COUNT == 1:
PW_COUNT = 2
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}The second character"
f" in the secret password is: E{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Be sure to make a"
f" note of it somewhere!{colours['CEND']}"
)
elif self.wpm >= 40 and PW_COUNT == 2:
PW_COUNT = 3
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}The third character "
f"in the secret password is: P{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Be sure to make a"
f" note of it somewhere!{colours['CEND']}"
)
elif self.wpm >= 50 and PW_COUNT == 3:
PW_COUNT = 4
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}The fourth and {colours['CURL']}"
f"final{colours['CURLSTOP']} character in the secret "
f"password is: 8{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Be sure to make "
f"a note of it somewhere!{colours['CEND']}"
)
elif PW_COUNT == 4:
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}You should now have all four "
f"characters of the secret password...{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Don't forget to use it in "
f"order to unlock BEAST MODE!{colours['CEND']}"
)
else:
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}"
f"{colours['CYELLOW']}Keep practicing to reveal the secret"
f" password and unlock BEAST MODE!{colours['CEND']}"
)
print('')
answer = prompt(question_restart, style=custom_style_2)
if answer['restart_game']:
print('')
self.activate()
print('')
print(
f"{colours['CBOLD']}{colours['CBLUE']}Thanks for playing"
f" The Zen of Typing!{colours['CEND']}"
)
print('')
print(
f"{colours['CBOLD']}{colours['CYELLOW']}"
f"See you again soon ;){colours['CEND']}"
)
print('')
sys.exit()
def activate(self):
"""
Primary in-game method for triggering various helper functions to
configure typing activity while looping through conditional logic
(i.e. choices based on user input). User is first of all asked if
they'd like to practice their typing before starting the game proper.
Following on from this, they are asked to select both a target text
and the number of lines they wish to type. They are also asked if they
know the secret password (which can be entered to unlock 'Beast Mode').
"""
self.game_restart()
self.running = True
answers = prompt(questions, style=custom_style_2)
if answers['practice']:
answers['secret_password'] = False
chosen_text = None
num_of_lines = 'all'
print('')
print(
f"{colours['CBOLD']}{colours['CBLUE']}"
f"You can warm up by typing the following. "
f"Hit enter when you're done!{colours['CEND']}"
)
else:
chosen_text = random.choice(
['dry', 'jokes', 'oop', 'python', 'sunscreen', 'zen']
) if (
answers['text'] == "can't decide. choose one for me!"
) else answers['text']
num_of_lines = 'all' if (
answers['lines'] == 'give me the whole thing!'
) else answers['lines']
print('')
if (
answers['secret_password'] and
answers['enter_password'] != 'PEP8'
):
print(
f"{colours['CBOLD']}{colours['CRED']}Sorry, that "
f"password is incorrect!{colours['CEND']}"
)
print('')
if answers['text'] != "can't decide. choose one for me!":
print(
f"{colours['CBOLD']}{colours['CBLUE']}You have chosen "
f"to type:{colours['CEND']}"
)
print(
f"{colours['CBOLD']}{colours['CBLUE']}{colours['CURL']}"
f"{num_of_lines} line(s){colours['CURLSTOP']} from "
f"{colours['CURL']}{texts[chosen_text]}{colours['CURLSTOP']}"
f"{colours['CEND']}"
)
if answers['text'] == "can't decide. choose one for me!":
print(
f"{colours['CBOLD']}{colours['CBLUE']}have been "
f"chosen for you:{colours['CEND']}"
)
print('')
text_for_typing = choose_text(chosen_text)
self.beast = True if (
answers['secret_password'] and
answers['enter_password'] == 'PEP8' and
not answers['practice'] and answers['mode'] == 'BEAST MODE'
) else False
text_for_typing = get_lines_for_typing(
text_for_typing, num_of_lines, self.beast
)
stringified_text_for_typing = '\n'.join(text_for_typing)
for line in text_for_typing:
print(
f"{colours['CITALIC']}{textwrap.fill(line, width=76)}"
f"{colours['CEND']}"
)
if answers['practice']:
print('')
input()
print('')
self.activate()
while self.running:
print('')
print(
f"{colours['CBOLD']}{colours['CBLINK']}{colours['CYELLOW']}"
f"Off you go!!!{colours['CEND']}"
)
print('')
self.started = True
self.start_time = time.time()
if self.started and not self.finished:
if num_of_lines != 'all':
for i in range(int(num_of_lines)):
self.text_typed += (input() + '\n')
else:
for i in range(13):
self.text_typed += (input() + '\n')
self.calculate_results(
stringified_text_for_typing, self.text_typed[:-1]
)
self.finished = True
self.running = False
def game_restart(self):
"""
Method for resetting game state by returning main properties to
original values.
"""
self.finished = False
self.text_typed = self.text_to_be_typed = ''
self.start_time = self.total_time = self.wpm = 0
if __name__ == '__main__':
# game activation method is immediately called
# in order to launch the application
TypingText().activate()