Should drivers handle units?


Hi. I’ve just started with a message-based device that requires inputs with units, specifically Volts for a voltage and Hz for a frequency. I was wondering if it is advisable to implement units with Pint with the driver, which is the lowest level or do that at the model level.


It is an interesting question. If you are basing your driver with Lantz, units are already there. But if not, you may want to keep the requirements of your driver to a minimum, and leave the units to the model.

This question, however, has not an unique answer. Pint is not a particularly standard package. if you plan to share your drivers, it would be best to rely on well supported packages (like numpy and core python ones). With the model you can take care of handling units appropriately.

Depends on the complexity of the driver you are developing, especially if you don’t foresee it becoming part of other’s programs, you can include units and changing it later shouldn’t be a problem and it will speed up your development cycle.


I would say, that whenever you add support for drivers in your package, it is better to rely on a package supporting drivers (such as pint), then fully developing your unit-support yourself.


I think that what Martín was asking is that if you follow the idea of MVC pattern, should you implement units in the Model or in the Controller. Following the definition of Controller = Driver, it seems best to use units only of the device has units. For instance, a camera may have exposure time with units of seconds, but intensity as integers. And to handle units in such a case, you can use Pint if you are not using any other framework for the driver.