forked from bregman-arie/devops-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_question.py
58 lines (44 loc) · 1.6 KB
/
random_question.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
import random
import optparse
import os
def main():
"""Reads through README.md for question/answer pairs and adds them to a
list to randomly select from and quiz yourself.
Supports skipping quesitons with no documented answer with the -s flag
"""
parser = optparse.OptionParser()
parser.add_option("-s", "--skip", action="store_true",
help="skips questions without an answer.",
default=False)
options, args = parser.parse_args()
with open('README.md', 'r') as f:
text = f.read()
questions = []
while True:
question_start = text.find('<summary>') + 9
question_end = text.find('</summary>')
answer_end = text.find('</b></details>')
if answer_end == -1:
break
question = text[question_start: question_end].replace('<br>', '').replace('<b>', '')
answer = text[question_end + 17: answer_end]
questions.append((question, answer))
text = text[answer_end + 1:]
num_questions = len(questions)
while True:
try:
question, answer = questions[random.randint(0, num_questions)]
if options.skip and not answer.strip():
continue
os.system("clear")
print(question)
print("...Press Enter to show answer...")
input()
print('A: ', answer)
print("... Press Enter to continue, Ctrl-C to exit")
input()
except KeyboardInterrupt:
break
print("\nGoodbye! See you next time.")
if __name__ == '__main__':
main()