Writing Common Code for Python Scripts

You can share common functions/code between your event action scripts. To do this, you can create a Python package and import it. See the Python documentation for information on how to create a package and where to locate it.

For small projects you can add the scripts to a subdirectory and import them into your script.

Example:
  1. Create a subdirectory called Common under the project Scripts directory.
  2. Add your common functions to a file called helpers.py.
  3. Import the helper functions in you event action script by running import common.helpers.
The directory structure for the above example:
Scripts directory: Scripts\common directory:




Contents of common\helpers.py script:

import cimplicity
import datetime as dt

def fmt_response(msg: str) -> str:
    resp = f"{msg} {dt.datetime.now().isoformat()}"
    return resp

def send_response(resp: str) -> None:
    print(resp)
    cimplicity.log_status(cimplicity.StatusSeverity.SUCCESS, "event script", resp)
    cimplicity.point_set("response1", resp)

Contents of event action script Script1.py

import cimplicity
import common.helpers

class EventHandlerState:

    def __init__(self, event_action_context: cimplicity.EMEventActionContext):
        pass

    def do_event_action(self, event_data: cimplicity.CimEMEvent):
       resp = common.helpers.fmt_response("script ran at")
        common.helpers.send_response(resp)
        pass

    def do_shutdown(self, event_data: cimplicity.CimEMEvent):
        pass

def do_test():
    EventHandlerState(None).do_event_action(None)
    pass
if __name__ == "__main__":
    do_test()