2021-10-20 09:10:44 +02:00
|
|
|
import random
|
|
|
|
import optparse
|
2022-11-06 08:58:03 +01:00
|
|
|
import os
|
2021-10-20 09:10:44 +02:00
|
|
|
|
2022-11-07 07:58:06 +01:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
def main():
|
2021-10-24 21:00:44 +02:00
|
|
|
"""Reads through README.md for question/answer pairs and adds them to a
|
|
|
|
list to randomly select from and quiz yourself.
|
2023-08-24 22:02:53 +02:00
|
|
|
Supports skipping questions with no documented answer with the -s flag
|
2021-10-20 09:10:44 +02:00
|
|
|
"""
|
|
|
|
parser = optparse.OptionParser()
|
2021-10-24 21:00:44 +02:00
|
|
|
parser.add_option("-s", "--skip", action="store_true",
|
|
|
|
help="skips questions without an answer.",
|
|
|
|
default=False)
|
2021-10-20 09:10:44 +02:00
|
|
|
options, args = parser.parse_args()
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
with open('README.md', 'r') as f:
|
|
|
|
text = f.read()
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
questions = []
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
while True:
|
|
|
|
question_start = text.find('<summary>') + 9
|
|
|
|
question_end = text.find('</summary>')
|
|
|
|
answer_end = text.find('</b></details>')
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
if answer_end == -1:
|
|
|
|
break
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
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:]
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
num_questions = len(questions)
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
question, answer = questions[random.randint(0, num_questions)]
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
if options.skip and not answer.strip():
|
|
|
|
continue
|
2022-11-06 08:58:03 +01:00
|
|
|
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()
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
print("\nGoodbye! See you next time.")
|
2021-10-24 21:00:44 +02:00
|
|
|
|
2021-10-20 09:10:44 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|