From 9941073813190372a2009255946fd86cd7e8a6cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Thu, 31 Jan 2019 17:51:05 +0100 Subject: [PATCH] initial work on exam ex 3 --- exam/ex3/callbacks.py | 27 ++++ exam/ex3/cfg.py | 4 + exam/ex3/database.py | 67 ++++++++ exam/ex3/main.glade | 363 ++++++++++++++++++++++++++++++++++++++++++ exam/ex3/main.py | 26 +++ exam/ex3/settings.py | 27 ++++ 6 files changed, 514 insertions(+) create mode 100644 exam/ex3/callbacks.py create mode 100644 exam/ex3/cfg.py create mode 100644 exam/ex3/database.py create mode 100644 exam/ex3/main.glade create mode 100644 exam/ex3/main.py create mode 100644 exam/ex3/settings.py diff --git a/exam/ex3/callbacks.py b/exam/ex3/callbacks.py new file mode 100644 index 0000000..244f614 --- /dev/null +++ b/exam/ex3/callbacks.py @@ -0,0 +1,27 @@ +from settings import load_words_from_file, manually_add_question, save_questions_to_file + +class CallbackHandler(object): + def __init__(self, gtk_builder): + self.builder = gtk_builder + def on_manually_add_submit_button_clicked(self, widget): + question_view = self.builder.get_object("add_question_question_view") + answer_view = self.builder.get_object("add_question_answer_view") + + question = question_view.get_text() + answer = answer_view.get_text() + + manually_add_question(question, answer) + + def on_load_from_file_submit_button_clicked(self, widget): + file_chooser = self.builder.get_object("load_from_file_chooser") + file_name = file_chooser.get_filename() + load_words_from_file(file_name) + + def on_save_question_to_file_submit_button_clicked(self, widget): + file_chooser = self.builder.get_object("save_questions_to_file_chooser") + file_name = file_chooser.get_filename() + save_questions_to_file(file_name) + + + + diff --git a/exam/ex3/cfg.py b/exam/ex3/cfg.py new file mode 100644 index 0000000..cc4959c --- /dev/null +++ b/exam/ex3/cfg.py @@ -0,0 +1,4 @@ + +config = {"APP_PATH": "." + , "WORD_BUCKET_SIZE": 5 +} diff --git a/exam/ex3/database.py b/exam/ex3/database.py new file mode 100644 index 0000000..aae1541 --- /dev/null +++ b/exam/ex3/database.py @@ -0,0 +1,67 @@ +import os +import sqlite3 +import random + +from cfg import config + +database_path = os.path.join(config["APP_PATH"], "app.sqlite") + +def create_database(): + # truncate the file if it exists + with open(database_path, "w") as database_file: + pass + + db = sqlite3.connect(database_path) + + db.execute('''CREATE TABLE QUESTIONS(question TEXT, answer TEXT, correct INT)''') + db.close() + + +def open_database(): + if(not os.path.exists(database_path)): + create_database() + + return sqlite3.connect(database_path) + +def update_correct(question, correct): + """ + Correct has to be +1 (for a correct answer) or -1 (for a wrong answer). + """ + db = open_database() + cursor = db.cursor() + cursor.execute("UPDATE QUESTIONS SET correct=correct + ? WHERE question = ?", (correct, question)) + db.close() + +def add_word(question, answer): + db = open_database() + cursor = db.cursor() + cursor.execute("INSERT INTO QUESTIONS(question, answer, correct) VALUES(?, ?, 0)", (question, answer)) + db.close() + +def add_words(input_data): + db = open_database() + cursor = db.cursor() + + cursor.executemany("INSERT INTO QUESTIONS(question, answer, correct) VALUES(?, ?, 0)", input_data.items()) + db.close() + print("added words") + +def fetch_question(): + db = open_database() + cursor = db.cursor() + + cursor.execute("SELECT COUNT(question) FROM questions") + number_of_questions = cursor.fetchone()[0] + if(number_of_questions == 0): + raise Exception("No questions in the database") + fetch_questions = number_of_questions // config["WORD_BUCKET_SIZE"] + if(fetch_questions == 0): + fetch_questions = 1 + + cursor.execute("SELECT question, answer, correct FROM QUESTIONS SORT BY correct") + + result = cursor.fetch(fetch_questions) + + my_result = random.choice(result) + return result[:2] + diff --git a/exam/ex3/main.glade b/exam/ex3/main.glade new file mode 100644 index 0000000..9dc2289 --- /dev/null +++ b/exam/ex3/main.glade @@ -0,0 +1,363 @@ + + + + + + False + + + + True + True + + + True + False + vertical + + + True + False + Question + + + False + True + 0 + + + + + True + True + Answer + + + False + True + 1 + + + + + True + False + + + + + + Submit Answer + True + True + True + + + False + True + 1 + + + + + + + + False + True + 2 + + + + + True + False + + + True + False + vertical + + + True + False + No question has been answered + + + False + True + 0 + + + + + True + False + No question has been answered + + + False + True + 1 + + + + + False + True + 0 + + + + + Next Question + True + True + True + + + False + True + 1 + + + + + False + True + 3 + + + + + + + + + + True + False + Exercises + + + False + + + + + True + False + vertical + + + True + False + + + True + False + Load questions from file + + + False + True + 0 + + + + + True + False + + + + False + True + 1 + + + + + Submit + True + True + True + + + + False + True + 2 + + + + + False + True + 0 + + + + + True + False + + + False + True + 1 + + + + + True + False + + + True + False + Manually add question + + + False + True + 0 + + + + + True + False + vertical + + + True + True + Question + + + False + True + 0 + + + + + True + True + Answer + + + False + True + 1 + + + + + False + True + 1 + + + + + Submit + True + True + True + + + + False + True + 2 + + + + + False + True + 2 + + + + + True + False + + + False + True + 3 + + + + + True + False + + + True + False + Save questions to file + + + False + True + 0 + + + + + True + False + + + + False + True + 1 + + + + + Submit + True + True + True + + + + False + True + 2 + + + + + False + True + 4 + + + + + 1 + + + + + True + False + Settings + + + 1 + False + + + + + + diff --git a/exam/ex3/main.py b/exam/ex3/main.py new file mode 100644 index 0000000..9d5c4f1 --- /dev/null +++ b/exam/ex3/main.py @@ -0,0 +1,26 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + + +from callbacks import CallbackHandler + +builder = Gtk.Builder() +builder.add_from_file("main.glade") + +callback_handler = CallbackHandler(builder) + +handlers = { + "onDestroy": Gtk.main_quit + , "on_manually_add_submit_button_clicked": callback_handler.on_manually_add_submit_button_clicked + , "on_load_from_file_submit_button_clicked": callback_handler.on_load_from_file_submit_button_clicked + , "on_save_question_to_file_submit_button_clicked": callback_handler.on_save_question_to_file_submit_button_clicked +} + +builder.connect_signals(handlers) + + +window = builder.get_object("window1") +window.show_all() + +Gtk.main() diff --git a/exam/ex3/settings.py b/exam/ex3/settings.py new file mode 100644 index 0000000..a2cca38 --- /dev/null +++ b/exam/ex3/settings.py @@ -0,0 +1,27 @@ +import os +import json +from database import add_word, add_words, open_database + +def load_words_from_file(file_name): + if(not os.path.exists(file_name)): + raise Exception("File does not exist") + + with open(file_name) as fin: + data = {line[0]: line[1] for line in + [ line.split() for line in fin if line] + if len(line) == 2} + print("adding", data) + add_words(data) + +def manually_add_question(question, answer): + add_word(question, answer) + +def save_questions_to_file(file_name): + db = open_database() + with open(file_name) as fout: + cursor = db.cursor() + cursor.execute("SELCT question, answer, correct FROM QUESTIONS") + data = [{"question":i[0], "answer": i[1], "correct": i[2]} for i in cursor.fetchall()] + + json.dump(fout) + db.close()