fixes some bugs in the lib and standalone

This commit is contained in:
waldek 2021-05-24 15:38:11 +02:00
parent 01c9cd169d
commit 747f573201
3 changed files with 97 additions and 16 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ bin/
include/
lib/
__pycache__
*.swp

View File

@ -1,8 +1,10 @@
import pathlib
import time
import uuid
import os
import csv
import random
import json
from rich.console import Console
from rich.markdown import Markdown
@ -16,8 +18,10 @@ 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):
@ -27,21 +31,51 @@ class Question(object):
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 get_possibilities(self):
"""returns a list of all the possible answers"""
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])
return possibilities
possibilities = [x for x in possibilities if x] # hack to remove empty objects
self._possibilities = possibilities
def verify(self, answer):
def get_possibilities(self):
return self._possibilities
def verify(self, answers):
"""needs quite some work"""
if answer in self._data[ANSWER]:
if not isinstance(answers, list):
raise TypeError
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
@ -58,6 +92,7 @@ class Question(object):
class Database(object):
"""holds all the Question objects and methods to get new questions"""
def __init__(self, filepath):
self.filepath = filepath
self._db = []
@ -71,6 +106,15 @@ class Database(object):
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):
@ -86,6 +130,18 @@ class Game(object):
return (right, wrong, total)
if __name__ == "__main__":
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())

View File

@ -10,12 +10,28 @@ from rich.markdown import Markdown
from ccpq.lib_ccpq import Question, Database, Game
MSG = {
True: [
"Yes!",
"Good one!",
"Super!",
"Excellent job!"
],
False: [
"Sorry, that's wrong",
"No, that's not right",
"Damn it! That's not the right answer",
]
}
class Tui(object):
def __init__(self):
self._console = Console()
self._stats = []
def ask_question(self, question):
os.system("clear")
md = Markdown("# {}".format(question.get_question()))
self._console.print(md)
md = ""
@ -25,8 +41,8 @@ class Tui(object):
self._console.print(md)
def prompt_for_answer(self):
md = Markdown("What's your answer?")
self._console.print(md)
#md = Markdown("What's your answer?")
#self._console.print(md)
answer = self._parse_input()
return answer
@ -35,12 +51,16 @@ class Tui(object):
TODO make it adapt to questions with multiple choices and fill the
blank
"""
while True:
result = input()
answers = []
results = input("What's your answer? (only numbers, separated by a SPACE) ")
results = results.split()
for result in results:
if result.isdigit():
return result
md = Markdown("**only digits please**")
self._console.print(md)
answers.append(result)
else:
md = Markdown("**only digits please**")
self._console.print(md)
return answers
def show_response(self, question):
answers = question.get_right_answers()
@ -54,6 +74,11 @@ class Tui(object):
md = Markdown(md)
self._console.print(md)
def show_success(self, success):
md = "# {}".format(random.choice(MSG[success]))
md = Markdown(md)
self._console.print(md)
def show_stats(self, stats):
md = "### you have {} out of {} right!".format(stats[0], stats[2])
md = Markdown(md)
@ -62,8 +87,6 @@ class Tui(object):
md = Markdown(md)
self._console.print(md)
input()
os.system("clear")
def goodbye(self):
md = Markdown("# Goodbye!")
@ -83,6 +106,7 @@ class Application(object):
answer = self._interface.prompt_for_answer()
stat = question.verify(answer)
self._session.update_stats(stat)
self._interface.show_success(stat)
self._interface.show_response(question)
self._interface.show_stats(self._session.get_stats())
@ -91,7 +115,7 @@ class Application(object):
if __name__ == "__main__":
filepath = pathlib.Path("./data/list_book1.csv")
filepath = pathlib.Path("./data/multiple.csv")
interface = Tui()
app = Application(filepath, interface)
try: