def main(client, ad_group_id):
  ad_group_ad_service = client.GetService('AdGroupAdService', 'v201509')

  # Create text ad.
  text_ad_operation = {
      'operator': 'ADD',
      'operand': {
          'adGroupId': ad_group_id,
          'ad': {
              # The 'xsi_type' field allows you to specify the xsi:type of the
              # object being created. It's only necessary when you must provide
              # an explicit type that the client library can't infer.
              'xsi_type': 'TextAd',
              'headline': 'Mars Cruise!!!',
              'description1': 'Visit the Red Planet in style.',
              'description2': 'Low-gravity fun for everyone!',
              'finalUrls': ['http://www.example.com'],
              'displayUrl': 'www.example.com',
          }
      }
  }

  operations = [text_ad_operation]

  # Validate the ad.
  try:
    # Enable "validate only" to check for errors.
    client.validate_only = True
    ad_group_ad_service.mutate(operations)
    print 'Validation successful, no errors returned.'
  except suds.WebFault, e:
    for error in e.fault.detail.ApiExceptionFault.errors:
      if error['ApiError.Type'] == 'PolicyViolationError':
        operation_index = re.findall(r'operations\[(.*)\]\.',
                                     error['fieldPath'])
        if operation_index:
          operation = operations[int(operation_index[0])]
          print ('Ad with headline \'%s\' violated %s policy \'%s\'.' %
                 (operation['operand']['ad']['headline'],
                  'exemptable' if error['isExemptable'] else 'non-exemptable',
                  error['externalPolicyName']))
          if error['isExemptable'].lower() == 'true':
            # Add exemption request to the operation.
            print ('Adding exemption request for policy name \'%s\' on text '
                   '\'%s\'.' %
                   (error['key']['policyName'], error['key']['violatingText']))
            if operation not in 'exemptionRequests':
              operation['exemptionRequests'] = []
            operation['exemptionRequests'].append({
                'key': error['key']
            })
        else:
          # Remove non-exemptable operation
          print 'Removing the operation from the request.'
          operations.delete(operation)
      else:
        # Non-policy error returned, re-throw exception.
        raise e

  # Add these ads. Disable "validate only" so the ads will get created.
  client.validate_only = False
  if operations:
    response = ad_group_ad_service.mutate(operations)
    if response and response['value']:
      ads = response['value']
      print 'Added %s ad(s) to ad group %s.' % (len(ads), ad_group_id)
      for ad in ads:
        print ('  Ad id is %s, type is %s and status is \'%s\'.' %
               (ad['ad']['id'], ad['ad']['Ad.Type'], ad['status']))
    else:
      print 'No ads were added.'
dataset/ETHPy150Open googleads/googleads-python-lib/examples/adwords/v201509/error_handling/handle_policy_violation_error.py/main/swapped operands of `not in`