Skip to content

Commit af717cc

Browse files
authored
fixed misplaced try block (googleads#232)
1 parent 67843db commit af717cc

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

examples/advanced_operations/find_and_remove_criteria_from_shared_set.py

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright 2018 Google LLC
2+
# Copyright 2020 Google LLC
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -18,7 +18,8 @@
1818
import argparse
1919
import sys
2020

21-
import google.ads.google_ads.client
21+
from google.ads.google_ads.client import GoogleAdsClient
22+
from google.ads.google_ads.errors import GoogleAdsException
2223

2324

2425
_DEFAULT_PAGE_SIZE = 1000
@@ -32,65 +33,76 @@ def main(client, customer_id, page_size, campaign_id):
3233
# First, retrieve all shared sets associated with the campaign.
3334
shared_sets_query = (
3435
'SELECT shared_set.id, shared_set.name FROM campaign_shared_set '
35-
'WHERE campaign.id = %s' % campaign_id)
36+
f'WHERE campaign.id = {campaign_id}')
3637

3738
try:
3839
shared_set_response = ga_service.search(
3940
customer_id, query=shared_sets_query, page_size=page_size)
40-
except google.ads.google_ads.errors.GoogleAdsException as ex:
41-
print('Request with ID "%s" failed with status "%s" and includes the '
42-
'following errors:' % (ex.request_id, ex.error.code().name))
41+
42+
shared_set_ids = []
43+
for row in shared_set_response:
44+
shared_set = row.shared_set
45+
shared_set_id = str(shared_set.id.value)
46+
shared_set_ids.append(shared_set_id)
47+
print(f'Campaign shared set ID "{shared_set_id}" and name '
48+
f'"{shared_set.name.value}" was found.')
49+
except GoogleAdsException as ex:
50+
print(f'Request with ID "{ex.request_id}" failed with status '
51+
f'"{ex.error.code().name}" and includes the following errors:')
4352
for error in ex.failure.errors:
44-
print('\tError with message "%s".' % error.message)
53+
print(f'\tError with message "{error.message}".')
4554
if error.location:
4655
for field_path_element in error.location.field_path_elements:
47-
print('\t\tOn field: %s' % field_path_element.field_name)
56+
print(f'\t\tOn field: {field_path_element.field_name}')
4857
sys.exit(1)
4958

50-
shared_set_ids = []
51-
for row in shared_set_response:
52-
shared_set = row.shared_set
53-
shared_set_id = str(shared_set.id.value)
54-
shared_set_ids.append(shared_set_id)
55-
print('Campaign shared set ID "%s" and name "%s" was found.'
56-
% (shared_set_id, shared_set.name.value))
57-
5859
# Next, retrieve shared criteria for all found shared sets.
5960
shared_criteria_query = (
6061
'SELECT shared_criterion.type, shared_criterion.keyword.text, '
6162
'shared_criterion.keyword.match_type, shared_set.id '
62-
'FROM shared_criterion WHERE shared_set.id IN (%s)'
63-
% ', '.join(shared_set_ids))
63+
'FROM shared_criterion WHERE shared_set.id IN '
64+
f'({", ".join(shared_set_ids)})')
6465

6566
try:
6667
shared_criteria_response = ga_service.search(
6768
customer_id, query=shared_criteria_query, page_size=page_size)
68-
except google.ads.google_ads.errors.GoogleAdsException as ex:
69-
print('Request with ID "%s" failed with status "%s" and includes the '
70-
'following errors:' % (ex.request_id, ex.error.code().name))
69+
except GoogleAdsException as ex:
70+
print(f'Request with ID "{ex.request_id}" failed with status '
71+
f'"{ex.error.code().name}" and includes the following errors:')
7172
for error in ex.failure.errors:
72-
print('\tError with message "%s".' % error.message)
73+
print(f'\tError with message "{error.message}".')
7374
if error.location:
7475
for field_path_element in error.location.field_path_elements:
75-
print('\t\tOn field: %s' % field_path_element.field_name)
76+
print(f'\t\tOn field: {field_path_element.field_name}')
7677
sys.exit(1)
7778

