petervizi/python-eeml

adding units on the fly

Closed this issue · 2 comments

I've attempted to add a Unit to eeml:

eeml does not know of litres

class liter(eeml.Unit):
def init(self):
eeml.Unit.init(self, 'liter', 'basicSI', u'l')

My Python skills are apparently inadequate, as this does not work afterwards:
pac.update([eeml.Data(0, level, unit=eeml.Unit.liter())])

This may work if I modify eeml directly, or add to your code, but how do I do this directly in my python code? The problem is, that I'd like to publish my project ( monitoring water level with a raspberry), and for simplicity I'd like to contain all python code in one place.

Hi overheder,

You almost got this right, try

pac.update([eeml.Data(0, level, unit=liter())])

As for the other thing you mention about all your code in one place:

  1. don't need to inherit from Unit at all, you can do pac.update([eeml.Data(0, level, unit=eeml.Unit('liter', 'basicSI', 'l')])
  2. you still have to deploy python-eeml wherever your code will be used so might as well add the liter class to eeml/units.py for the benefit of other users

Obviously I would prefer if you chose option 2, but it's up to you. Pull requests are always welcomed :-)

Thanks!

I was unable to make the update work with my own liter definition:
pac.update([eeml.Data(0, level, unit=liter())])
NameError: name 'liter' is not defined

So I went with your option 1, in case anyone tries your code, there is a missing parenthesis, here is the working version:

pac.update([eeml.Data(0, level, unit=eeml.Unit('liter', 'basicSI', 'l'))])

Have a great day

Henrik