What I am trying to do is functionally easy enough to code, but I am looking for a clean and scalable way to organize this... "Beautiful is better than ugly."
I have an app where we are gathering information about an item from several different places. Once the raw data is gathered, we need to extract many attributes from this data.
The interface to the item's data should work like a simple dict
, accessing the attributes with a friendly key name. I know I can accomplish this by subclassing one of the ABCs from the collections module.
My question is: How do I (cleanly) organize the many functions needed to extract the data attributes?
I could use cached properties... Break out the data sources and extraction functions into separate modules / classes...
I have considered putting all of them in a single class like follows:
class item_data(object):
def __init__(self, item_name):
self.name = item_name
self._item_data = {'name': item_name}
self._data_a = _get_data_from_source_a()
self._data_b = _get_data_from_source_b()
def _get_data_from_source_a(self):
pass
def _get_data_from_source_b(self):
pass
def _extract_attr_1(self):
# Extract some data attribute from _data_a
pass
def _extract_attr_2(self):
# Extract some data attribute from _data_a
pass
def _extract_attr_3(self):
# Extract some data attribute from _data_b
pass
_attr_extract_methods = {
'Attribute 1': _extract_attr_1,
'Attribute 2': _extract_attr_2,
'Attribute 3': _extract_attr_3,
}
def __getitem__(self, item):
if item not in self._item_data:
self._attr_extract_methods[item](self)
return self._item_data[item]
If I wanted to break out the data sources (and the associated attribute extraction functions) into their own modules / classes, how can I do so in a clean and scalable fashion so that new data sources and attributes can be easily added later? Is there a way to enable the data source classes to register themselves and their associated attributes with the top-level class?
Note: This is being leaveraged in an app that is written to support both Python v2.7+ and v3.x.