Reputation: 185
I am cleaning up some ROS robot code using the method in this QA. This is not ROS related.
Here is the original code:
data = []
data.append(sensor[thermo].data.thermo)
data.append(sensor[imu].data.imu.x)
data.append(sensor[imu].data.imu.rotation.x)
Now I have a list of tuples containing all the topics so I can loop around:
topics = [('thermo', 'thermo'),
('imu', 'imu.x')
('imu', 'imu.rotation.x')]
and:
for sensor, topic in topics:
data.append(getattr[sensor].data, topic)
This works for thermo
, but not for imu
, and I am getting the following error:
AttributeError: 'imu' object has no attribute 'x'
How can I fix the getattr
statement to achieve the goal here?
Upvotes: 0
Views: 132
Reputation: 155
The function reduce
of functools
can be used:
reduce(getattr, "att1.att2.att3".split('.'), sensor[imu])
Upvotes: 1