7879
# Use the enum type to determine the enum name from the value.
7980
keyword_match_type_enum = (
8081
client.get_type('KeywordMatchTypeEnum', version='v2').KeywordMatchType)
8182

82-
criterion_ids = []
83-
for row in shared_criteria_response:
84-
shared_criterion = row.shared_criterion
85-
shared_criterion_resource_name = shared_criterion.resource_name
86-
if (shared_criterion.type ==
87-
client.get_type('CriterionTypeEnum', version='v2').KEYWORD):
88-
keyword = shared_criterion.keyword
89-
print('Shared criterion with resource name "%s" for negative '
90-
'keyword with text "%s" and match type "%s" was found.'
91-
% (shared_criterion_resource_name, keyword.text.value,
92-
keyword_match_type_enum.Name(keyword.match_type)))
93-
criterion_ids.append(shared_criterion_resource_name)
83+
try:
84+
criterion_ids = []
85+
for row in shared_criteria_response:
86+
shared_criterion = row.shared_criterion
87+
shared_criterion_resource_name = shared_criterion.resource_name
88+
if (shared_criterion.type == client.get_type(
89+
'CriterionTypeEnum', version='v2').KEYWORD):
90+
keyword = shared_criterion.keyword
91+
print('Shared criterion with resource name '
92+
f'"{shared_criterion_resource_name}" for negative '
93+
f'keyword with text "{keyword.text.value}" and match type'
94+
f' "{keyword_match_type_enum.Name(keyword.match_type)}" '
95+
'was found.')
96+
criterion_ids.append(shared_criterion_resource_name)
97+
except GoogleAdsException as ex:
98+
print(f'Request with ID "{ex.request_id}" failed with status '
99+
f'"{ex.error.code().name}" and includes the following errors:')
100+
for error in ex.failure.errors:
101+
print(f'\tError with message "{error.message}".')
102+
if error.location:
103+
for field_path_element in error.location.field_path_elements:
104+
print(f'\t\tOn field: {field_path_element.field_name}')
105+
sys.exit(1)
94106

95107
operations = []
96108

@@ -104,25 +116,24 @@ def main(client, customer_id, page_size, campaign_id):
104116
try:
105117
response = shared_criterion_service.mutate_shared_criteria(
106118
customer_id, operations)
107-
except google.ads.google_ads.errors.GoogleAdsException as ex:
108-
print('Request with ID "%s" failed with status "%s" and includes the '
109-
'following errors:' % (ex.request_id, ex.error.code().name))
119+
except GoogleAdsException as ex:
120+
print(f'Request with ID "{ex.request_id}" failed with status '
121+
f'"{ex.error.code().name}" and includes the following errors:')
110122
for error in ex.failure.errors:
111-
print('\tError with message "%s".' % error.message)
123+
print(f'\tError with message "{error.message}".')
112124
if error.location:
113125
for field_path_element in error.location.field_path_elements:
114-
print('\t\tOn field: %s' % field_path_element.field_name)
126+
print(f'\t\tOn field: {field_path_element.field_name}')
115127
sys.exit(1)
116128

117129
for result in response.results:
118-
print('Removed shared criterion "%s".' % result.resource_name)
130+
print(f'Removed shared criterion "{result.resource_name}".')
119131

120132

121133
if __name__ == '__main__':
122134
# GoogleAdsClient will read the google-ads.yaml configuration file in the
123135
# home directory if none is specified.
124-
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
125-
.load_from_storage())
136+
google_ads_client = GoogleAdsClient.load_from_storage()
126137

127138
parser = argparse.ArgumentParser(
128139
description=('Finds shared sets, then finds and removes shared set '

0 commit comments

Comments
 (0)