after 40 years of developing in Cobol and other procedural language, I feel a bit confused starting OOP. My main app already contains a lot of code so that I want to progressively move some utility methods to another module. This is a small example of the operation. Until now the only way to make it work with expected results consist in using an object "objmod". This does not look very orthodox. Is there a more convenient way ?
MAIN APP *****************************************************
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
import sys
import module1
import sqlite3
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
self.button = QPushButton("Insert data from work01 into work02")
self.button.clicked.connect(self.envoi)
self.setCentralWidget(self.button)
def envoi(self):
mod1 = module1.clmod1 #<<<< USING THE MODULE 'module1'
mod1.insert(self)
############## WE HAVE MOVED ##################
"""def cleanWork02(self):
self.connecter()
self.cur.execute('''DELETE FROM Work02''')"""
###############################################
def execute(self, sql):
self.connecter()
self.cur.execute(sql)
self.conexion.commit()
def connecter(self):
self.db = 'C:\TestPyth\Base.db'
self.conexion = sqlite3.connect(self.db)
self.cur = self.conexion.cursor()
self.conexion.commit()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
MODULE ****************************************************
import sqlite3
class clmod1:
def insert(self):
objmod = clmod1
objmod.cleanWork02(self)
sql = '''INSERT INTO Work02 (field1,field2,field3)
SELECT Work01.field1,Work01.field2,Work01.field3
FROM Work01'''
self.execute(sql)
def cleanWork02(self):
sql = '''DELETE FROM Work02'''
self.execute(sql)```
***********************************************************************************************
inside the 'insert method' if I use 'self.cleanWork02()' instead of 'objmod.cleanWork02(self)',
it says "AttributeError: 'MainWindow' object has no attribute 'cleanWork02'