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

27 lines
743 B
Python
Raw Normal View History

2019-01-31 16:51:05 +00:00
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()
2019-01-31 18:53:49 +00:00
with open(file_name, "w") as fout:
2019-01-31 16:51:05 +00:00
cursor = db.cursor()
2019-01-31 18:53:49 +00:00
cursor.execute("SELECT question, answer, correct FROM QUESTIONS")
2019-01-31 16:51:05 +00:00
data = [{"question":i[0], "answer": i[1], "correct": i[2]} for i in cursor.fetchall()]
2019-01-31 18:53:49 +00:00
json.dump(data, fout)
2019-01-31 16:51:05 +00:00
db.close()