Skip to content

Commit b097bac

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
This change adds Python type hints and annotations to the
scripts in the examples/travel directory. Type hints were added to function arguments, return values, and variables where appropriate to improve code readability and maintainability. The `typing.Any` type was used for Google Ads API objects where specific types were not readily available. All changes are compatible with Python 3.8+.
1 parent 2d36b74 commit b097bac

File tree

5 files changed

+397
-295
lines changed

5 files changed

+397
-295
lines changed

examples/travel/add_hotel_ad.py

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,72 @@
2424
import argparse
2525
import sys
2626
import uuid
27+
from typing import Any
2728

2829
from google.ads.googleads.client import GoogleAdsClient
2930
from google.ads.googleads.errors import GoogleAdsException
3031

3132

3233
def main(
33-
client, customer_id, hotel_center_account_id, cpc_bid_ceiling_micro_amount
34-
):
35-
budget_resource_name = add_budget(client, customer_id)
36-
37-
campaign_resource_name = add_hotel_campaign(
34+
client: GoogleAdsClient,
35+
customer_id: str,
36+
hotel_center_account_id: int,
37+
cpc_bid_ceiling_micro_amount: int,
38+
) -> None:
39+
budget_resource_name: str = add_budget(client, customer_id)
40+
41+
campaign_resource_name: str = add_hotel_campaign(
3842
client,
3943
customer_id,
4044
budget_resource_name,
4145
hotel_center_account_id,
4246
cpc_bid_ceiling_micro_amount,
4347
)
4448

