Quick Python Question

Posted on
Sun Jan 02, 2022 2:00 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Quick Python Question

So I’ve got a bunch of schedules that need to happen during different times of the year. I can easily set this up thru the Schedule GUI, but I end up with a bunch of schedules, all conditioned on “current date is between….”. It works, but I have a lot of schedules that don't fire except for a few weeks a year. So I was wondering about creating a Master Schedule in Python, so that I have a bunch of "if" statements that evaluate a range of dates, and act accordingly.

I got to this point where the date is evaluated and the action group executed,
Code: Select all
import time
Date = time.strftime  ("%x")

if Date == "01/02/22":
   indigo.actionGroup.execute(1590883785)

but obviously need this to happen between 2 dates. I've tried a few different punctuations on the "IF" statement, but can't get any of them to work.
Code: Select all
if Date >== "01/02/22" and <=="01/20/22":

I'm assuming there is an easy to to evaluate a range of dates but I can't figure that part out. Any suggestions?

Posted on
Sun Jan 02, 2022 2:22 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Quick Python Question

Code: Select all
if Date >= "01/02/22" and Date <="01/20/22":

Posted on
Sun Jan 02, 2022 2:26 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Quick Python Question

but that is US format: month/date/year
you need year/month/day
Code: Select all
DATE = time.strftime("%Y%m%d")
if Date >= "20220102" and Date <="20220120":

is between jan 2 and jan 20


====
time.strftime("%Y%m%d")
gives:
'20220102'

Posted on
Sun Jan 02, 2022 2:59 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Quick Python Question

Thanks

I'll look at both options and I'm sure I can figure out the rest. My first attempt was to deal with days of the year. (000-365), but thought that was going to be too clumsy for me to change occasionally.

Posted on
Mon Jan 03, 2022 11:18 am
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Quick Python Question

You'll likely want to use date objects directly rather than try doing string comparisons:

Code: Select all
from datetime import date
today = date.today()
start_date = date(month=1, day=1, year=today.year) # Start date is January 1st of the current year
end_date = date(month=1, day=31, year=today.year) # End date is January 31st of the current year
if today >= start_date and today <= end date:
    # today is within the start_date and end_date
    # so do whatever you want


There are a variety of other approaches (you could create a range of dates for instance), but this is the first that came to mind for me.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jan 03, 2022 8:45 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Quick Python Question

Thanks

I ended up with this, which so far seems to be working. I opted out of the year as I'd need to change it every year. Although so far it seems to be working, I won't really know for a few days 'until I'm past "0105", but preliminary tests suggest it should work.
import time
Date = time.strftime("%m%d")


if Date >= "0101" and Date <="0105":
indigo.actionGroup.execute(858203413) # 1/6 to 0105

if Date >= "0106" and Date <="0109":
indigo.actionGroup.execute(655869350) # 1/6 to 1/9

if Date >= "0110" and Date <="0212":
indigo.actionGroup.execute(1590883785) # 1/6 to 2/12

Posted on
Wed Jan 05, 2022 3:10 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Quick Python Question

I ended up with this, which so far seems to be working. I opted out of the year as I'd need to change it every year. Although so far it seems to be working, I won't really know for a few days 'until I'm past "0105", but preliminary tests suggest it should work.

Just for others/posterity, the reason Jay suggested avoiding the string comparisons is that this can be considered a bit fragile. Jay suggests building and comparing dates which is considered a pretty concrete and fundamental operation and won't change. Your approach may work now, but it also might break in the future.

As we know, at some point in the future, Indigo is likely to have to go to Python 3 because of Apple (and Python itself) dropping support for older versions. A date compare is almost 100% transferrable, but your code has several potential holes -- for instance, if in a new version of Python the month or day is no longer always two digits (so instead of "01" you get "1") then it will break. I'll give you this isn't likely but it IS a potential breaking point and we know that Python 2 -> 3 has breaking changes... those of us with plugins have probably all found at least one or two things that much change in our plugins.

Adam

Posted on
Fri Jan 07, 2022 10:25 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Quick Python Question

Adam, Jay,
I fully agree, BUT

when you learn to program it is important to get quick successes. Working with abstract items, objects, classes etc is a bigger step than a simple character compare.

That is how I got back into the saddle after a 30 year break.

Karl

Posted on
Fri Jan 07, 2022 11:47 am
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Quick Python Question

Thanks for your input. As a non-coder, I 'tend to look for the more simplistic solutions, as they are easier for me to understand, implement, and ultimately change when necessary. As luck would have it, I had arrived at my first solution before Jay's post. But Jay's code seems equally easy to see how it works, easy to implement, and easy to change.

Given your concern about breaking in the future, changing my scripts to Jay's suggestion doesn't seem that difficult to implement. I've got to mess around with implementing a few "if" lines that depend on the day of the week, but I'm guessing there is enough of a road map already for me to get that to work as well.

Thanks!

Posted on
Fri Jan 07, 2022 1:04 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Quick Python Question

kw123 wrote:
when you learn to program it is important to get quick successes. Working with abstract items, objects, classes etc is a bigger step than a simple character compare.


I'd argue that giving someone the best answer with code is a quick success, and it's a learning experience.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jan 07, 2022 5:36 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Quick Python Question

While I was happy with my initial solution, I can easily understand why it may not be the best one, so I did the grunt work and changed my whole script to this: (in part)

from datetime import date

today = date.today()

#Christmas 1/1/ to 1/5
start_date11 = date(month=1, day=1, year=today.year)
end_date11 = date(month=1, day=5, year=today.year)
if today >= start_date11 and today <= end_date11:
indigo.actionGroup.execute(858203413)

#Winter 1/6 to 1/9
start_date15 = date(month=1, day=6, year=today.year)
end_date15 = date(month=1, day=9, year=today.year)
if today >= start_date15 and today <= end_date15:
indigo.actionGroup.execute(655869350)


Originally, I had all the variables defined 1st, but I've got so many lines I was getting confused so ended up with this solution. For me, this organization is much easier to mange. And for anyone using this, the original code was missing an underscore, but that was easily found and fixed.

Again, thanks for all the help!

Posted on
Sat Jan 08, 2022 10:25 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Quick Python Question

Shortest winter ever. I want to live where you live

Posted on
Sat Jan 08, 2022 3:53 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Quick Python Question

After Winter, comes (not shown here) Mardi Gras! :lol:

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests

cron