initial work on exam ex 3
This commit is contained in:
parent
f7693ee0a4
commit
9941073813
27
exam/ex3/callbacks.py
Normal file
27
exam/ex3/callbacks.py
Normal file
|
@ -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)
|
||||
|
||||
|
||||
|
||||
|
4
exam/ex3/cfg.py
Normal file
4
exam/ex3/cfg.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
|
||||
config = {"APP_PATH": "."
|
||||
, "WORD_BUCKET_SIZE": 5
|
||||
}
|
67
exam/ex3/database.py
Normal file
67
exam/ex3/database.py
Normal file
|
@ -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]
|
||||
|
363
exam/ex3/main.glade
Normal file
363
exam/ex3/main.glade
Normal file
|
@ -0,0 +1,363 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Generated with glade 3.20.0 -->
|
||||
<interface>
|
||||
<requires lib="gtk+" version="3.20"/>
|
||||
<object class="GtkWindow" id="window1">
|
||||
<property name="can_focus">False</property>
|
||||
<signal name="destroy" handler="onDestroy" swapped="no"/>
|
||||
<child>
|
||||
<object class="GtkNotebook">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="question_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Question</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="answer_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="placeholder_text" translatable="yes">Answer</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="submit_answer_button">
|
||||
<property name="label" translatable="yes">Submit Answer</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="answer_was_correct_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">No question has been answered </property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="correct_answer_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">No question has been answered </property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="next_question_button">
|
||||
<property name="label" translatable="yes">Next Question</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child type="tab">
|
||||
<object class="GtkLabel" id="exercise_frame">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Exercises</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="tab_fill">False</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Load questions from file</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkFileChooserButton" id="load_from_file_chooser">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="title" translatable="yes"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="load_from_file_submit_button">
|
||||
<property name="label" translatable="yes">Submit</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<signal name="clicked" handler="on_load_from_file_submit_button_clicked" swapped="no"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSeparator">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Manually add question</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkEntry" id="add_question_question_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="placeholder_text" translatable="yes">Question</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="add_question_answer_view">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="placeholder_text" translatable="yes">Answer</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="manually_add_submit_button">
|
||||
<property name="label" translatable="yes">Submit</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<signal name="clicked" handler="on_manually_add_submit_button_clicked" swapped="no"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSeparator">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Save questions to file</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkFileChooserButton" id="save_questions_to_file_chooser">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="title" translatable="yes"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="save_question_to_file_submit_button">
|
||||
<property name="label" translatable="yes">Submit</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<signal name="clicked" handler="on_save_question_to_file_submit_button_clicked" swapped="no"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child type="tab">
|
||||
<object class="GtkLabel" id="settings_frame">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">Settings</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">1</property>
|
||||
<property name="tab_fill">False</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</interface>
|
26
exam/ex3/main.py
Normal file
26
exam/ex3/main.py
Normal file
|
@ -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()
|
27
exam/ex3/settings.py
Normal file
27
exam/ex3/settings.py
Normal file
|
@ -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()
|
Loading…
Reference in New Issue
Block a user