Forum Discussion

Stuart_Weenig's avatar
Icon for Mastermind rankMastermind
2 years ago

SDK shortcut for getting a good dict of objects

Was doing some coding today and came up with a handy bit of code for getting a list of objects into a nice, neat dictionary:

# download 1000 devices (doesn't account for pagination) and bring the properties up to the root level of the device
devices = {{**x.to_dict(), **{ for y in x.custom_properties+x.auto_properties+x.inherited_properties+x.system_properties}} for x in lm.get_device_list(size=1000).items}

# the above one-liner is probably enough, but you can further clean it up:
for x,y in devices.items():
  for l in ['custom_properties','auto_properties','inherited_properties','system_properties']:
    del y[l]


Essentially, this creates a dictionary of dictionaries. The top level dictionary (devices) uses the device ID as the key and contains a dictionary as the value. Each device's dictionary contains all the attributes you normally expect to be there, plus all the properties all in a flat dictionary (instead of a list of dictionaries with "name" and "value" as the keys).

No RepliesBe the first to reply