def transform_inventory_targeting_to_dfp(targeting):
    targeted_placements = []
    targeted_adunits = []
    excluded_adunits = []

    includes, excludes = targeting.get_includes_and_excludes()

    for i in includes:
        if isinstance(i, AdUnit):
            targeted_adunits.append(_adunit_to_dfp(i))
        elif isinstance(i, Placement):
            targeted_placements.append(i.id)

    for e in excludes:
        if isinstance(e, AdUnit):
            excluded_adunits.append(_adunit_to_dfp(e))

    target_dict = {}
    if targeted_placements:
        target_dict['targetedPlacementIds'] = targeted_placements

    if targeted_adunits:
        target_dict['targetedAdUnits'] = targeted_adunits

    if excluded_adunits:
        target_dict['excludedAdUnits'] = excluded_adunits

    return target_dict
Convert inventory targeting model to dfp dictionary
dataset/ETHPy150Open chartbeat-labs/parselmouth/parselmouth/adapters/dfp/targeting_utils.py/transform_inventory_targeting_to_dfp