[Tutor] How to use function from specific module and then switch to other module
Ertl, John C CIV 63134
john.ertl at navy.mil
Fri Nov 7 19:56:40 CET 2008
Classification: UNCLASSIFIED
Caveat (s): FOUO
Kent,
Thanks for the lead. I eventually did something like the Strategy Pattern you sent. It was so much simpler when I just inherited the functions. I think I need to redesign the code but for now it works and my boss will be happy.
Thanks again.
John Ertl
Meteorologist
FNMOC
7 Grace Hopper Ave.
Monterey, CA 93943
(831) 656-5704
john.ertl at navy.mil
Classification: UNCLASSIFIED
Caveat (s): FOUO
From: Kent Johnson
Sent: Thu 11/6/2008 7:36 PM
To: Ertl, John C CIV 63134
Cc: tutor at python.org
Subject: Re: [Tutor] How to use function from specific module and then switch to other module
On Thu, Nov 6, 2008 at 5:54 PM, Ertl, John C CIV 63134
<john.ertl at navy.mil> wrote:
> Classification: UNCLASSIFIED
> Caveat (s): FOUO
>
> I have a program that collects weather data from weather models. I
> originally had a module that contained a bunch of function that I used. So
> I just added it to the init of the class I was using and inherited the
> functions. That worked great but now I have two different models that I can
> get weather from. Each Module A and B have the exact same function names
> in them but they both do slightly different things.
It might help to see a working example of what you did for one model.
>
> The idea is as I step through a list I want to use a different function
> (same name but from a different module) for each element in the list. How
> do I have a generic way to do this.
>
> for example for point 1 I want to use the rain function from Module A and
> then for point 2 I want to use the rain function from Module B. At first
> though I would just init the class from either A or B for each point but I
> need the function from A or B to be able to use function from my main
> program...that is why the inheritance thing worked great for just one
> module.
Don't try to make the model A and B into base classes of the forecast.
Just pass the forecast object to the model. So your rain() method will
look more like this:
def rain(self, fc):
fc.calTime() # this function is in the main forecast class
fc.rain = do stuff for model A
where fc will be the forecast instance.
You might also be able to use simple functions rather than classes:
def rain(fc):
fc.calTime() # this function is in the main forecast class
fc.rain = do stuff for model A
then just call moduleA.rain(self) or moduleB.rain(self).
You might want to read about the Strategy pattern, that is what you
are doing. Here is a Python example of a class-based Strategy.
http://mail.python.org/pipermail/python-list/2006-April/379188.html
This example passes the strategy to the constructor but you can set it
in your pointInfo() method if you like. I would just make one function
to handle each point though:
for each in x.pointList:
x.handlePoint(each)
where
def handlePoint(self, point):
pointStrategy = ...
pointStrategy.rain(self, point)
Kent
Kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081107/7a1427ea/attachment-0001.htm>
More information about the Tutor
mailing list