mardi 23 juillet 2019

How to create an extendable software model for different hardware drivers in Python?

I have to conceptualize a software model for different hardware drivers. The hardware in question are measurement instruments with the same core functionality but different additional features. The drivers and also the scripts where the drivers are used are programmed in Python. It should be easy to replace the used hardware by keeping almost the same script. The necessary changes to the script should be minimal when replacing the hardware.

The core model must therefore be the same for all different hardware drivers, so that scripts that only use the core functionality of the hardware are independent of the actually connected hardware. This is easily solvable with inheritance.

But how can this be solved for functionality that are only supported by some instruments and not by others? Let's take the following code as example:

class BaseDriver:
   def coreFunctionA(self):
      pass
   def coreFunctionB(self):
      pass

class DriverModelA(BaseDriver):
   def coreFunctionA(self):
      pass
   def coreFunctionB(self):
      pass

   def additionalFunctionX(self):
      pass
   def additionalFunctionY(self):
      pass

class DriverModelB(BaseDriver):
   def coreFunctionA(self):
      pass
   def coreFunctionB(self):
      pass

   def additionalFunctionX(self):
      pass

class DriverModelC(BaseDriver):
   def coreFunctionA(self):
      pass
   def coreFunctionB(self):
      pass

   def additionalFunctionY(self):
      pass

The core functionality that is the same for all instrument models is summarized in the BaseDriver. But how to handle functions that are only supported by some drivers (like "additionalFunctionX" and "additionalFunctionY")? If the functions additionalFunctionX in DriverModelA and in DriverModelB do the same thing, they should have the same signature, so that the interface is easier to understand by the users. I could put those functions also into the base class and throw an exception for all drivers that don't support this feature. But that would be very ugly code in my opinion.

So do you guys have any idea how to bring these additional functions together? I want to avoid that the same functionality is implemented in different ways for each driver. If a driver supports a certain functionality that is also supported by some other drivers, this functionality should also have the same call semantics or the same interface for all drivers.

Aucun commentaire:

Enregistrer un commentaire