Standards and instructions for PZero developers - gecos-lab/PZero GitHub Wiki

Importing modules

Do not import modules in a generic way (e.g import numpy)!

Import classes as from <module> import <class> or from <module> import <class as ...> to avoid importing a lot of non-necessary classes.

This could also make easier the distribution with PyInstaller.

See examples in pzero.py, helper_dialogs.py, and boundary_collection.py.

For VTK we do as explained here. @gbene made a quick helper script (published in https://github.com/gecos-lab/PZero/commit/7b8074f8ba0c1b7ea2faae4d08ba7268a2bdb2a8) that, given a name, returns the correct from ... import ... VTK statement. The script can be used directly from the terminal:

../PZero/helper_scripts$ python vtk_search.py <vtk module>

So for example:

../PZero/helper_scripts$ python vtk_search.py vtkPoints
from vtkmodules.vtkCommonCore import vtkPoints

../PZero/helper_scripts$ python vtk_search.py vtkPointCloudWidget
from vtkmodules.vtkInteractionWidgets import vtkPointCloudWidget

Using super() in methods

Super() can be used in methods within classes in a way that allows defining more general code in the superclass, and then adding more code in the same method in the subclass, without substituting the whole method, thus avoiding code repetition.

For example, here I define the method initialize_menu_tools() in BaseView() (superclass) and then I add more cone VTKView() (subclass) after importing the base code with super().initialize_menu_tools():

class BaseView():
    -- more code here --
    def initialize_menu_tools(self):
        """This is the base method of the abstract BaseView() class, used to add menu tools used by all windows.
        The code appearing here is appended in subclasses using super().initialize_menu_tools() in their first line."""
        self.removeEntityButton = QAction("Remove Entity", self)  # create action
        self.removeEntityButton.triggered.connect(self.remove_entity)

        # connect action to function
        self.menuBaseView.addAction(self.removeEntityButton)  # add action to menu
        self.toolBarBase.addAction(self.removeEntityButton)  # add action to toolbar

    -- more code here --

class VTKView():
    -- more code here --
    def initialize_menu_tools(self):
        """This is the intermediate method of the VTKView() abstract class, used to add menu tools used by all VTK windows.
        The code appearing here is appended in subclasses using super().initialize_menu_tools() in their first line."""
        # append code from BaseView()
        super().initialize_menu_tools()

        # then add new code specific to VTKView()
        self.saveHomeView = QAction("Save home view", self)  # create action
        self.saveHomeView.triggered.connect(self.save_home_view)
        
        # connect action to function
        self.menuBaseView.addAction(self.saveHomeView)  # add action to menu
        self.toolBarBase.addAction(self.saveHomeView)  # add action to toolbar
        self.zoomHomeView = QAction("Zoom to home", self)
        self.zoomHomeView.triggered.connect(self.zoom_home_view)
        self.menuBaseView.addAction(self.zoomHomeView)
        self.toolBarBase.addAction(self.zoomHomeView)

        -- more code here --
⚠️ **GitHub.com Fallback** ⚠️