Page 1 of 1

Indigo – Python documentation

PostPosted: Tue Feb 23, 2016 8:33 pm
by canalrun
Hello,

I'm writing a few very simple Python scripts. I've searched through the documentation, IOM, examples, and Python beginner websites.

I'm wondering where I could find documentation for some of the functions that seem to be available to Indigo scripts?

For example, I was writing a script to email a trigger notice with date and time if the Smoke Alarm went off. I found the examples for sending an email – those are almost enough.

One thing I want to do is append the date and time. I came up with (almost by luck):
theBody = "Indigo Smoke Alarm Trigger\n" + str(indigo.server.getTime().date()) + "\n" + str(indigo.server.getTime().time())

I see in the IOM documentation where indigo.server.getTime() is documented, But I don't see where .date() or .time() is documented. Where would these be documented? Are these two actually part of the Python language?

I'm sure there has got to be a better way to append the date and time to a string. Again, would that actually be part of Python rather than Indigo?

Thanks,
Barry.

Re: Indigo – Python documentation

PostPosted: Tue Feb 23, 2016 8:52 pm
by FlyingDiver
Date and time stuff you should just use the standard Python libraries. I think those functions you're using are for getting the time from Indigo, which is not necessary.

For general Python docs, you want https://docs.python.org/2/

Re: Indigo – Python documentation

PostPosted: Wed Feb 24, 2016 11:01 am
by jay (support)
In fact, the indigo.server.getTime() method returns a standard Python datetime object - so you would use the standard Python datetime module to manipulate it.

Re: Indigo – Python documentation

PostPosted: Wed Feb 24, 2016 11:18 am
by DaveL17
canalrun wrote:
I'm sure there has got to be a better way to append the date and time to a string. Again, would that actually be part of Python rather than Indigo?

I think that the prevailing opinion when it comes to Python is that there is no "best way" to do anything. Python's favorite things are simplicity and readability. Beyond that, whatever works for you rules the roost (frankly, if you're coding only for yourself, you can effectively do whatever you want.)

In this example, you're dealing with (at least) two different kinds of objects--a string object and a datetime object. You can't combine two different kinds of objects, so you need to do a bit of conversion. In this case, you're converting a datetime object to a string object in order to combine them with your other string objects. There's no "better way" that I know of; just preference.

This would be probably be my preference for readability:
Code: Select all
theDate = "\n" + str(indigo.server.getTime().date())
theTime = "\n" + str(indigo.server.getTime().time())

theBody = u"Indigo Smoke Alarm Trigger %s %s" % (theDate, theTime)

And you could even break it down further:
Code: Select all
theDate = indigo.server.getTime().date()
theDateStr = "\n" + str(theDate)

theTime = indigo.server.getTime().time()
theTimeStr = "\n" + str(theTime)

theBody = u"Indigo Smoke Alarm Trigger %s %s" % (theDateStr, theTimeStr)

There. I've probably confused you more! :D
Dave