2008/11/13

Dynamic multiple inheritance (II)

It works. We are using multiple inheritance and also we change dynamically on one macro, one of its inheritance to adapt to the concrete measurement.

More understandable code that A and B classes should be:

class _ltb:

····def __init__(self):
········print "__init__ 4 __ltb"
····def mesh(self):
········print "do a mesh scan"

class _energy(_ltb):
····def showResults(self):
········print "Energy results"

class _emittance(_ltb):
····def showResults(self):
········print "Emittance results"

class ltb_energy(_energy):
····def run(self):
········self.mesh()
········self.showResults()

class ltb_emittance(_emittance):
····def run(self):
········self.mesh()
········self.showResults()

class ltb_offline:
····def run(self,operation):
········if operation == "energy":
············self.__class__.__bases__ = (_energy,)
········elif operation == "emittance":
············self.__class__.__bases__ = (_emittance,)
········self.mesh()
········self.showResults()

In the implementation, there is also multi-inheritance because the ltb_{energy,emittance,offline} also inherit from Macro (a class of the MacroServer).

The sources of the Sardana project are in sourceforge. This macros are not already uploaded to the tango-ds svn. It needs a little bit more test, and probabli next week we will publish it.

Nota: in the implementation, the classes ltb_{energy,emittance,offline} has another primary inheritance from Macro. But the classes _energy and _emittance cannot inherite from Macro, because are not macros itself, but uses method of this superclass.

No comments: