Help with an algorithm wanted

Russell E. Owen rowen at cesmail.net
Thu Jun 27 01:59:00 CEST 2002


In article <mailman.1025125654.6891.python-list at python.org>,
 William Park <opengeometry at yahoo.ca> wrote:

>On Wed, Jun 26, 2002 at 01:02:02PM -0700, Russell E. Owen wrote:
>> For instance, an object with A as the given would return D by solving 
>> A->C->D.
>
>> The real issue is creating such objects without hand coding each one 
>> (since in the real world I have more representations and the number of 
>> casese proliferates badly).
>
>If pairing (ie. mapping between 2 objects) occurs randomly, then it's
>virtually impossible, because you don't know which direction to go from any
>one object.  It would be travelling from city to city, without knowing
>which state or county.

Fortunately the pairings (conversion functions) are completely fixed. So  
the graph never varies, and it even has a fairly simple shape (basically 
a fork with 3 tines and a long handle, if that makes any sense).

The problem is that the graph has 10 nodes. Rather than hand code 10 
different objects, I'm trying to create a factory that will generate the 
required object based on which node (data representation) is a given 
from which the others are derived.

For example, given conversion functions CtoA(C), AtoC(A), CtoB(C), 
BtoC(B), CtoD(C), DtoC(D), then I'll want to be able to create four 
objects, one with A as a given, one with B, etc. The object for A as a 
given should act like:
class Agiven:
   def getA(self):
      return self.A
  def getB(self):
    return CtoB(self.getC)
  def getC(self):
      return AtoC(self.getA)
  def getD(self):
      return CtoD(self.getC)

I'm not too worried about figuring out what methods to run depending on 
which representation is a given. My main worry is how to generate the 
desired objects programmatically in a way that is fairly understandable.

The obvious thing is probably to create a new object and 
programmatically add methods to it. But I'm not sure how to do it. If 
that's messy, I may have to have generate the created object by passing 
it a set of functions in the init method. Bjorn Pettersen just sent me 
some code that may do the trick. i have to study it. I'm not yet sure if 
it creates objects or pseudo-objects.

Of course I could create a text representation of the object and eval 
it, but that really sounds ugly!

-- Russell



More information about the Python-list mailing list