I am trying to create a class that parses a JSON and selects all labels, or just a particular label that you prefer to later do a calculation with the value pair extracted off that label. This is working well for .all()
This is the first function, wich selects ALL available labels. But then I want to make it flexible and be able to call names = IncomeSources(result).spa()
to see the revenue results for the spa only. The code is working fine, but I will die if I have to write a function for each label separately. Is there more efficient way to do it ?
class IncomeSources(object):
def __init__(self, result):
self.result = result
def all(self):
labels_list = []
for index in self.result:
for key, value in index.items():
if 'accountLabelType' in key:
names = value
labels_list.append(names)
collection = set(labels_list)
return collection
def brickyard(self):
name = 'Brickyard'
labels_list = []
for index in self.result:
for key, value in index.items():
if 'accountLabelType' in key and value.startswith('BYD'):
names = value
labels_list.append(names)
collection = set(labels_list)
return collection
def schoolhouse(self):
name = 'Schoolhouse'
labels_list = []
for index in self.result:
for key, value in index.items():
if 'accountLabelType' in key and value.startswith('SCH'):
names = value
labels_list.append(names)
collection = set(labels_list)
return collection
def spa(self):
name = 'Spa'
labels_list = []
for index in self.result:
for key, value in index.items():
if 'accountLabelType' in key and value.startswith('Spa'):
names = value
labels_list.append(names)
collection = set(labels_list)
return collection