Pattern question

cantabile cantabile.03 at wanadoo.fr
Thu Jul 7 12:13:08 EDT 2005


Hi,

I'm trying to write a small installer for a server. But this program
should be able to run in the future under heterogenous environments and
os (at least linux/windows). I mean, the install will be done either in
text mode or curses or gtk or tk, either in debian or windows 2000 and
so on...

The idea, at the moment, is as follows :

class Server:
    def __init__(self, params = None):
        self.params = params
    def __getattr__(self, attr):
        return self.params.get(attr, None)

class Installer:
     def __init__(self, gui = None):
        self.gui =  gui
        self.srv = None

    def update(self, dict):
        self.srv = Server(dict)

class Gui:
    def __init__(self, installer = None):
        self.installer = installer
    def main():
    ## Some stuff here calling doIt() when the
    ## user is done with filling various fields

    def doIt(self):
        dict = {"param1":"qwerty", "param2":"uiop"}
        self.installer.update(dict)

def main():
    inst = Installer()
    gui =  Gui(inst)
    inst.gui = gui
    inst.gui.main()

    ## Here will be the actual install method
    ## ...

    ## An example of accessing srv values:
    print inst.srv.param1, inst.srv.param2

But, considering this code, I find the 3 first lines of my main() a bit
heavy. I have to inform inst that it has a 'gui', and Gui that it has an
'installer'. I was trying to implement something looking like (very
roughly) to the Observer pattern (so that the Gui would be totally
independant from the actual install process).
 I guess there is something wrong in my approach. Is there a better
pattern than this one for that kind of stuff ?

Tanks for your help.



More information about the Python-list mailing list