-1

I've used list of lists [ [], [] ]

I've used list of tuples [ (), () ]

and I've used list of dictionaries [ {}, {} ]

Today I'm looking at list of instances [ <>, <> ]

I'm not sure how to traverse through the list elements' fields.

Here is python output (with carriage returns added for readability):


>>> import pulsectl

>>> pulse = pulsectl.Pulse('mserve')  # 'mserve' is irrelvant

>>> pulse.sink_input_list()
[
<PulseSinkInputInfo at 7f3c2a188290 - 
index=569L, mute=0, name=u'AudioStream'>, 

<PulseSinkInputInfo at 7f3c2a1884d0 - 
index=571L, mute=0, name=u'Simple DirectMedia Layer'>, 

<PulseSinkInputInfo at 7f3c2a188b50 - 
index=573L, mute=0, name=u'Simple DirectMedia Layer'>, 

<PulseSinkInputInfo at 7f3c2a188fd0 - 
index=575L, mute=0, name=u'Simple DirectMedia Layer'>
]

>>> print type(pulse.sink_input_list()[1])
<class 'pulsectl.pulsectl.PulseSinkInputInfo'>

I'm not sure how to traverse a list of classes. The - to separate the class name and dictionary of fields is weird. The fact dictionary key/value pairs are separated by = instead of : is alien.

I searched to see if an answer to this was already posted and I didn't get any results. Ultimately I'd like to convert the list of classes into a list of dictionaries for normal processing.

WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34
  • 1
    Those are [representation strings](https://docs.python.org/3/library/functions.html?highlight=repr#repr). Just iterate over like you would with any other list: `for input_info in pulse.sink_input_list(): ...`. – InSync Jul 08 '23 at 01:04
  • I'm voting to close this question as a duplicate of [for loop in Python](https://stackoverflow.com/questions/4170656/for-loop-in-python). – InSync Jul 08 '23 at 01:09
  • 1
    And, as a side note, your first job should be to upgrade from Python 2 to Python 3. Python 2 really is an antique, and support was dropped LONG ago. – Tim Roberts Jul 08 '23 at 01:20
  • @TimRoberts deprecation warnings are enabled and the program is designed to run in whatever Linux Python version is installed. This particular installation is Ubuntu 16.04 on extended maintenance. UTF-8, Unicode, future print, future with and subprocess32 are all used. Tkinter/TCL is version 8.6 and it like pulsectl.py are quite happy working with Python 2 or 3. Support for different versions of Gdk/Gtk is also backed it... – WinEunuuchs2Unix Jul 08 '23 at 14:21
  • Python 3 predates Ubuntu 16 by 8 years. There's something to be said for stability, but Python 3 really is better. – Tim Roberts Jul 08 '23 at 17:53
  • @TimRoberts Python 3 had some teething problems between 3.4 and 3.11 I think? Ubuntu 16.04 does have Python `3.5.2` dated Jan 26 2021.To use it I just change the shebang on line 1 to: `#!/usr/bin/env python3` I just want to finish this application I started three years ago and make sure it works in all versions from python version `2.7.12` up. – WinEunuuchs2Unix Jul 09 '23 at 15:55
  • I think we've moved on, but just to set the record straight, Python 3.5.2 was released in June of 2016. It's the same age as Ubuntu 16. – Tim Roberts Jul 09 '23 at 20:46
  • @TimRoberts `$ python3 Python 3.5.2 (default, Jan 26 2021, 13:30:48) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ` – WinEunuuchs2Unix Jul 10 '23 at 05:15

1 Answers1

2

It's not a list of classes. It's a list of objects. ALL of those things you mentioned are lists of objects. They are all traversed in EXACTLY the same way. You're making this harder than it is. pulse.sink_input_list()[1] gives you an object, it just has a funny name. You would print pulse.sink_input_list()[1].index.

sil = pulse.sink_input_list()
for i in range(len(sil)):
    print sil[i].index
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • TBH, I think the O.P. has no clue what they are doing and definitely does not understand that printing an object simply converts it to a string representation that, hopefully, conveys a useful summary to a human about the object rather than every single detail about the object. – Kurtis Rader Jul 08 '23 at 01:37
  • 1
    Yes. I actually wrote an essay about the fact that many people confuse the VALUE of an object with the REPRESENTATION of that object, which is a real problem with strings and backslashes. – Tim Roberts Jul 08 '23 at 02:59
  • This answer is a good start but requires you to know that `index`is one of the keys. Also the other keys `mute` and `name` aren't printed. – WinEunuuchs2Unix Jul 08 '23 at 14:34
  • Presumably, you wouldn't be trying to use this object unless you knew how to use it and what you wanted from it. You asked how to traverse a "list of classes", and I have shown you that. – Tim Roberts Jul 08 '23 at 17:51