fixes some bugs in the lib and standalone
This commit is contained in:
parent
01c9cd169d
commit
747f573201
|
@ -2,3 +2,4 @@ bin/
|
||||||
include/
|
include/
|
||||||
lib/
|
lib/
|
||||||
__pycache__
|
__pycache__
|
||||||
|
*.swp
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import pathlib
|
import pathlib
|
||||||
import time
|
import time
|
||||||
|
import uuid
|
||||||
import os
|
import os
|
||||||
import csv
|
import csv
|
||||||
import random
|
import random
|
||||||
|
import json
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.markdown import Markdown
|
from rich.markdown import Markdown
|
||||||
|
|
||||||
|
@ -16,8 +18,10 @@ class Question(object):
|
||||||
class to hold the question data and methods
|
class to hold the question data and methods
|
||||||
TODO: needs to json methods for the REST API
|
TODO: needs to json methods for the REST API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self._data = data
|
self._data = data
|
||||||
|
self._uuid = uuid.uuid1()
|
||||||
self._clean_data()
|
self._clean_data()
|
||||||
|
|
||||||
def _clean_data(self):
|
def _clean_data(self):
|
||||||
|
@ -27,21 +31,51 @@ class Question(object):
|
||||||
self._level = self._data[LEVEL].strip()
|
self._level = self._data[LEVEL].strip()
|
||||||
self._question = self._data[QUESTION].strip()
|
self._question = self._data[QUESTION].strip()
|
||||||
self._answers = self._data[ANSWER].strip().split(" ")
|
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):
|
def get_question(self):
|
||||||
return self._question
|
return self._question
|
||||||
|
|
||||||
def get_possibilities(self):
|
def _create_list_of_possibilities(self):
|
||||||
"""returns a list of all the possible answers"""
|
"""creates and cleans a list of all the possible answers"""
|
||||||
possibilities = []
|
possibilities = []
|
||||||
for key in self._data.keys():
|
for key in self._data.keys():
|
||||||
if key.isnumeric():
|
if key.isnumeric():
|
||||||
possibilities.append(self._data[key])
|
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"""
|
"""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
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -58,6 +92,7 @@ class Question(object):
|
||||||
|
|
||||||
class Database(object):
|
class Database(object):
|
||||||
"""holds all the Question objects and methods to get new questions"""
|
"""holds all the Question objects and methods to get new questions"""
|
||||||
|
|
||||||
def __init__(self, filepath):
|
def __init__(self, filepath):
|
||||||
self.filepath = filepath
|
self.filepath = filepath
|
||||||
self._db = []
|
self._db = []
|
||||||
|
@ -71,6 +106,15 @@ class Database(object):
|
||||||
def get_question(self):
|
def get_question(self):
|
||||||
return random.choice(self._db)
|
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):
|
class Game(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -86,6 +130,18 @@ class Game(object):
|
||||||
return (right, wrong, total)
|
return (right, wrong, total)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
class Player(object):
|
||||||
|
"""TODO placeholder for the player class"""
|
||||||
pass
|
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())
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,28 @@ from rich.markdown import Markdown
|
||||||
|
|
||||||
from ccpq.lib_ccpq import Question, Database, Game
|
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):
|
class Tui(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._console = Console()
|
self._console = Console()
|
||||||
self._stats = []
|
self._stats = []
|
||||||
|
|
||||||
def ask_question(self, question):
|
def ask_question(self, question):
|
||||||
|
os.system("clear")
|
||||||
md = Markdown("# {}".format(question.get_question()))
|
md = Markdown("# {}".format(question.get_question()))
|
||||||
self._console.print(md)
|
self._console.print(md)
|
||||||
md = ""
|
md = ""
|
||||||
|
@ -25,8 +41,8 @@ class Tui(object):
|
||||||
self._console.print(md)
|
self._console.print(md)
|
||||||
|
|
||||||
def prompt_for_answer(self):
|
def prompt_for_answer(self):
|
||||||
md = Markdown("What's your answer?")
|
#md = Markdown("What's your answer?")
|
||||||
self._console.print(md)
|
#self._console.print(md)
|
||||||
answer = self._parse_input()
|
answer = self._parse_input()
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
@ -35,12 +51,16 @@ class Tui(object):
|
||||||
TODO make it adapt to questions with multiple choices and fill the
|
TODO make it adapt to questions with multiple choices and fill the
|
||||||
blank
|
blank
|
||||||
"""
|
"""
|
||||||
while True:
|
answers = []
|
||||||
result = input()
|
results = input("What's your answer? (only numbers, separated by a SPACE) ")
|
||||||
|
results = results.split()
|
||||||
|
for result in results:
|
||||||
if result.isdigit():
|
if result.isdigit():
|
||||||
return result
|
answers.append(result)
|
||||||
md = Markdown("**only digits please**")
|
else:
|
||||||
self._console.print(md)
|
md = Markdown("**only digits please**")
|
||||||
|
self._console.print(md)
|
||||||
|
return answers
|
||||||
|
|
||||||
def show_response(self, question):
|
def show_response(self, question):
|
||||||
answers = question.get_right_answers()
|
answers = question.get_right_answers()
|
||||||
|
@ -54,6 +74,11 @@ class Tui(object):
|
||||||
md = Markdown(md)
|
md = Markdown(md)
|
||||||
self._console.print(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):
|
def show_stats(self, stats):
|
||||||
md = "### you have {} out of {} right!".format(stats[0], stats[2])
|
md = "### you have {} out of {} right!".format(stats[0], stats[2])
|
||||||
md = Markdown(md)
|
md = Markdown(md)
|
||||||
|
@ -62,8 +87,6 @@ class Tui(object):
|
||||||
md = Markdown(md)
|
md = Markdown(md)
|
||||||
self._console.print(md)
|
self._console.print(md)
|
||||||
input()
|
input()
|
||||||
os.system("clear")
|
|
||||||
|
|
||||||
|
|
||||||
def goodbye(self):
|
def goodbye(self):
|
||||||
md = Markdown("# Goodbye!")
|
md = Markdown("# Goodbye!")
|
||||||
|
@ -83,6 +106,7 @@ class Application(object):
|
||||||
answer = self._interface.prompt_for_answer()
|
answer = self._interface.prompt_for_answer()
|
||||||
stat = question.verify(answer)
|
stat = question.verify(answer)
|
||||||
self._session.update_stats(stat)
|
self._session.update_stats(stat)
|
||||||
|
self._interface.show_success(stat)
|
||||||
self._interface.show_response(question)
|
self._interface.show_response(question)
|
||||||
self._interface.show_stats(self._session.get_stats())
|
self._interface.show_stats(self._session.get_stats())
|
||||||
|
|
||||||
|
@ -91,7 +115,7 @@ class Application(object):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
filepath = pathlib.Path("./data/list_book1.csv")
|
filepath = pathlib.Path("./data/multiple.csv")
|
||||||
interface = Tui()
|
interface = Tui()
|
||||||
app = Application(filepath, interface)
|
app = Application(filepath, interface)
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue