I’m using a Labjack for some Digital I/O with python 2.7.3 32bit and encounter the following:
This is the labjack u6 function I’m calling:
class PortStateRead(FeedbackCommand):
"""
PortStateRead Feedback command
Reads the state of all digital I/O.
>>> d.getFeedback( u6.PortStateRead() )
[ { 'FIO' : 10, 'EIO' : 0, 'CIO' : 0 } ]
"""
def __init__(self):
self.cmdBytes = [ 26 ]
def __repr__(self):
return "<u6.PortStateRead()>"
readLen = 3
def handle(self, input):
return {'FIO' : input[0], 'EIO' : input[1], 'CIO' : input[2] }
The function is returning (what appears to be) a dictionary, but when I assign the return to a variable it is assigned as a list.
>>> import u6
>>> handle = u6.U6()
>>> x = handle.getFeedback(u6.PortStateRead())
>>> x
[{'CIO': 15, 'FIO': 255, 'EIO': 255}]
>>> x['FIO']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
Assigning x[0] to a new variable assigns as a dictionary
>>> y = x[0]
>>> y['FIO']
255
Can someone explain this behavior to me please?
In the example call in the docstring the function returns a list, so I can assume this behavior is normal.
No, it’s a dictionary in a list. The square brackets are part of the value.