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. 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('@')])"
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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;
|
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...
|
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...
|
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.
| |
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?
|
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...
|
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)
|
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...
|
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:
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |