Chris Aaaaa Chris Aaaaa - 1 year ago 122
Python Question

Python - howto keep type of elements in list

Sorry first to the pythonistas, C/C++ programmer having to do some python.

I am porting some C (not C++), so I essentially want to create a list of structures to pass around for initialization, processing and writing on to a device over the serial port.

I am keeping the python very close to the C so when the C is updated the python may easily get the same updates, so this means it looks procedural and there is some stuff I realize is nonsense, like creating an array, passing it off to be initialized and receiving it back (I understand that the one I create and pass in in is redundant)

My problem is that when the array arrives to be processed, my list of recordtypes has become a flat list, so I can no longer accesses elements of my recordtype by name.

I can cast them back (aka recreate each element from the list) but I would rather work out how to keep the type information intact.

I have distilled the code to the following:

GainSettings = recordtype('GainSettings', 'label value')

def init_arrays(Gains):
labels = [ "Gain_1:", "Gain_2:", "Gain_48:" ]
for itr in range(3):
value = []
value.extend([0] * CHANNELS)
label = labels[itr]
entry = GainSettings(label, value)
return True, Gains

def process_array(Gains):
thing = Gains[0].label # dies here with AttributeError: 'str' object has no attribute 'label'
thing += "wibble"
Gains[0].label = thing
return True, Gains

def main(args):
# char* label;
# sint16 value[CHANNELS];
Gains = [] # will be array of gain settings
ret, Gain = init_arrays(Gains)
ret, Gains = process_array(Gains)

I have a breakpoint where it fails so I can check the array, this tells me:

>>> print type(Gains)
<type 'list'>
>>> print Gains
['Gain_1:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Gain_2:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Gain_48:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
<type 'list'>
>>> print type(Gains[0])
<type 'str'>
>>> print type(Gains[1])
<type 'list'>

I would like it to say it is a list of "GainSettings".

In reality there are a number of similar arrays, they have common items like label, the processing functions are supposed to take any type of array and use the similar parts to do the work.



Answer Source

As I said in a comment, first of all, Python is not C. My suggestion is: take advantage of it, and use Python for your own good. Do not try to mimic C code. It will only make things harder.

On the other hand, you are flattening your list in an unexpected way. I think instead of:


You want:


See the difference:

>>> from recordtype import recordtype
>>> A = recordtype('A', 'x y')
>>> x = A(1, 2)

# Extend
>>> l = []
>>> l.extend(x)
>>> l
[1, 2]

# Append
>>> l = []
>>> l.append(x)
>>> l
[A(x=1, y=2)]

The method append() adds an element to the list, while extend takes the recordtype as an iterable and, therefore, adds elements to the list for each of the records in the recordtype.

Python is a beautiful programming language. Don't waste this opportunity and try to learn some! ;-)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download