def _recursive_custom_to_dfp(targeting):
    """
    Convert custom targeting model to dfp dictionary

    @param targeting: TargetingCriterion
    @return: dict
    """
    operator, targets = targeting.get_data()

    if operator == TargetingCriterion.OPERATOR.NOT:
        # In this case targets is of the form [TargetingCriterion]
        # NOTE: NOT operators are always a list of size 1
        # In this case we redefine operator and targets
        operator, targets = targets[0].get_data()
        is_operator = 'IS_NOT'
    else:
        is_operator = 'IS'

    if isinstance(targets[0], Custom):
        # In this case, we are in the deepest level of the targeting
        # targets is of type list(Custom)
        children = _custom_target_list_to_child_list(targets, is_operator)
    elif len(targets) == 1:
        # targets is a list of exactly one TargetingCriterion
        # To avoid redundant nesting, we return the output of this
        # individual result
        return _recursive_custom_to_dfp(targets[0])
    else:
        # In this case there are further depths to the targeting
        # targets is of type list(TargetingCriterion)
        children = [_recursive_custom_to_dfp(c) for c in targets]

    return {
        'xsi_type': 'CustomCriteriaSet',
        'children': children,
        'logicalOperator': operator
    }
dataset/ETHPy150Open chartbeat-labs/parselmouth/parselmouth/adapters/dfp/targeting_utils.py/_recursive_custom_to_dfp/original