Commit cf6980ef authored by LammLukas's avatar LammLukas

Added list of attributes to be exported to write_json and data2dict in exportable class.

parent 04ec04d9
......@@ -12,28 +12,29 @@ class Exportable(object):
"""Empty constructor"""
pass
def data2dict(self):
def data2dict(self, export_list=[]):
"""Store attributes in dictionary for .json export
"""
data = {}
for attr_name in self.get_attributes():
attribute = getattr(self, attr_name)
if isinstance(attribute, list):
if attribute and isinstance(attribute[0], Exportable):
item_list = []
for item in attribute:
item_data = item.data2dict()
item_list.append(item_data)
data[attr_name] = item_list
if not export_list or (attr_name in export_list):
if isinstance(attribute, list):
if attribute and isinstance(attribute[0], Exportable):
item_list = []
for item in attribute:
item_data = item.data2dict()
item_list.append(item_data)
data[attr_name] = item_list
else:
data[attr_name] = attribute
else:
data[attr_name] = attribute
else:
data[attr_name] = attribute
return data
def write_json(self, file):
def write_json(self, file, export_list=[]):
"""Write object data to .json file"""
data = self.data2dict()
data = self.data2dict(export_list)
with open(file, 'w') as outfile:
json.dump(data, outfile, indent=4)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment