database and Borg

Hi list,

I have a table that I load from a database that should be shared
between three widgets of my application (let say, three different
pages of a notebook).
Is it a good idea to use Borg (monostate), to avoid multiple
instantiation?

You’ll likely get a debate on whether Borg is a best use case. Irregardless of using Bord, there’s LOTS of discussions on your very question about seperating data from GUI for the very reason of your concern, data integrety. And there are lots of ways to handle it. It’s what abstraction helps to do (of seperating data from GUI) and why so many different OOP patterns where invented and evolved.

I’ll give you my novice programmer approach. I’ll leave the fancy or Pythonic ways for my betters to share with you.

You can use PubSub or wx events being thrown around between the different panels and some a data object. Or the simpler approach but not very database “correct”.

Make a MyCustomMadeTableData object (class) in the widget (ie the notebook constructor init() or whereever.) that holds the panel. and let the controls on the panels get the values from the data object in the parent widget

Here is my contrived example: (contrived = not real code)

class MyDataObject(object):

def init(self, first, last):

self.first = first

self.last = last

class MyNBPanel1(wx.Panel):

def init(self, parent, bla_bla, display_data):

ctrl1 = wx.TextCtrl1(self, bla)

ctrl1.Value( parent.data_obj.first) # HERES this important part

ctrl2 = wx.TextCtrl1(self, bla)

ctrl2.Value( parent.data_obj.last) # HERES this important part

class MyNotebook(wx.subclass_ofNotebook):

def init(self, *args, **kwargs):

wx.Notebook.init(self, bla, bla, dbtable_data=None)

self.GetFreshData()

panel1 = MyNBPanel1(self, bla_bla, self.data_obj ) # or whatever code it takes to add a panel to a notebook

panel2 = MyNBPanel2(self, bla_bla, self.data_obj ) # or whatever code it takes to add a panel to a notebook

def GetFreshData(self):

self.data_obj = MyDataObject(“Dev”, “Player”)

Or you can do this:

Look at the wxPython Demo. Look at how Robin uses the log. He passes that thing down from the top level object. You can make a Data Namespace and pass that down as a parameter to each “containing” widget or control that uses data. But most don’t do this for various (good reasons).

But if your database is complex. Switch to a “framework”. Although I’ve never used it, Dabo is well supported and highly active. And I here it’s very easy to use. It uses wxPython.