Python import directives at the top of each and every jython rule file

Hi, since I’ve started using jython for my rules, one thing annoyed me, is that I have to have these two lines at the top of every script.

from core.rules import rule
from core.triggers import when

Is there a way / mechanism that would allow me to do away with having those at the top of every one of my rule script?

I guess you could compile your own personal version of Jython from source that includes those libraries.

The need to import stuffy like this is pretty standard across many programming languages. If you are coming from Rules DSL it may feel annoying but really it is Rules DSL that is the standout, not Python (or JS or Groovy) in this respect.

One important reason you have to import these is that they are not part of OH. Another is that these decorators are not the only way to define rules in Python and you do not need these decoratorss for every file you might create. For example, if you write tests, you will most likely not even have rules to trigger your test code, it just runs.

I don’t think there is any way to avoid these imports.

5 Likes

You could reduce these down to one import by messing around with packages and an __init__.py. Totally not worth it IMO. Are you aware you can have more than one rule per script file? Put all your rules into one script file and then you only need these imports once!

1 Like

Thanks for the confirmation that there isn’t a straight forward way. I’ll continue having them at the top of my scripts. Maybe vscode can auto insert them whenever I created a new .py file inside a certain directory… but trying to find that out will not be easy I guess.