Python coding question

Posted on
Thu Sep 19, 2013 6:13 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Python coding question

I have been playing with some extensions to the apcupsd plugin and in one case, I want to call an existing method that expects an Indigo action and device object. If I log an action received from Indigo, using
    indigo.server.log('Received action: \n%s\n' % (action))
I get:
    delayAmount : 900
    description : plugin action
    deviceId : 145579207
    pluginId : com.berkinet.apcupsd
    pluginTypeId : apcupsdServerEvent
    props : com.berkinet.apcupsd : (dict)
      actionType : commok (string)
    replaceExisting : True
    textToSpeak :

Now, I have added a class to create my own action object with just the fields I need for my method. The method processes the object just fine. But, when I attempt to log the object, I get
    <__main__.Action object at 0xfd1c710>

I was just wondering why Indigo's object can be interpreted by a %s and mine can't?

For reference, here is the class that creates the object:
    Code: Select all
    class Action(object):
        def __init__(self):
            self.deviceId = 1
            self.actionType = None
            self.pluginTypeId = 'apcupsdServerEvent'
            self.props = {'actionType': 'commok'}

And here is where I actually create my object:
    Code: Select all
    action = Action()
    action.deviceId = 145579207
    action.pluginTypeId = 'apcupsdServerEvent'
    action.props['actionType'] = event
    action.pluginTypeId = 'apcupsdServerEvent'

Posted on
Thu Sep 19, 2013 7:47 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Python coding question

Indigo python objects define the __str__ method to inspect the object (and return it as a string). You could do something similar with your Indigo action facade object. FYI, internally we are using the dir() function to inspect the object (to build the string inside __str__).

Image

Posted on
Thu Sep 19, 2013 8:58 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python coding question

Thanks Matt, That was it.

For others who may have the same question here is my code. Warning: this is probably not the most pythonista approach, and is probably not very extensible, plus lots of other caveats. But, IWFM*
Code: Select all
class Action(object):
    def __init__(self):
        self.deviceId = None
        self.pluginTypeId = None
        self.props = {'actionType': None}
    def __str__(self):
        desc_str = "deviceId: %s \npluginTypeId: %s \nprops: %s \n" %(self.deviceId, self.pluginTypeId, self.props)
        return desc_str


and called by:
Code: Select all
        action = Action()
        action.deviceId = 145579207
        action.pluginTypeId = 'apcupsdServerEvent'
        action.props['actionType'] = event


* It Works For Me

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests