6
\$\begingroup\$

(I originally asked this on Stack Overflow and was directed here)

So I have 2 classes, one is a PyQT window widget, and the other class is a stack which holds a dictionary pointing to all of the window objects.

Log Window:

from PyQt5.QtWidgets import QTreeView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from Log.Controllers.Routines.log_get_next_default_index_routine import get_next_nefault_index


class LogWindow(QTreeView):
    def __init__(self,
                 model=None,
                 index=get_next_default_index(),
                 title=None):

        super().__init__()
        self.model = model
        self.index = index
        self.title = title
        self.set_title(title)

    def set_model(self,model):
        if model is not None:
            self.model = model
        else:
            self.model = QStandardItemModel()
        self.setModel(self.model)

    def set_index(self,index):
        if index is not None:
            self.index = index
        else:
            self.index = log_coordinator.get_next_default_index()

    def set_title(self,title):
        if title is not None:
            self.setWindowTitle(title)
        else:
            self.setWindowTitle('Log Window %s' % self.index)

Log Stack:

class LogWindowStack():
    def __init__(self,index=None,log_window=None, log_window_bundle=None):
        self.stack = {}
        self.default_index_counter = 0

        self.populate_index(index,log_window,log_window_bundle)

    def populate_index(self,index,log_window,log_window_bundle):
        if index or log_window is not None:
            self.stack[index] = log_window
        elif log_window_bundle is not None:
            for index,log_window in log_window_bundle:
                self.stack[index]=log_window

    def increment_default_index_counter(self):
        self.default_index_counter = ++self.default_index_counter

There is also a controller which both creates, modifies, and passes values to the constructors of these 2 classes.

Log Controller/Coordinator:

from Log.Controllers.Routines.log_insert_log_entry_routine import insert_log_entry
from Log.Objects.log_entry_obj import LogEntry
from Log.Objects.log_window_obj import LogWindow
from Log.log_window_stack_instance import log_window_stack


def create_log_windows(log_window_indexs):
    log_window_bundle = {}
    if log_window_indexs is not None:
        for index in log_window_indexs:
            log_window_bundle[index] = LogWindow(index=index)
    return log_window_bundle


def create_log_entry(log_entry):
    return LogEntry(log_entry)


def insert_assigned_log_entries(log_window_entry_bundle=None):
    if log_window_entry_bundle is not None:
        for log_window_index,log_entry in log_window_entry_bundle.items():
            insert_log_entry(log_window_index,log_entry)


def distribute_log_entries(log_window_indexs=None,log_entries=None):
    if log_window_indexs or log_entries is not None:
        for index in log_window_indexs:
            for log_entry in log_entries:
                insert_log_entry(index,log_entry)


def push_to_log_window_stack(log_window_bundle):
    for index,log_window in log_window_bundle.items():
        log_window_stack.stack[index] = log_window


def present_log_window_stack(log_window_indexs):
    for index in log_window_indexs:
        log_window_stack.stack[index].show()


def window_index_exists(log_window_index):
    key_exists = False
    for key in log_window_stack.stack.keys():
        if log_window_index == key:
            key_exists = True
    return key_exists

Does this design look sloppy or riddled with potential errors down the line? How can I improve it?

\$\endgroup\$
0
2
\$\begingroup\$
if model is not None:

You do this all over the place. You should change it to:

if model:

@property is the Pythonic way to do getter and setters. For instance, I would do the following for the model variable:

Change:

self.model = model

To:

self._model = model

Create a property:

@property
def model(self):
    return self._model

@model.setter
def model(self, new_model):
    if new_model:
        self._model = new_model
    else:
        self._model = QStandardItemModel()
    self.setModel(self._model)

And do this for the other set functions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.