A place to store python functions in Indigo?

Posted on
Tue Oct 16, 2018 3:28 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

A place to store python functions in Indigo?

I’m sorry, but I recall there being a way of storing python scripts somewhere in Indigo (this is way back in 2012 or so) as an alternative to running the script as an action, and as an alternative to using a .py file.

Am I tripping?

If this is a thing, can I run functions from them? How? I want to store commonly used functions there, if there is a “there.”


Sent from my iPhone using Tapatalk

Posted on
Tue Oct 16, 2018 3:36 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: A place to store python functions in Indigo?


Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 16, 2018 4:18 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: A place to store python functions in Indigo?

Thanks Jay!

So, to be clear, if I wanted to write a function, myLogger:
Code: Select all
def myLogger(thisString):
     do stuff
     return


I would put it in a .py file in the above referenced directory, but how would I call it in an embedded python script in Indigo?
Note: The function doesn’t use any specific Indigo
IOM stuff.

Posted on
Tue Oct 16, 2018 4:45 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: A place to store python functions in Indigo?

If the name of the file is my_funcs.py, then:

Code: Select all
import my_funcs
my_funcs.myLogger("log this")


Be sure to select the Plugins->Reload Libraries and Attachments menu item when you make changes.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 16, 2018 4:49 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: A place to store python functions in Indigo?

I will name it My Funcs, for sure. It’s catchy.
Thank you.
Unfortunately I now have “My Funcs” to the tune of that Black Eyed Peas song in my head. From 2004. Will the IOM let me delete that? Or should I just exclude it...

Posted on
Mon Nov 05, 2018 8:52 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: A place to store python functions in Indigo?

Deterred but not broken...

Use case: I want my own logger, so I can review the day's activity

1) I added a script in the directory /Library/Python/2.7/site-packages
Code: Select all
"""
myLog.py
"""

try:
   import logging
except ImportError:
   print "Error importing logging"
   raise ImportError

try:
   import indigo
except ImportError:
   print "The indigo module can only be used by scripts started from within Indigo"
   raise ImportError

def log(input):
   logger = logging.getLogger('myapp')
   hdlr = logging.FileHandler('/Users/MYNAME/indigo/myLogger/myapp.log')
   formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', "%m-%d-%Y %H:%M:%S")
   hdlr.setFormatter(formatter)
   logger.addHandler(hdlr)
   logger.setLevel(logging.DEBUG)
   logger.error(input)


I call it from a script window in an Action Group like so:
Code: Select all
import myLog
myLog.log("c")


Each successive run appears to open an additional handler or logger! If I log 'a', 'b', 'c' in successive runs, it spits out like so:
Code: Select all
11-05-2018 21:50:56 ERROR a
11-05-2018 21:51:02 ERROR b
11-05-2018 21:51:02 ERROR b
11-05-2018 21:51:07 ERROR c
11-05-2018 21:51:07 ERROR c
11-05-2018 21:51:07 ERROR c


Any thoughts on how I check to see if a logger is already open, or something like that?

Thank you!

Posted on
Tue Nov 06, 2018 10:12 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: A place to store python functions in Indigo?

It's because you're creating the handler and formatter inside the method. So every time the method is called, a new version of the handler is added to the logger.

The best approach is to get the logger, create the handler and formatter and assign them outside the method. This will get executed once when the module is imported. Then the log method just needs to log.

Code: Select all
"""
myLog.py
"""

try:
   import logging
except ImportError:
   print "Error importing logging"
   raise ImportError

try:
   import indigo
except ImportError:
   print "The indigo module can only be used by scripts started from within Indigo"
   raise ImportError
   
hdlr = logging.FileHandler('/Users/MYNAME/indigo/myLogger/myapp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', "%m-%d-%Y %H:%M:%S")
hdlr.setFormatter(formatter)
logger = logging.getLogger('myapp')
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

def log(input):
   logger.error(input)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Nov 06, 2018 10:20 am
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: A place to store python functions in Indigo?

Thank you for the explanation! I’m sure this will be helpful in the future

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests