def as_table(obj_list,
show_id=False,
sort_headers=True,
hide_fields=['href', 'type']):
if len(obj_list) == 0:
return ''
else:
if sort_headers:
headers = sorted(obj_list[0].keys())
else:
headers = obj_list[0].keys()
if not show_id and 'id' in headers:
headers.remove('id')
for field in hide_fields:
if field in headers:
headers.remove(field)
table = []
for obj in obj_list:
table.append(
[obj.get(k) if k in obj.keys() else '' for k in headers])
return tabulate(table, headers)
If sort_headers=False and a key from hide_fields is used ('href'), headers.remove(field) will cause an error because dict_keys has no attribute remove
headers = sorted(obj_list[0].keys()) results in a list, which has the attribute removeheaders = obj_list[0].keys() results in a dict_keys, which does not have the attribute remove
This bug prevents tables to have any header defined in hide_fields if sort_headers=False
If sort_headers=False and a key from hide_fields is used ('href'),
headers.remove(field)
will cause an error becausedict_keys has no attribute remove
headers = sorted(obj_list[0].keys())
results in a list, which has the attributeremove
headers = obj_list[0].keys()
results in adict_keys
, which does not have the attributeremove
This bug prevents tables to have any header defined in
hide_fields
ifsort_headers=False