Converting Indigo Lists and Dicts to Python Types

Posted on
Wed Jul 15, 2020 8:27 am
hannt offline
User avatar
Posts: 84
Joined: Jul 08, 2011
Location: TN USA

Converting Indigo Lists and Dicts to Python Types

I'm storing a list of dictionaries in a custom plugin device. The list is of type indigo.List() and the dictionaries are each of type indigo.Dict() based on Indigo Specific Data Types. I can't get json to work with these Indigo data types so I would like to convert the list into a local variable that is a Python list() containing items of type Python dict(). I could then use json to convert the list into a string. So two questions:

  1. Is there an easy way to convert indigo.List() or indigo.Dict() into a string using json?
  2. If not, is there any easier way than the code below to convert these Indigo data types to the Python equivalent types?
Here's how I'm doing it now. First a generic function to convert a simple indigo.List() or simple indigo.Dict():
Code: Select all
   def simpleConvertIndigoToPython(self, dataType, data):
      if dataType == 'list':
         newData = []
         for item in data:
            newData.append(item)
      elif dataType == 'dict':
         newData = {}
         for key in data.keys():
            newData[key] = data[key]
      return newData
To convert the list of dicts mentioned above, I call it like this:
Code: Select all
pythonList = []
for item in localPropsCopy['indigoList']:
   pythonList.append(self.simpleConvertIndigoToPython('dict', item))
Another way I found to convert an indigo.List() to list() is:
Code: Select all
import copy
newList = []
newList += copy.copy(indigoList)

Posted on
Wed Jul 15, 2020 9:31 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Converting Indigo Lists and Dicts to Python Types

Unfortunately, not at the moment. I myself have run across this issue lately. We really need to make the Indigo versions more easily serialized such that json would just work, and it's on the list of things to consider moving forward.

While I haven't thoroughly tested yet, I think this function should recursively convert any Indigo.Dict or Indigo.List (starting with one of those as the initial argument) to a native python hierarchy which can then be serialized as necessary:

Code: Select all
def convert_to_native(obj):
    if isinstance(obj, indigo.List):
        native_list = list()
        for item in obj:
            native_list.append(convert_to_native(item))
        return native_list
    elif isinstance(obj, indigo.Dict):
        native_dict = dict()
        for key, value in obj.items():
            native_dict[key] = convert_to_native(value)
        return native_dict
    else:
        return obj


A few obvious restrictions: the first object passed in should be an indigo.Dict or an indigo.List object, and any python containers (list or dict for instance) that themselves contain indigo.List or indigo.Dict objects won't be converted. Basically, if you've managed to mix and match throughout the hierarchy the function will likely not work for you. It could be altered to deal with that scenario of course, but my needs didn't require it.

This obviously doesn't convert the other way either since I haven't found the need yet.

In any case, maybe this will help you along the way.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jul 15, 2020 10:14 am
hannt offline
User avatar
Posts: 84
Joined: Jul 08, 2011
Location: TN USA

Re: Converting Indigo Lists and Dicts to Python Types

Thanks, Jay. Very nice!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests