def get_snapshot(self, name): Log.log('d', f'name: {name}') snapshot = self.cursor.execute(f"SELECT \ operations, expressions_count, columns_num, min_num, max_num \ from history \ where snapshot_name = '{name}'") return snapshot.fetchone()
def add_snaptshot(self, params): Log.log('d', f'add_snapshot params: {params}') self.cursor.execute( f"INSERT into history \ (snapshot_name, operations, expressions_count, columns_num, min_num, max_num)\ values (?,?,?,?,?,?)", params) self.conn.commit()
def set_checked(self, values): from src.logger.logger import Log for i in range(self.model().rowCount()): item = self.model().item(i) Log.log('i', 'Operations item data: ', item.data()) item.setCheckState(Qt.Checked if item.data() in values else Qt.Unchecked)
def load_snapshot(self, config: Config): Log.log('i', 'Config: ', config.__dict__) self.operation_checkbox.set_checked(config.operations) self.expressions_count_input.setText(str(config.expressions_count)) self.columns_input.setText(str(config.columns)) self.min_number_input.setText(str(config.min_number)) self.max_number_input.setText(str(config.max_number))
def test_add_snapshot(self): task = TaskModel(17, 3, ['+', '-'], (10, 49)) Log.log('d', f'Test format: {task.to_sqlite_format()}') name = ''.join([choice(ascii_letters) for _ in range(randint(5, 15))]) self.hm.add_snaptshot([name, *task.to_sqlite_format()]) # self.dbm.save() Log.log('d', self.hm.get_snapshot_names())
def save(self, file_name, with_answers=False): self._fill_table(with_answers) self.doc.add_page_break() name = open_file(documents_path() + '/' + file_name) Log.log('d', 'name: ', name) if name[0] == '': return self.doc.save(name[0])
def generate_task_action(self, with_answers=False): config = self.validate_task_config() Log.log('i', 'with answers: ', with_answers) if config: task = TaskModel(int(config.expressions_count), int(config.columns), config.operations, (int(config.min_number), int(config.max_number))) em = ExportManager(task) em.save('Math 1.docx', with_answers)
def __init__(self): # db_manager = DBManager() # self.dbm = db_manager # db_manager.init_tables() hm = HistoryProvider() self.hm = hm Log.log('d', hm.get_snapshot_names()) name = ''.join([choice(ascii_letters) for _ in range(randint(5, 15))]) hm.add_snaptshot((name, '+, -', 3, 3, 4, 20)) Log.log('d', hm.get_snapshot_names())
def _fill_table(self, with_answers=False): expression_index = 0 for col_index in range(self.cols): for row_index in range(self.rows): if expression_index >= self.task.expressions_count: return cell = self.table.rows[row_index].cells[col_index] expression = self.task.get_field()[expression_index] Log.log('d', 'Expression: ', expression.docx_format()) cell.text = expression.docx_format(with_answers) expression_index += 1
def generate_example(self): op = choice(self._operations) a = randint(self._from_num, self._to_num) b = randint(self._from_num, self._to_num) try: Log.log('d', "data: ", a, op, b) ret = eval(str(a) + op + str(b)) Log.log('d', ret) ex = ExpressionModel(a, op, b, result=ret) return ex except Exception: return Exception
def snapshot_entry_clicked(self, index: QModelIndex): config = Config() Log.log('i', 'Clicked snaphost entry') snapshot = self.history_provider.get_snapshot(index.data()) Log.log('i', 'Snapshot name: ', index.data()) if not snapshot: return config.operations = snapshot[0] config.expressions_count = snapshot[1] config.columns = snapshot[2] config.min_number = snapshot[3] config.max_number = snapshot[4] self.view.load_snapshot(config)
def show_view(self): Log.log('i', 'show_view') self.view.show() self.view.generate_task_button.clicked.connect( self.generate_task_action) self.view.generate_task_with_answers_button.clicked.connect( lambda: self.generate_task_action(True)) self.view.table.clicked.connect(self.snapshot_entry_clicked) self.view.save_snapshot_button.clicked.connect(self.save_snapshot) self.view.delete_snapshot_button.clicked.connect(self.delete_snapshot) self.update_snapshot_table()
def delete_snapshot(self): name, is_pressed = QInputDialog.getText(self.view, "Delete Snapshot", "Enter name") if is_pressed: Log.log('d', self.history_provider.get_snapshot(name)) if self.history_provider.get_snapshot(name): self.history_provider.delete_snapshot(name) self.update_snapshot_table() self.dbm.save() else: dialog = ErrorDialog(self.view) dialog.exec_() else: return
def generate_final(self): errors = False for _ in range(self._task.expressions_count): ex = self.generate_example() if not ex: errors = True Log.log('d', 'Cannot generate Example') Log.log('d', ex) continue self._task.add_item(ex) if errors: Log.log('er', "Table is incomplete")
def close(self): hp = HistoryProvider() Log.log('d', hp.get_snapshot_names()) self.conn.close()
def update_snapshot_table(self): Log.log('i', 'Snapshots: ', self.history_provider.get_snapshot_names()) self.view.clear_snapshot_table() for name in self.history_provider.get_snapshot_names(): self.view.add_snapshot_entry(name)
def add_snapshot_entry(self, name): Log.log('i', 'Param name: ', name) snapshot_item = QTableWidgetItem(name) self.table.setRowCount(self.table.rowCount() + 1) row = self.table.rowCount() - 1 self.table.setItem(row, 0, snapshot_item)
def delete_snapshot(self, name): Log.log('i', f'delete_snapshot(name): {name}') self.cursor.execute( f"DELETE from history where snapshot_name = '{name}'") self.conn.commit()
def test_path(self): Log.log('d', "Documents path: ", documents_path()) self.dbm.save()
def test_TaskModel(self): Log.log('d', self.task._field)
def quit(self): Log.log('d', 'quit') self.controller.dbm.save() self.controller.dbm.close()
from .tests import * from src.logger.logger import Log if __name__ == '__main__': log = Log() tt = TestTaskModel() tt.test_TaskModel() tfm = TestFileManager() tfm.test_path() # tfm.test_open_file() tsql = TestSQLite() tsql.test_add_snapshot() tsql.test_get_snapshot() # tsql # test_model = TestModel() # test_model.test_is_valid_min_max() # Docx Generation eo = TestExportManager() # for _ in range(10): task = tsql.sample_TaskModel(randint(30, 40), randint(10, 30)) # task = TaskModel(14, 4, ['+', '-'], (1, 100)) task.ex_generator.generate_final() eo.test_output(task)
def test_get_snapshot(self): ret = self.hm.get_snapshot('test1') if not(ret == ('+,-', 17, 3, 10, 49)): Log.log('er', f'Not equal {ret}') task = TaskModel.from_sqlite_format(ret) Log.log('d', task)
def test_output(self, task): eo = ExportManager(task) eo.save('test1.beta', True) eo.save('test1_with_answers.beta', False) Log.log('d', 'saved')
def documents_path(): home = os.path.expanduser("~") documents_path = os.path.join(home, 'Documents') Log.log('d', 'doc path: ', documents_path) return documents_path