def clean_target_dict(data):
    """
    There are many unimplemented fields throughout parselmouth,
    for example some types of targeting are not yet implemented.
    In these cases, we must clean the dictionary before we pass
    it back to DFP. Fields like X.Type exist in many places, and these
    must be replaced with 'xsi_type' for exaample. Also, unneeded fields
    sometimes raise errors when passed back to DFP.

    @param data: dict
    @return: dict
    """
    if not isinstance(data, dict):
        return data

    # Track whether this is at bottom level of a nested dictionary
    at_bottom = True
    cln_dict = {}
    for _key, _val in data.items():
        if isinstance(_val, dict):
            at_bottom = False
            cln_dict[_key] = clean_target_dict(_val)
        elif isinstance(_val, list):
            at_bottom = False
            cln_dict[_key] = [
                clean_target_dict(d) for d in _val
            ]
        elif _key.find('_Type') >= 0:
            cln_dict['xsi_type'] = _val
        else:
            cln_dict[_key] = _val

    if at_bottom:
        # If you are at lowest level dictionary, only include
        # id and type fields
        clean_dict = {}
        for _key, _val in cln_dict.items():
            if _key.find('id') >= 0 or _key.find('Id') >= 0:
                clean_dict[_key] = _val
            elif _key == 'xsi_type':
                clean_dict[_key] = _val
    else:
        clean_dict = cln_dict

    return clean_dict
dataset/ETHPy150Open chartbeat-labs/parselmouth/parselmouth/adapters/dfp/targeting_utils.py/clean_target_dict/original