0

I have a program which is becoming rather sizable. I have a main app class Pierre which encases all other major functions of the program and imports functions and classes from other scripts.

My main problem right now is that I have a widget that is separated into two classes and launched from inside the main app. The button that launches the utility window works fine, however a button inside the utility works about 50% of the time.

The following is the code structure:

In pierre.py:

from src.capsuleswindow import Ui_Capsules
from src import capsulecalc

class Pierre(Ui_PierreMainWindow):
    def __init__(self):
        super(Pierre, self).__init__()
        ...
        # additional initialization
        ...
        self.capsule.clicked.connect(self.open_capsules_utility)

     ...
     # additional functions of main program
     ...
    def open_capsules_utility(self):
        self.capsules_utility = CapsulesWindow()


class CapsulesWindow(QWidget):

    def __init__(self):
        super(CapsulesWindow, self).__init__()
        self.capsuleswindow = Ui_Capsules()
        self.capsuleswindow.setupUi(self)
        self.capsuleswindow.capsuleSize.setDisabled(True)
        capsule = capsulecalc.CapsuleFormula(self.capsuleswindow)
        self.capsuleswindow.calculate.clicked.connect(capsule.
                                                     validate_fields)
        self.show()

In capsulecalc.py:

class CapsuleFormula:
     def __init__(self, capsules_window):
          self.capsules_window = capsules_window
          ...
          # additional initialization
          ...

     def validate_fields(self):
         # validation of user input text fields
         # this function is connected in pierre.py to its button
         # but only works half the time

This structure of calling another class from a script just to prevent the main script from being bulky seems clumsy in itself. From my other research it seems like this causes the garbage collector to destroy the reference to the button.

How should this be structured to prevent garbage collection but keep the source of this program tidy and readable?

smallpants
  • 460
  • 7
  • 20
  • change `capsule = capsulecalc.CapsuleFormula(self.capsuleswindow) self.capsuleswindow.calculate.clicked.connect(capsule. validate_fields)` to `self.capsule = capsulecalc.CapsuleFormula(self.capsuleswindow) self.capsuleswindow.calculate.clicked.connect(self.capsule. validate_fields)` that is, have a capsule member of the class – eyllanesc Jul 07 '18 at 02:14
  • Breaking up classes and modules (as opposed to functions and methods) based purely on size seems arbitrary and wrong. Code should be divided up into ***meaningful*** units (within the context of the application). Many programmers have strong opinions about how many lines of code is "too much" for a class or module. But there is no magic number that defines a threshold for "readability" (however you choose to define it). My advice is to let your test suite decide what the meaningful units are, and forget about counting lines of code. – ekhumoro Jul 07 '18 at 09:56

0 Answers0