I have many delayed dict returned from a dask delayed function. I would like to aggregate them into a summary_dict
like below. items
function doesn't work on delayed object.
@dask.delayed
def get_dict(date):
return {
'a': {'date': date},
'b': {'date': date}
}
summary_dict = {}
dates = [d1, d2, d3, ...]
for date in dates:
date_dict = get_dict(date)
# the following doesn't work because date_dict is a delayed object
for key, val in date_dict.items():
summary_dict.setdefault(key, []).append(val)
I'm able to do the following to make it work. However, it is quite ugly because I have to hardcode the keys ahead.
hardcoded_keys = ['a', 'b']
get_dict_items = {key, dask.delayed(operator.itemgetter(key)) for key in keys}
summary_dict = {}
dates = [d1, d2, d3, ...]
for date in dates:
date_dict = get_dict(date)
for hardcoded_key in hardcoded_keys:
val = get_dict_items[key](date_dict)
summary_dict.setdefault(key, []).append(val)
Is there a better way to achieve this?