Help with an algorithm wanted: a solution (improvements gratefully accepted)
Russell E. Owen
rowen at cesmail.net
Thu Jun 27 19:57:19 EDT 2002
More information about the Python-list mailing list
Thu Jun 27 19:57:19 EDT 2002
- Previous message (by thread): Pythoniac: Thoughts on a hardware Python processor
- Next message (by thread): Help with an algorithm wanted
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In article <rowen-CE3C0F.13020226062002 at nntp1.u.washington.edu>, "Russell E. Owen" <rowen at cesmail.net> wrote a long complicated message about a generating what is basically a data converter object. I really appreciate everybody's help on this. I apologize for not making the problem clearer in the first place. I offer my solution below with enough comments to, I hope, make the problem clearar. Any critique would be appreciated. """Sample object that converts between different data representations A-D, starting from a given value in one representation. In reality there will be more representations and the conversion code will be complicated (though much of it may reside in an external library of subroutines). But the graph of data representations connected by conversion functions will always be a constant and will always be very simple (one hub with lines of varying numbers of nodes radiating from it). Figuring out which order to do the conversions (as a function of what is a given) is solved with a rather simplistic algorithm that tests too many cases but is short and easy to read. Thanks to Bjorn Pettersen for his help (but the crudeness is mine). The main thing I am worried about is: given the order in which to do the conversions, how best to create an object that implements it. The solution shown here works, but I suspect there's a better way. It feels as if I'm creating some sort of meta-object (by using a function dictionary) instead of making best use of the existing optimized object mechanism. """ class CnvObj (object): def __init__(self, givenValue, whatGiven): assert whatGiven in ("A", "B", "C", "D") self.givenValue = givenValue self.funcDict = {whatGiven: self.getGiven} self.addFunctions(whatGiven) def addFunctions(self, fromWhat): for toWhat in ("A", "B", "C", "D"): if not self.funcDict.has_key(toWhat): funcName = "%sfrom%s" % (toWhat, fromWhat) if hasattr(self, funcName): self.funcDict[toWhat] = getattr(self, funcName) self.addFunctions(toWhat) def getItem(self, item): return self.funcDict[item]() def BfromC(self): return "BfromC(%s)" % self.getItem("C") def CfromA(self): return "CfromA(%s)" % self.getItem("A") def DfromC(self): return "DfromC(%s)" % self.getItem("C") def getGiven(self): return self.givenValue #...etc. only the functions needed for "A given" are shown co = CnvObj("value for A", "A") for rep in ("A", "B", "C", "D"): print co.getItem(rep) """this prints out: value for A BfromC(CfromA(value for A)) CfromA(value for A) DfromC(CfromA(value for A)) """
- Previous message (by thread): Pythoniac: Thoughts on a hardware Python processor
- Next message (by thread): Help with an algorithm wanted
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list