[Tutor] metaclass question

Kim Branson kim.branson at gmail.com
Tue Jan 23 23:10:47 CET 2007


Hi,

what i've ended up doing is the following

define a EmpiricalScore class that has all the methods for computing  
results

define a single method in  the evaluation class that is called   
Score. This method simply walks though a list and executes the  
methods in that list. There may be one or many.


     def score(self):
         """
         this function computes the interaction energy and returns a
         dict of key=value pairs for the scoring functions requested
         """
         for x in self.functionList:
             x()
         return(self.scoreResults)

The factory class takes as agument some input data and determine  
which of the methods in the Evaluation class should be called.
it then produces an instance of the EmpiricalScore class,   (called  
scoreObject,) and then append the methods from that instance to its  
internal list.

         scoreObject=EmpiricalScore()

         #for each valid scoring function in the functions
         #tuple we call the method which adds the attribute
         for x in self.functionsToUse:
             #we need to check if we want both smog and smogh
             if x == "smog":
                 if bothSmog == True:
                     continue
                 for y in self.functionsToUse:
                     if y == "smog_h":
                         scoreObject.functionList.append 
(scoreObject.calBothSmog)

This is possibly not the best way to approach this, the factory class  
is possibly not required and could be added to the  EmpiricalScore  
class. I think its probably better to separate these to keep things  
clean.  In a way this method is constructing a decorator for the  
EmpiricalScore.score  method.  Is there a way of appending a method  
to the class after it is initialized. Or adding decorators to the  
method after it is defined?

kim







On Jan 22, 2007, at 5:14 PM, Kent Johnson wrote:

> Kim Branson wrote:
>> Hi i'm interested in implementing a factoryclass in python
>> What i'd like to do is have my factoryClass produce an instance of  
>> a  class with some methods defined in arguments to the factory class.
>> The classes that are produced have many common methods, but a  
>> single  unique method. This method actually is a series of calls  
>> to a c++ api.
>> Depending on what we are doing with the produced class, i'd like  
>> the  unique method to call api function A, or api function B  
>> etc.   Alternatively the unique method might call A and the B and  
>> return a  dict of the results.
>> I'm doing this because i'd like all my produced class instances  
>> to  simply have  a calculateResults method which will then go and  
>> do the  right thing.  I don't want to set some values in the init,  
>> like A==  True  and have a if A: call methodA etc statement.
>
> Do you need to be passing in the unique method, or can you just  
> make a base class with the common methods and subclasses that  
> define their unique methods? For example,
>
> class Base(object):
>   def a(self):
>     pass
>   def b(self):
>     pass
>   def calculateResults(self):
>     raise NotImplementedError
>
> class A(Base):
>   def calculateResults(self):
>     return self.a() * self.b()
>
> class B(Base):
>   def calculateResults(self):
>     return dict(a=self.a(), b=self.b())
>
> Kent
>



More information about the Tutor mailing list