Setting a variable to a date in Python

Posted on
Fri May 17, 2019 11:39 am
gskarp offline
Posts: 143
Joined: Apr 19, 2012

Setting a variable to a date in Python

I am getting around to moving my legacy AppleScript to Python. I want to set a variable to a date that is 75 days in the future. How would you code that? I imagine it is pretty straightforward.

Posted on
Wed May 22, 2019 8:59 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Setting a variable to a date in Python

Code: Select all
from datetime import date, timedelta
target_date = date.today() + timedelta(days=75)
indigo.variable.updateValue(ID_OF_VAR, value=str(target_date))


Will result in something like 2019-08-05 as the variable value. You can format the date if you prefer a different date string.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed May 22, 2019 10:24 am
gskarp offline
Posts: 143
Joined: Apr 19, 2012

Re: Setting a variable to a date in Python

That works. Now how do I convert a variable to a number? That is., I have a variable that is a number, but it is not considered a number by python. I have extracted the content of a variable by using:

var_test = indigo.variables[ID_OF_VARIABLE)]

I use that with the line:

target_date = date.today() + timedelta(days=var_test)

The error I get is "unsupported type for timedelta days component: Variable"

Sorry for the newbie questions.

Posted on
Wed May 22, 2019 11:06 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Setting a variable to a date in Python

var_test = int(indigo.variables[ID].value)


Sent from my iPad using Tapatalk Pro

Posted on
Wed May 22, 2019 11:16 am
gskarp offline
Posts: 143
Joined: Apr 19, 2012

Re: Setting a variable to a date in Python

Thank you!! This is my first script conversion. I appreciate everyone's help!

Posted on
Wed May 22, 2019 11:31 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Setting a variable to a date in Python

howartp wrote:
var_test = int(indigo.variables[ID].value)


Or, alternately:

Code: Select all
var_test = indigo.variables[ID].getValue(int)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri May 24, 2019 12:53 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Setting a variable to a date in Python

As a nicer way to get variable int, that’s possible.

As a more wider means of casting to int across Python, mine is more transferable.

.getValue(int) doesn’t work on a random Python string.


Sent from my iPhone using Tapatalk Pro

Posted on
Fri May 24, 2019 10:42 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Setting a variable to a date in Python

Just presenting options...

Actually, our .getValue method is better at casting between types than going directly to Python. For instance:

Code: Select all
>>> indigo.variables[1531327628].value
u'1.2'
>>> int(indigo.variables[1531327628].value)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.2'
>>> indigo.variables[1531327628].getValue(int)
1
>>>


And:

Code: Select all
>>> indigo.variables[241925870].value
u'false'
>>> bool(indigo.variables[241925870].value)
True
>>> indigo.variables[241925870].getValue(bool)
False
>>>

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests

cron