import copy


def safe_deepcopy_dict(d):
    """Util for safe deepcopy of a dict.

    Solves the issue with 'TypeError: can't pickle dict_items objects'
    of the default 'copy.deepcopy'.
    """
    try:
        new_d = copy.deepcopy(d)
    except TypeError:
        new_d = dict()
        for k, v in d.items():
            new_d[k] = copy.deepcopy(list(v.items()))
    return new_d