I have an array containing a set of an object with many attributes. I want to get the values of specific attributes as a list in a simple way.
I do know that I can make a list of each attribute and save them into individual lists as follows.
attr = (o.attr for o in objarray)
But as there are a lot of attributes and these need to analyzed using plots, distributions etc. this is not an efficient way.
In my case, I am analyzing an array of 'Structure' objects which has attributes like lattice constants, position of atoms etc. And the object has functions to get distance, angles, etc. which when we give the index of atoms will return the corresponding values. What I want is to get a list of values (which may be an attribute like lattice constant or an output of a function of the object like distance between two atoms) each corresponding to each of the structures in the array. Making an individual list for each of the values needed (as mentioned above) is less efficient as a lot of such lists may need to be made and the values needed may differ depending on the purpose.
What I need is to get a list of values by something in the manner of:
objarray[a:b].attr
which can be used easily for plotting and other functions. But this doesn't work and gives an error:
[ERROR] 'list' object has no attribute 'attr'
Alternately, is there a way to make an array of objects which treats objects in the above mentioned way.
I would probably use the getattr
built-in function for this purpose.
>>> my_object.my_attribute = 5
>>> getattr(my_object, 'my_attribute')
5
To create the numpy array as you would want:
def get_attrs(obj, attributes):
"""Returns the requested attributes of an object as a separate list"""
return [getattr(obj, attr) for attr in attributes]
attributes = ['a', 'b', 'c']
attributes_per_object = np.array([get_attrs(obj, attributes) for obj in all_objects])
This answer is inspired from the answer of @energya using the getattr built-in function. As that answer makes a function to get a list of attributes of a specific object, while the question was for getting a list of one specific attribute for all the objects in the array of objects.
So using getattr function,
>>> my_object.my_attribute = 5
>>> getattr(my_object, 'my_attribute')
5
For getting a numpy array of a specific attribute of all objects:
def get_attrs(all_objects, attribute, args=None):
"""Returns the requested attribute of all the objects as a list"""
if(args==None):
# If the requested attribute is a variable
return np.array([getattr(obj, attribute) for obj in all_objects])
else:
# If the requested attribute is a method
return np.array([getattr(obj, attribute)(*args) for obj in all_objects])
# For getting a variable 'my_object.a' of all objects
attribute_list = get_attrs(all_objects, attribute)
# For getting a method 'my_object.func(*args)' of all objects
attribute_list = get_attrs(all_objects, attribute, args)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With