Making lists in python

Posted on
Mon Sep 09, 2013 2:21 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Making lists in python

Could someone help me build a list of devices in python.

In applescript I can do something like

Code: Select all
set devlist to devices whos model is "blablabl"

Posted on
Mon Sep 09, 2013 2:52 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Making lists in python

foo = [] # Could also do foo = [1,2,'something',4,5,6]
foo.append(123)
foo.append("asdf")
foo.append("foo")
print foo

devices = []
devices.append(123) # where 123 is some device ID
devices.append(dev.id) # where dev is a device instance variable you already have

Oh, and for device are you wanting a list of device IDs? That would be recommended if it will be a long list since if you store devices themselves they might be out-of-date by the time you use them.

Image

Posted on
Mon Sep 09, 2013 3:02 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Re: Making lists in python

In your example do I have to cycle through every device in the list until it meets the requirement then add it to the list? Compared to the one line I use in applescript?

I dont know about your device id question. Now in applescript I build list of devices by name

Code: Select all
set devlist to devices whose name contains "Living Room"

or
Code: Select all
set devlist to devices whose name contains "Celling Fan"


Or sometimes I use the description or model of the device. I call these device lists every time I run a script I don't store them.

Posted on
Mon Sep 09, 2013 3:09 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Making lists in python

There are several ways to do this but it depends on what you are trying to accomplish. Specifically what are the requirements of the devices you are trying to put into the list?

Image

Posted on
Mon Sep 09, 2013 3:28 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Re: Making lists in python

Different things depending on what I'm doing. But as in my example sometimes I want all the devices that have "Living Room" in the name, or all devices that have "Celling fan" in the name or I might want to get all devices which are a "Hue Bulb".

Posted on
Mon Sep 09, 2013 3:40 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Making lists in python

Building a device list for all devices that have the word "Living Room" in their name:

Code: Select all
dev_list = [dev for dev in indigo.devices.iter() if "Living Room" in dev.name]

Building a list of device IDs for all devices that have the word "Living Room" in their name:

Code: Select all
dev_id_list = [dev.id for dev in indigo.devices.iter() if "Living Room" in dev.name]

Image

Posted on
Mon Sep 09, 2013 4:11 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Re: Making lists in python

Perfect. Exactly what I was looking for.

I can change the dev.whatever based on what Indigo supports.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 11 guests