I have a list of objects. These objects have nested attributes, which were generated in accordance with a method by hpaulj's response in this post: Object-like attribute access for nested dictionary.
I want to be able to find attributes, and values of attributes within these objects, and manipulate data they hold. However, in the real scenario, there may be upwards of one million objects, and the attributes may be deeply nested, which makes searching through a flat list a costly exercise when lots of manipulations need to occur.
For example, imagine the list of objects is as follows:
list_of_objects = [object1, object2, object3, object4]
- object1 has the following attributes:
self.country = "Kenya", self.disease = "breast cancer"
- object2 has the following attributes:
self.country = "Kenya", self.disease = "diabetes"
- object3 has the following attributes:
self.country = 'Ireland', self.risk_factor.smoking = "Current"
- object4 has the following attributes:
self.country = 'Kenya', self.risk_factor.smoking = "Previous"
These objects were created from the following State
class:
class State:
def __init__(self, state_dictionary):
self._state_dictionary = state_dictionary
for key, value in self._state_dictionary.items():
if isinstance(value, dict):
value = State(value)
setattr(self, key, value)
An example state_dictionary
would be as follows, in the case of object3:
state_dictionary = {
"country":"Ireland",
"risk_factor":{
"smoking":"Current"
}
}
Importantly, nested attributes are State objects too.
I would like to affect all objects that possess an attribute, set of nested attributes, or possess an attribute with a specified value.
My thought, was to create a 'Controller', which would store that original list as separate lists within an object instance of the Controller class. Each of the original attributes and values would point to a list of the objects which contained those attributes or values, the basic design would be as follows:
class Controller:
def __init__(self, list_of_objects):
self.list_of_objects = list_of_objects # Our list of objects from above
self.create_hierarchy_of_objects()
def create_hierarchy_of_objects(self):
for o in self.list_of_objects:
# Does something here
After the create_hierarchy_of_objects
method has run, I would be able to do the following operations:
Controller.country.Kenya
would contain the list of all objects whose self.country is "Kenya", i.e object1, object2, object4Controller.disease
would contain the list of all objects who had the attribute self.disease i.e. object1 and object2Controller.risk_factor.smoking.Current
would contain the list of objects who had that set of attributes i.e. object3
The question is how would create_hierarchy_of_objects
work?
I few points of clarification
- Nesting is arbitrarily long, and values may be the same e.g.
self.risk_factor.attribute1.attribute2 = "foo"
self.risk_factor.attribtue3.attribute4 = "foo"
as well.
- There may be a simpler way to do this, and I would welcome any suggestions.