Python Getter Setters for Indigo variables

Posted on
Sat Nov 17, 2018 5:40 pm
Hackencrash offline
User avatar
Posts: 246
Joined: Dec 16, 2015
Location: UK

Python Getter Setters for Indigo variables

I am trying to create Python variable Getter Setters so that reading a normal Python variable, e.g. var.kitchen_television_onon variable will, will get the value of the matching Indigo variable and changing the Python variable will automatically set the Indigo variable such that they are in sync.

Code: Select all
class Var(object):
   
    def __init__(self):
        import indigo
        self.kitchen_television_on = True if indigo.variables[1650530773] == 'true' else False
       
    @property
    def kitchen_television_on(self):
        self.kitchen_television_on = True if (indigo.variables[1650530773] == 'true') else False
        return self.kitchen_television_on

    @kitchen_television_on.setter
    def kitchen_television_on(self, value):
        indigo.variable.updateValue(1650530773, value='true' if value else 'false')
        self._my_attribute = value
       
var = Var()
print var.kitchen_television_on

However, running this code to do this produces the following results. Any idea what I am doing wrong?

on depth exceeded while calling a Python object
Script Error Exception Traceback (most recent call shown last):

var.py, line 24, at top level
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
var.py, line 16, in kitchen_television_on
RuntimeError: maximum recursion depth exceeded while calling a Python object


FYI line 16 is return self.kitchen_television_on

Posted on
Sat Nov 17, 2018 5:43 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Getter Setters for Indigo variables

Changing answer.

You can't have the attribute you're saving be the same name as the property.

From StackOverflow: https://stackoverflow.com/questions/262 ... nd-setters

Code: Select all
class Obj:
    """property demo"""
    #
    @property
    def attribute(self): # implements the get - this name is *the* name
        return self._attribute
    #
    @attribute.setter
    def attribute(self, value): # name must be the same
        self._attribute = value
    #
    @attribute.deleter
    def attribute(self): # again, name must be the same
        del self._attribute

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Nov 17, 2018 5:52 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Getter Setters for Indigo variables

Untested

Code: Select all
    import indigo
    def __init__(self):
        self._kitchen_television_on = True if indigo.variables[1650530773] == 'true' else False
       
    @property
    def kitchen_television_on(self):
        self._kitchen_television_on = True if (indigo.variables[1650530773] == 'true') else False
        return self._kitchen_television_on

    @kitchen_television_on.setter
    def kitchen_television_on(self, value):
         self._kitchen_television_on = value
        indigo.variable.updateValue(1650530773, value='true' if value else 'false')
       
var = Var()
print var.kitchen_television_on

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Nov 17, 2018 7:14 pm
Hackencrash offline
User avatar
Posts: 246
Joined: Dec 16, 2015
Location: UK

Re: Python Getter Setters for Indigo variables

Absolutely perfect! Thank you FlyingDiver. Of course you can't have the attribute you're saving be the same name as the property - makes perfect sense when it's not 1am in the morning :?

Here is the code reposted as there were a couple of typos now corrected after getting past the main problem:
Code: Select all
"""
var.py

Getter Setters for Indigo variables
"""

class Var(object):
   
    def __init__(self):
        import indigo
        self._kitchen_television_on = True if indigo.variables[1650530773].value == 'true' else False
       
    @property
    def kitchen_television_on(self):
        self._kitchen_television_on = True if (indigo.variables[1650530773].value == 'true') else False
        return self._kitchen_television_on

    @kitchen_television_on.setter
    def kitchen_television_on(self, value):
        indigo.variable.updateValue(1650530773, value='true' if value else 'false')
        self._kitchen_television_on = value
       
# Tests
var = Var()
indigo.server.log(str(var.kitchen_television_on), type="var.kitchen_television_on")
var.kitchen_television_on = True
indigo.server.log(str(var.kitchen_television_on), type="Should be True")
var.kitchen_television_on = False
indigo.server.log(str(var.kitchen_television_on), type="Should be False")

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests