scientific-programming-exer.../exam/ex03/settings.py

27 lines
743 B
Python

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}
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, "w") as fout:
cursor = db.cursor()
cursor.execute("SELECT question, answer, correct FROM QUESTIONS")
data = [{"question":i[0], "answer": i[1], "correct": i[2]} for i in cursor.fetchall()]
json.dump(data, fout)
db.close()