def _custom_target_list_to_child_list(custom_targets, is_operator):
    """
    Convert a list of custom targets to a list of dictionaries
    formatted for use in DFP

    @param custom_targets: list(Custom)
    @param is_operator: str, 'IS' or 'IS_NOT'
    @return: list(dict)
    """
    _children = defaultdict(lambda: defaultdict(list))
    for value in custom_targets:
        _children[value.parent_id][is_operator].append(value)

    dfp_children = []
    for _parent_id, _operations in _children.iteritems():
        for _operator, _values in _operations.iteritems():
            _value_target_ids = []
            _node_type = None
            _node_value_type = None

            for _custom_value in _values:
                _this_node_type = _custom_value.node_key
                _this_node_value_type = _custom_value.id_key

                if not _node_type and not _node_value_type:
                    _node_type = _this_node_type
                    _node_value_type = _this_node_value_type

                if (_this_node_type != _custom_value.node_key or
                        _this_node_value_type != _custom_value.id_key):
                    raise ParselmouthException(
                        "Could not serialize Custom Target: %s" % _custom_value
                    )
                _value_target_ids.append(_custom_value.id)

            _doc = {
                'operator': _operator,
                'xsi_type': _node_type,
                _node_value_type: _value_target_ids,
            }
            if _parent_id:
                _doc['keyId'] = _parent_id

            dfp_children.append(_doc)

    return dfp_children
dataset/ETHPy150Open chartbeat-labs/parselmouth/parselmouth/adapters/dfp/targeting_utils.py/_custom_target_list_to_child_list/original