In the code below, I have a dictionary of attributes and values. I am checking to see if the value if an attribute is True, and if so adding the attribute to a list for later processing (it becomes part of a SQL group by).
This code works fine, but I was wondering how it could be more "Pythonic". Note that the col_map
dict is actually a lot bigger.
col_map = {
'dim_3a_group_checkbox': 'pn_group',
'dim_3a_family_checkbox': 'pn_family',
'dim_3a_root_checkbox': 'pn_root',
}
# get an object with the attributes (eg:dim_3a_root_checkbox) as True / False
supply_kpi = self.browse(cr, uid, ids, context=None)[0]
group_by = []
for key, value in col_map.iteritems():
x = getattr(supply_kpi,key)
if x: # only add if attribute is True
group_by.append(value)
print group_by
Example results
['pn_family', 'order_type', 'pn_group', 'categ']