I am confused about the person objects returned by the Contacts module. I came across this issue because I was trying to make a dataset class. I thought I should harmonise the data once read in from the source. So I thought translate the data to namedtuples. Rightly or wrongly, could have been another format. But I am guessing contacts.person is a little different. Maybe it just uses slots, I am not sure. Anyway, I went the long way around and converted the list of contacts to namedtuples. But it's slow on my iPad Pro. I have 992 contacts.
Any comments welcome. I just have a feeling I am missing something very basic here. Either speeding up the func to create a list of namedtuples or by passing it all together. Again, I am trying to get to one internal data structure format. In this case namedtuples. The idea is that data from other sources will be loaded from .json files, memory or whatever, but will land in a nice format internally.
Slow motion code
import ui, editor, contacts
from collections import namedtuple
def objlist_to_named_tp(obj_list, filter_list = []):
'''
objlist_to_named_tp:
Desc:
Convert a list of objects in to a list of namedtuples.
params
obj_list = list of objects. The objects in the list need to
be able to support __dir__
filter_list = exclude any attr that appears in this list
'''
tup_list = []
for obj in obj_list:
d = {k:getattr(obj, k) for k in dir(obj) if not k.startswith('_') and k not in filter_list and not callable(k)}
np= namedtuple('DataItem', d.keys())(**d)
tup_list.append(np)
return tup_list
if __name__ == '__main__':
lst =objlist_to_named_tp(contacts.get_all_people(), filter_list = 'vcard')
p = lst[0]
print(p.first_name,p.last_name, p.birthday)
print('records=', len(lst))
print('*'*50)
#print(p._source)