45-
ad_group_resource_name = add_hotel_ad_group(
49+
ad_group_resource_name: str = add_hotel_ad_group(
4650
client, customer_id, campaign_resource_name
4751
)
4852

4953
add_hotel_ad(client, customer_id, ad_group_resource_name)
5054

5155

52-
def add_budget(client, customer_id):
53-
campaign_budget_service = client.get_service("CampaignBudgetService")
56+
def add_budget(client: GoogleAdsClient, customer_id: str) -> str:
57+
campaign_budget_service: Any = client.get_service("CampaignBudgetService")
5458

5559
# Create a budget, which can be shared by multiple campaigns.
56-
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
57-
campaign_budget = campaign_budget_operation.create
60+
campaign_budget_operation: Any = client.get_type("CampaignBudgetOperation")
61+
campaign_budget: Any = campaign_budget_operation.create
5862
campaign_budget.name = f"Interplanetary Budget {uuid.uuid4()}"
5963
campaign_budget.delivery_method = (
6064
client.enums.BudgetDeliveryMethodEnum.STANDARD
6165
)
6266
campaign_budget.amount_micros = 500000
6367

6468
# Add budget.
65-
campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
66-
customer_id=customer_id, operations=[campaign_budget_operation]
69+
campaign_budget_response: Any = (
70+
campaign_budget_service.mutate_campaign_budgets(
71+
customer_id=customer_id, operations=[campaign_budget_operation]
72+
)
6773
)
6874

69-
budget_resource_name = campaign_budget_response.results[0].resource_name
75+
budget_resource_name: str = campaign_budget_response.results[
76+
0
77+
].resource_name
7078

7179
print(f"Created budget with resource name '{budget_resource_name}'.")
7280

7381
return budget_resource_name
7482

7583

7684
# [START add_hotel_ad_3]
77-
def add_hotel_ad(client, customer_id, ad_group_resource_name):
78-
ad_group_ad_service = client.get_service("AdGroupAdService")
85+
def add_hotel_ad(
86+
client: GoogleAdsClient, customer_id: str, ad_group_resource_name: str
87+
) -> str:
88+
ad_group_ad_service: Any = client.get_service("AdGroupAdService")
7989

8090
# Creates a new ad group ad and sets the hotel ad to it.
81-
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
82-
ad_group_ad = ad_group_ad_operation.create
91+
ad_group_ad_operation: Any = client.get_type("AdGroupAdOperation")
92+
ad_group_ad: Any = ad_group_ad_operation.create
8393
ad_group_ad.ad_group = ad_group_resource_name
8494
# Set the ad group ad to enabled. Setting this to paused will cause an error
8595
# for hotel campaigns. For hotels pausing should happen at either the ad group or
@@ -88,11 +98,13 @@ def add_hotel_ad(client, customer_id, ad_group_resource_name):
8898
client.copy_from(ad_group_ad.ad.hotel_ad, client.get_type("HotelAdInfo"))
8999

90100
# Add the ad group ad.
91-
ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
101+
ad_group_ad_response: Any = ad_group_ad_service.mutate_ad_group_ads(
92102
customer_id=customer_id, operations=[ad_group_ad_operation]
93103
)
94104

95-
ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name
105+
ad_group_ad_resource_name: str = ad_group_ad_response.results[
106+
0
107+
].resource_name
96108

97109
print(f"Created hotel ad with resource name '{ad_group_ad_resource_name}'.")
98110

@@ -101,12 +113,14 @@ def add_hotel_ad(client, customer_id, ad_group_resource_name):
101113

102114

103115
# [START add_hotel_ad_2]
104-
def add_hotel_ad_group(client, customer_id, campaign_resource_name):
105-
ad_group_service = client.get_service("AdGroupService")
116+
def add_hotel_ad_group(
117+
client: GoogleAdsClient, customer_id: str, campaign_resource_name: str
118+
) -> str:
119+
ad_group_service: Any = client.get_service("AdGroupService")
106120

107121
# Create ad group.
108-
ad_group_operation = client.get_type("AdGroupOperation")
109-
ad_group = ad_group_operation.create
122+
ad_group_operation: Any = client.get_type("AdGroupOperation")
123+
ad_group: Any = ad_group_operation.create
110124
ad_group.name = f"Earth to Mars cruise {uuid.uuid4()}"
111125
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
112126
ad_group.campaign = campaign_resource_name
@@ -115,11 +129,11 @@ def add_hotel_ad_group(client, customer_id, campaign_resource_name):
115129
ad_group.cpc_bid_micros = 10000000
116130

117131
# Add the ad group.
118-
ad_group_response = ad_group_service.mutate_ad_groups(
132+
ad_group_response: Any = ad_group_service.mutate_ad_groups(
119133
customer_id=customer_id, operations=[ad_group_operation]
120134
)
121135

122-
ad_group_resource_name = ad_group_response.results[0].resource_name
136+
ad_group_resource_name: str = ad_group_response.results[0].resource_name
123137

124138
print(
125139
"Added a hotel ad group with resource name '{ad_group_resource_name}'."
@@ -131,18 +145,18 @@ def add_hotel_ad_group(client, customer_id, campaign_resource_name):
131145

132146
# [START add_hotel_ad]
133147
def add_hotel_campaign(
134-
client,
135-
customer_id,
136-
budget_resource_name,
137-
hotel_center_account_id,
138-
cpc_bid_ceiling_micro_amount,
139-
):
140-
campaign_service = client.get_service("CampaignService")
148+
client: GoogleAdsClient,
149+
customer_id: str,
150+
budget_resource_name: str,
151+
hotel_center_account_id: int,
152+
cpc_bid_ceiling_micro_amount: int,
153+
) -> str:
154+
campaign_service: Any = client.get_service("CampaignService")
141155

142156
# [START add_hotel_ad_1]
143157
# Create campaign.
144-
campaign_operation = client.get_type("CampaignOperation")
145-
campaign = campaign_operation.create
158+
campaign_operation: Any = client.get_type("CampaignOperation")
159+
campaign: Any = campaign_operation.create
146160
campaign.name = f"Interplanetary Cruise Campaign {uuid.uuid4()}"
147161

148162
# Configures settings related to hotel campaigns including advertising
@@ -170,11 +184,11 @@ def add_hotel_campaign(
170184
# [END add_hotel_ad_1]
171185

172186
# Add the campaign.
173-
campaign_response = campaign_service.mutate_campaigns(
187+
campaign_response: Any = campaign_service.mutate_campaigns(
174188
customer_id=customer_id, operations=[campaign_operation]
175189
)
176190

177-
campaign_resource_name = campaign_response.results[0].resource_name
191+
campaign_resource_name: str = campaign_response.results[0].resource_name
178192

179193
print(
180194
"Added a hotel campaign with resource name '{campaign_resource_name}'."
@@ -185,7 +199,7 @@ def add_hotel_campaign(
185199

186200

187201
if __name__ == "__main__":
188-
parser = argparse.ArgumentParser(
202+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
189203
description=(
190204
"Adds an expanded text ad to the specified ad group ID, "
191205
"for the given customer ID."
@@ -213,11 +227,13 @@ def add_hotel_campaign(
213227
required=True,
214228
help="The hotel center account ID.",
215229
)
216-
args = parser.parse_args()
230+
args: argparse.Namespace = parser.parse_args()
217231

218232
# GoogleAdsClient will read the google-ads.yaml configuration file in the
219233
# home directory if none is specified.
220-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
234+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
235+
version="v20"
236+
)
221237

222238
try:
223239
main(

examples/travel/add_hotel_ad_group_bid_modifiers.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020

2121
import argparse
2222
import sys
23+
from typing import Any
2324

2425
from google.ads.googleads.client import GoogleAdsClient
2526
from google.ads.googleads.errors import GoogleAdsException
2627

2728

2829
# [START add_hotel_ad_group_bid_modifiers]
29-
def main(client, customer_id, ad_group_id):
30-
ad_group_service = client.get_service("AdGroupService")
31-
ag_bm_service = client.get_service("AdGroupBidModifierService")
30+
def main(client: GoogleAdsClient, customer_id: str, ad_group_id: str) -> None:
31+
ad_group_service: Any = client.get_service("AdGroupService")
32+
ag_bm_service: Any = client.get_service("AdGroupBidModifierService")
3233

3334
# Create ad group bid modifier based on hotel check-in day.
34-
check_in_ag_bm_operation = client.get_type("AdGroupBidModifierOperation")
35-
check_in_ag_bid_modifier = check_in_ag_bm_operation.create
35+
check_in_ag_bm_operation: Any = client.get_type(
36+
"AdGroupBidModifierOperation"
37+
)
38+
check_in_ag_bid_modifier: Any = check_in_ag_bm_operation.create
3639
check_in_ag_bid_modifier.hotel_check_in_day.day_of_week = (
3740
client.enums.DayOfWeekEnum.MONDAY
3841
)
@@ -43,20 +46,22 @@ def main(client, customer_id, ad_group_id):
4346
check_in_ag_bid_modifier.bid_modifier = 1.5
4447

4548
# Create ad group bid modifier based on hotel length of stay info.
46-
los_ag_bm_operation = client.get_type("AdGroupBidModifierOperation")
47-
los_ag_bid_modifier = los_ag_bm_operation.create
49+
los_ag_bm_operation: Any = client.get_type("AdGroupBidModifierOperation")
50+
los_ag_bid_modifier: Any = los_ag_bm_operation.create
4851
los_ag_bid_modifier.ad_group = ad_group_service.ad_group_path(
4952
customer_id, ad_group_id
5053
)
5154
# Creates the hotel length of stay info.
52-
hotel_length_of_stay_info = los_ag_bid_modifier.hotel_length_of_stay
55+
hotel_length_of_stay_info: Any = (
56+
los_ag_bid_modifier.hotel_length_of_stay
57+
)
5358
hotel_length_of_stay_info.min_nights = 3
5459
hotel_length_of_stay_info.max_nights = 7
5560
# Sets the bid modifier value to 170%.
5661
los_ag_bid_modifier.bid_modifier = 1.7
5762

5863
# Add the bid modifiers
59-
ag_bm_response = ag_bm_service.mutate_ad_group_bid_modifiers(
64+
ag_bm_response: Any = ag_bm_service.mutate_ad_group_bid_modifiers(
6065
customer_id=customer_id,
6166
operations=[check_in_ag_bm_operation, los_ag_bm_operation],
6267
)
@@ -70,7 +75,7 @@ def main(client, customer_id, ad_group_id):
7075

7176

7277
if __name__ == "__main__":
73-
parser = argparse.ArgumentParser(
78+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
7479
description=("Adds an ad group bid modifier to a hotel ad group.")
7580
)
7681
# The following argument(s) should be provided to run the example.
@@ -88,11 +93,13 @@ def main(client, customer_id, ad_group_id):
8893
required=True,
8994
help="The ad group ID of the hotel ad group.",
9095
)
91-
args = parser.parse_args()
96+
args: argparse.Namespace = parser.parse_args()
9297

9398
# GoogleAdsClient will read the google-ads.yaml configuration file in the
9499
# home directory if none is specified.
95-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
100+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
101+
version="v20"
102+
)
96103

97104
try:
98105
main(googleads_client, args.customer_id, args.ad_group_id)

0 commit comments

Comments
 (0)