155 lines
4.0 KiB
Python
155 lines
4.0 KiB
Python
import pathlib
|
|
import time
|
|
import uuid
|
|
import os
|
|
import csv
|
|
import random
|
|
import json
|
|
from rich.console import Console
|
|
from rich.markdown import Markdown
|
|
|
|
LEVEL = "LEVEL"
|
|
QUESTION = "QUESTION"
|
|
ANSWER = "ANSWER"
|
|
|
|
|
|
class Question(object):
|
|
"""
|
|
class to hold the question data and methods
|
|
TODO: needs to json methods for the REST API
|
|
"""
|
|
|
|
def __init__(self, data):
|
|
self._data = data
|
|
self._uuid = uuid.uuid1()
|
|
self._clean_data()
|
|
|
|
def _clean_data(self):
|
|
"""
|
|
TODO needs quite bit of actual cleanup to make the parsing more robust
|
|
"""
|
|
self._level = self._data[LEVEL].strip()
|
|
self._question = self._data[QUESTION].strip()
|
|
self._answers = self._data[ANSWER].strip().split(" ")
|
|
self._answers = [x for x in self._answers if x]
|
|
self._create_list_of_possibilities()
|
|
|
|
def dump_json(self):
|
|
"""
|
|
dumps all data to JSON for the REST API
|
|
"""
|
|
data = {
|
|
"UUID": self.get_uuid(),
|
|
QUESTION: self.get_question(),
|
|
ANSWER: self.get_right_answers(),
|
|
"POSSIBILITIES": self.get_possibilities(),
|
|
}
|
|
return json.dumps(data)
|
|
|
|
def get_uuid(self):
|
|
return str(self._uuid)
|
|
|
|
def get_question(self):
|
|
return self._question
|
|
|
|
def _create_list_of_possibilities(self):
|
|
"""creates and cleans a list of all the possible answers"""
|
|
possibilities = []
|
|
for key in self._data.keys():
|
|
if key.isnumeric():
|
|
possibilities.append(self._data[key])
|
|
possibilities = [x for x in possibilities if x] # hack to remove empty objects
|
|
self._possibilities = possibilities
|
|
|
|
def get_possibilities(self):
|
|
return self._possibilities
|
|
|
|
def verify(self, answers):
|
|
"""needs quite some work"""
|
|
if not isinstance(answers, list):
|
|
raise TypeError
|
|
if len(answers) == 0:
|
|
return False
|
|
right_answers = list(self._answers) # need a copy so we don't change for future questions
|
|
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 get_right_answers(self):
|
|
answers = []
|
|
for answer in self._answers:
|
|
if len(answer) == 0:
|
|
print("question not complete...")
|
|
else:
|
|
answers.append(self._data[answer])
|
|
return answers
|
|
|
|
|
|
class Database(object):
|
|
"""holds all the Question objects and methods to get new questions"""
|
|
|
|
def __init__(self, filepath):
|
|
self.filepath = filepath
|
|
self._db = []
|
|
self.setup()
|
|
|
|
def setup(self):
|
|
data = csv.DictReader(open(self.filepath, "r"))
|
|
for row in data:
|
|
self._db.append(Question(row))
|
|
|
|
def get_question(self):
|
|
return random.choice(self._db)
|
|
|
|
def get_question_by_uuid(self, uuid):
|
|
question = [question for question in self._db if question.get_uuid() == uuid]
|
|
if len(question) == 1:
|
|
return question[0]
|
|
elif len(question) > 1:
|
|
raise Exception
|
|
else:
|
|
return None
|
|
|
|
|
|
class Game(object):
|
|
def __init__(self):
|
|
self._stats = []
|
|
|
|
def update_stats(self, stat):
|
|
self._stats.append(stat)
|
|
|
|
def get_stats(self):
|
|
right = self._stats.count(True)
|
|
wrong = self._stats.count(False)
|
|
total = len(self._stats)
|
|
return (right, wrong, total)
|
|
|
|
|
|
class Stats(object):
|
|
"""TODO placeholder for the stats class"""
|
|
pass
|
|
|
|
|
|
class Player(object):
|
|
"""TODO placeholder for the player class"""
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
filepath = pathlib.Path("../data/list_book1.csv")
|
|
db = Database(filepath)
|
|
for i in range(0, 10):
|
|
q = db.get_question()
|
|
uid = q.get_uuid()
|
|
print(uid)
|
|
t = db.get_question_by_uuid(uid)
|
|
print(t.get_uuid())
|
|
|