2008/11/13

Dynamic multiple inheritance

I am having a little problem with the offline measurement on the linac. My task in the linac is to produce an application to measure the electron beam energy and emittance. A long a go, we start to document it on the Alba's site.

The idea of the offline measurement was on means of all, but not so clear because we did some offline with data from other synchrotrons. But now we like to repeat the measurements we already did with our linac. Always when a measurement had been launch we store the images. Then the offline is to reload the images and deceive the system like this is real.

Problems about we didn't take care before are, for example, the magnets doesn't move precisselly to the ordered position; there is a threshold on that. And when the fit will be repeated offline we have to use the old magnet values, not the dummy motors how we use to simulate that.

Then something specific has to be programed to do this. We had macros to do the online measurements and they are not usable to load data from an old session for the offline measurement. Ok, then we need a new macro:


But, the offline macro needs to be the same to simulate energy or emittance. Then the inheritance, apart to be multiple, has to be dynamically changed...

Why not an instance? Why not all the macros only inherit from 'Macro' and has an instance of __energy or __emittance? Because the three on top right uses methods from the 'Macro' class.

class A:
def method(self):
print "A"
def methodA(self):
print "AA"

class B:
def method(self):
print "B"
def methodB(self):
print "BB"

class C:
def setA(self):
self.__class__.__bases__ = (A,)
def setB(self):
self.__class__.__bases__ = (B,)
def setAB(self):
self.__class__.__bases__ = (A,B)
def setBA(self):
self.__class__.__bases__ = (B,A)

Ok, then what can be done is:
c = C()

c has 4 methods: {c.setA(),c.setB(),c.setAB(),c.setBA}

c.setA()
now has 2 methods more {c.setA(),c.setB(),c.setAB(),c.setBA,c.method(),c.methodA()}

with a call c.setB() the list of methods changes to {c.setA(),c.setB(),c.setAB(),c.setBA,c.method(),c.methodB()}

c.setAB()
now the list of methods is
{c.setA(),c.setB(),c.setAB(),c.setBA,c.method(),c.methodA(),c.methodB()}
and notice the call c.method() uses the one from A.

with c.setBA()
the list is the same, but now the c.method() uses the one from B.

Actualization: Be careful if you use multiple objects using this. If we have multiple instances, the modification affects all of them:
c1 = C()
c2 = C()
c1.setA()
c1.method()
A
c2.setB()
c2.method()
B
c1.method()
B

No comments: