ccpq/poc_hugo.py

130 lines
2.9 KiB
Python
Raw Normal View History

2021-05-28 11:59:44 +02:00
#!/usr/bin/python3
import os
import csv
import random
from rich.console import Console
from rich.markdown import Markdown
2021-05-28 16:01:01 +02:00
# Open the CSV file and create a dictionary with all the questions
def _create_dictionary():
2021-05-28 11:59:44 +02:00
with open(path) as file:
reader = csv.DictReader(file)
data = list(reader)
2021-05-28 16:01:01 +02:00
return data
def _print_question(question_asked):
2021-06-03 14:47:26 +02:00
"""
:param question_asked:
:return:
"""
2021-05-28 16:01:01 +02:00
os.system("clear")
c = Console()
actual_question = "# {}".format(question_asked["QUESTION"])
actual_question = Markdown(actual_question)
c.print(actual_question)
answers = []
for i in question_asked.keys():
if i.isnumeric():
answers.append(question_asked[i])
answer = ""
for i in answers:
answer += "1. {} \n".format(i)
answer = Markdown(answer)
c.print(answer)
2021-06-03 14:47:26 +02:00
def _has_multiple_answer(question_asked):
"""
:param question_asked:
:return Boolean:
"""
answers = str(question_asked["ANSWER"]).split()
try:
if answers[1]:
return True
except:
return False
def _one_digit_answer(question_asked):
"""
:param question_asked:
:return Boolean:
"""
result = input("What's your answer? ")
if result == question_asked["ANSWER"]:
return True
else:
return False
def _multiple_digit_answer(question_asked):
"""
:param question_asked:
:return Boolean:
"""
answers = input("What's your answer? ").split(" ")
right_answers = str(question_asked["ANSWER"]).split(" ")
if len(answers) == 0:
return False
for answer in answers:
try:
test = right_answers.index(answer)
right_answers.pop(test)
except:
return False
if len(right_answers) == 0:
return True
else:
return False
def _test_question(question_asked):
"""
:param question_asked:
:return Boolean:
"""
if _has_multiple_answer(question_asked):
return _multiple_digit_answer(question_asked)
else:
return _one_digit_answer(question_asked)
def _print_success(result):
"""
:param result:
"""
c = Console()
if result:
msg = "## Good job!"
else:
msg = "## that's not the right answer..."
md = Markdown(msg)
c.print(md)
def print_stats(counter):
"""
:param counter:
"""
success = counter.count(True)
fail = counter.count(False)
print("{} out of {} correct!".format(success, success + fail))
input("press ENTER for a new question or CTRL-C to quit")
2021-05-28 11:59:44 +02:00
if __name__ == "__main__":
path = "./data/list_book1.csv"
2021-06-03 14:47:26 +02:00
counter = []
2021-05-28 16:01:01 +02:00
dictionary = _create_dictionary()
random.shuffle(dictionary)
for question in dictionary:
_print_question(question)
result = _test_question(question)
2021-06-03 14:47:26 +02:00
counter.append(result)
_print_success(result)
print_stats(counter)