def to_dicts(collection, key=None, **kw):
    _dicts = []
    try:
        for each in collection:
            try:
                each_dict = each.to_dict(**kw)
                if key:
                    each_dict = key(each_dict)
                _dicts.append(each_dict)
            except __HOLE__:
                _dicts.append(each)
    except TypeError:
        return collection

    return _dicts
dataset/ETHPy150Open ramses-tech/nefertari/nefertari/utils/data.py/to_dicts