473,804 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__(sel f, 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":"qwer ty", "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 1263
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__(sel f, 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":"qwer ty", "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(obje ct):
def getGui(self, installer):
if os.name == 'posix':
return PosixGui(instal ler)
elif os.name == 'win32':
return Win32Gui(instal ler)
else:
raise "os %s not supported" % os.name

class Installer(objec t):
def __init__(self, guiFactory):
self.gui = guiFactory.getG ui(self)

def main():
inst = Installer(GuiFa ctory())
return inst.gui.main()

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

class Installer(objec t):
def __init__(self):
self.gui = GuiFactory().ge tGui(self)

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

def main():
return Installer().mai n()
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(obje ct):
def getGui(self, installer):
if os.name == 'posix':
return PosixGui(instal ler)
elif os.name == 'win32':
return Win32Gui(instal ler)
else:
raise "os %s not supported" % os.name

class Installer(objec t):
def __init__(self, guiFactory):
self.gui = guiFactory.getG ui(self)

def main():
inst = Installer(GuiFa ctory())
return inst.gui.main()

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

class Installer(objec t):
def __init__(self):
self.gui = GuiFactory().ge tGui(self)

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

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

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(objec t):
def __init__(self, guiFactory):
self.gui = guiFactory(self )

def main():
inst = Installer(makeG ui)
return inst.gui.main()

--Scott David Daniels
Sc***********@A cm.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(objec t):
def __init__(self, guiFactory):
self.gui = guiFactory(self )

def main():
inst = Installer(makeG ui)
return inst.gui.main()

--Scott David Daniels
Sc***********@A cm.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
4763
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 starting point using setw and don't want to use cout for every line of display. What should i be looking for to use. Thanks
17
6649
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
7
1846
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 choose to change data providers). Therefore i thought to myself, a type of Adapter Pattern is best here and so i proceeded with that. here's an example of what i did (note this implementation differs from the text book one due to the way data...
3
2126
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 was thinking of just using an unsigned int that increments for the cycling of bit patterns, but those only come in prescribed sizes. i was hoping for something more generic, that would allow for a 10bit pattern, for example. for the sub-pattern...
12
3043
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 itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new operations added without being changed itself.
34
11210
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
4
2387
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 my own tool (http://openswarm.sourceforge.net). I already started restructuring my code to separate the actual command implementations from the command-line scripts (which is optparser-based now) and have some ideas how to proceed. But probably...
1
4163
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 had no connection or reference to the object that triggered it. It goes something like this: (not syntactically correct..it's just for the idea)
2
2567
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 http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html. While I've read about implementing Data Access Objects by Factory Method Pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/images09/figure09_07.gif) and Abstract Factory Pattern...
5
1486
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:
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.