diff --git a/soitool/database.py b/soitool/database.py index 06e1eb58e23dcc1c9a05fd006ecd9bb45897dff4..bd13ea7c21fc7e8c72f14204e8ba01a47cd6fb6a 100644 --- a/soitool/database.py +++ b/soitool/database.py @@ -33,6 +33,7 @@ class Database: If db is created, tables are created and filled. Runs a thread that upadtes Code in CodeBook every 24h since last update. + """ def __init__(self): @@ -51,10 +52,11 @@ class Database: self.fill_tables() print("Tables filled with data.") - # Initiates timer that triggers CodeBook update + # Initiate timer that triggers CodeBook update self.timer = QTimer() self.timer.setInterval(self.seconds_to_next_update()) - self.timer.timeout.connect(self.update_codebook_auto) + self.timer.timeout.connect(lambda: self.update_codebook_auto()) + self.timer.start() self.conn.row_factory = sqlite3.Row # Enables row['columnName'] @@ -247,6 +249,14 @@ class Database: """ Generate and insert a code for the new word in DB-table CodeBook. + This function is espescially designed for when a single word is added + to the CodeBook table. A unique code is generate and inserted in + Code which for the parameter word from before were NULL. Dependent on + the number of entries in the table various actions are performed. If + the new word makes the number of entries pass 26^x and the length of + the codes does not have he capacity, all the codes are updatet to an + appropriate length. + Parameters ---------- word : string @@ -260,10 +270,15 @@ class Database: stmt = "SELECT COUNT(*) FROM CodeBook" number_of_entries = self.conn.execute(stmt).fetchall()[0][0] stmt = "SELECT Code FROM CodeBook" - # Incase db is approximate empty, min code lenght is 2 + # In special case where table is empty or has a single word, the + # minimal length of codes will be applied if number_of_entries < 2: - actual_code_len = soitool.coder.get_code_length_needed(0) + actual_code_len = soitool.coder.get_code_length_needed( + number_of_entries + ) else: + # Since the newly added word's code is NULL and at [0][0], + # get [1][0] to get code length from an actual code actual_code_len = len(self.conn.execute(stmt).fetchall()[1][0]) needed_code_len = soitool.coder.get_code_length_needed( @@ -283,6 +298,3 @@ class Database: # Insert code to the param word in db stmt = "UPDATE CodeBook SET Code = ? WHERE Word = ?" self.conn.execute(stmt, (code, word)) - - -DB = Database() diff --git a/soitool/main_window.py b/soitool/main_window.py index db0184903e61a790d3f30b4cead03a8a92a23b11..d9c115ca7793c31ad7f6b5ca6b9e3f3e14a282dc 100644 --- a/soitool/main_window.py +++ b/soitool/main_window.py @@ -9,7 +9,7 @@ from enum import Enum from PySide2.QtWidgets import QMainWindow, QApplication, QTabWidget, QAction from PySide2.QtGui import QIcon from soitool.soi_workspace_widget import SOIWorkspaceWidget -from soitool.database import DB +from soitool.database import Database class ModuleType(Enum): @@ -28,8 +28,8 @@ class MainWindow(QMainWindow): self.setWindowTitle("SOI-tool") self.statusBar() - # Start Qtimer that triggers db update - DB.timer.start() + # Database instance + Database() # flytt ut til egen funksjon, for setup av menubar menu = self.menuBar()