473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Pattern question

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.
Jul 21 '05 #1
4 1239
cantabile wrote:
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.


You may want to have a look at the Factory pattern...

# outrageously oversimplified dummy exemple
class Gui(object):
def __init__(self, installer):
self.installer = installer

class PosixGui(Gui):
pass

class Win32Gui(Gui):
pass

class GuiFactory(object):
def getGui(self, installer):
if os.name == 'posix':
return PosixGui(installer)
elif os.name == 'win32':
return Win32Gui(installer)
else:
raise "os %s not supported" % os.name

class Installer(object):
def __init__(self, guiFactory):
self.gui = guiFactory.getGui(self)

def main():
inst = Installer(GuiFactory())
return inst.gui.main()

NB 1:
You may want to hide away the gui stuff:

class Installer(object):
def __init__(self):
self.gui = GuiFactory().getGui(self)

def main(self):
return self.gui.main()

def main():
return Installer().main()
NB 2 :
if it has to run in text mode, you should consider renaming "gui" to
"ui", since a CLI is not really a 'graphical' ui !-)

NB 3 :
I made the GuiFactory a class, but it could also be a simple function.

NB 4 :
there are of course other solutions to the problem, which may or not be
more appropriate...
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 21 '05 #2
bruno modulix a écrit :
You may want to have a look at the Factory pattern...

# outrageously oversimplified dummy exemple
class Gui(object):
def __init__(self, installer):
self.installer = installer

class PosixGui(Gui):
pass

class Win32Gui(Gui):
pass

class GuiFactory(object):
def getGui(self, installer):
if os.name == 'posix':
return PosixGui(installer)
elif os.name == 'win32':
return Win32Gui(installer)
else:
raise "os %s not supported" % os.name

class Installer(object):
def __init__(self, guiFactory):
self.gui = guiFactory.getGui(self)

def main():
inst = Installer(GuiFactory())
return inst.gui.main()

NB 1:
You may want to hide away the gui stuff:

class Installer(object):
def __init__(self):
self.gui = GuiFactory().getGui(self)

def main(self):
return self.gui.main()

def main():
return Installer().main()

Thanks for this, Bruno. It is much more elegant and adaptable than my
first attempt.
NB 2 :
if it has to run in text mode, you should consider renaming "gui" to
"ui", since a CLI is not really a 'graphical' ui !-)
You're right :))
NB 3 :
I made the GuiFactory a class, but it could also be a simple function. NB 4 :
there are of course other solutions to the problem, which may or not be
more appropriate...


Thanks a lot for these detailed explanations.
Jul 21 '05 #3
cantabile wrote:
bruno modulix a écrit :
You may want to have a look at the Factory pattern...
... demo of class Factory ...


Taking advantage of Python's dynamic nature, you could simply:
# similarly outrageously oversimplified dummy example
class Gui(object):
def __init__(self, installer):
self.installer = installer

class PosixGui(Gui):
pass

class Win32Gui(Gui):
pass

if os.name == 'posix':
makeGui = PosixGui
elif os.name == 'win32':
makeGui = Win32Gui
else:
raise "os %s not supported" % os.name
class Installer(object):
def __init__(self, guiFactory):
self.gui = guiFactory(self)

def main():
inst = Installer(makeGui)
return inst.gui.main()

--Scott David Daniels
Sc***********@Acm.Org
Jul 21 '05 #4
Scott David Daniels a écrit :
cantabile wrote:
bruno modulix a écrit :
You may want to have a look at the Factory pattern...
... demo of class Factory ...

Taking advantage of Python's dynamic nature, you could simply:
# similarly outrageously oversimplified dummy example
class Gui(object):
def __init__(self, installer):
self.installer = installer

class PosixGui(Gui):
pass

class Win32Gui(Gui):
pass

if os.name == 'posix':
makeGui = PosixGui
elif os.name == 'win32':
makeGui = Win32Gui
else:
raise "os %s not supported" % os.name
class Installer(object):
def __init__(self, guiFactory):
self.gui = guiFactory(self)

def main():
inst = Installer(makeGui)
return inst.gui.main()

--Scott David Daniels
Sc***********@Acm.Org


Thank you too for this tip. :)
Coming from C++ (mainly), I'm not used to this dynamic way of doing
things. That's usefull.
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nicolla MacPherson | last post by:
Hi I'm a newbie and want to display a pattern that will increment with each line of display. I thought i might be able to count the rows and use that info to increment my display. I've got my...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
7
by: farseer | last post by:
Here is the scenario: I have an interface which defines get methods for data that will make up a row in a table. However, the source of this data may, over time, switch/change (The company may...
3
by: jason | last post by:
is there a way to set up an array of bits of generic size, cycle through all the possible bit patterns, and detect a sub-pattern within the bit pattern? for cycling through possible patterns: i...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
5
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #1. (I'll put the other is a separate post.) Here is a standard example of the observer pattern in Python:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.