Protobuf 消息

Python 客户端库的 14.0.0 版本引入了一个名为 use_proto_plus 的新必需配置参数,用于指定您希望该库返回 proto-plus 消息还是 protobuf 消息。如需详细了解如何设置此参数,请参阅配置文档

本部分介绍选择要使用的消息类型对性能的影响,因此,我们建议您阅读并了解相关选项,以便做出明智的决策。不过,如果您想在不更改代码的情况下升级到版本 14.0.0,可以将 use_proto_plus 设置为 True 以避免破坏接口更改。

Proto-plus 消息与 protobuf 消息

在版本 10.0.0 中,Python 客户端库迁移到新的代码生成器流水线,该流水线集成了 proto-plus,旨在通过让 protobuf 消息接口的行为更像原生 Python 对象来改进 protobuf 消息接口的工效学设计。这种改进的代价是 proto-plus 会产生性能开销。

Proto+ 性能

proto-plus 的核心优势之一是,它通过一种名为类型编组的过程将 protobuf 消息知名类型转换为原生 Python 类型。

在 proto-plus 消息实例上访问字段时会进行编组,尤其是在读取或设置字段时(例如在 protobuf 定义中):

syntax = "proto3";

message Dog {
  string name = 1;
}

此定义转换为 proto-plus 类时,将如下所示:

import proto

class Dog(proto.Message):
    name = proto.Field(proto.STRING, number=1)

然后,您可以初始化 Dog 类并访问其 name 字段,就像处理任何其他 Python 对象一样:

dog = Dog()
dog.name = "Scruffy"
print(dog.name)

读取和设置 name 字段时,该值将从原生 Python str 类型转换为 string 类型,以使该值与 protobuf 运行时兼容。

自版本 10.0.0 发布以来,我们进行分析时确定,执行这些类型转换所用的时间会对性能产生足够大的影响,因此有必要为用户提供使用 protobuf 消息的选项。

proto-plus 和 protobuf 消息的用例

Proto-plus 信息应用场景
与 protobuf 消息相比,Proto-plus 提供了许多人体工程学改进,因此非常适合用于编写可维护、可读的代码。由于它们公开了原生 Python 对象,因此更易于使用和理解。
Protobuf 消息用例
对性能要求较高的用例使用 protobuf,特别是在需要快速处理大型报告或使用大量操作构建 mutate 请求的应用(例如使用 BatchJobServiceOfflineUserDataJobService)中。

动态更改消息类型

为应用选择适当的消息类型后,您可能会发现特定工作流需要使用其他类型。在这种情况下,您可以使用客户端库提供的实用程序轻松地在两种类型之间进行动态切换。使用与上文相同的 Dog 消息类:

from google.ads.googleads import util

# Proto-plus message type
dog = Dog()

# Protobuf message type
dog = util.convert_proto_plus_to_protobuf(dog)

# Back to proto-plus message type
dog = util.convert_protobuf_to_proto_plus(dog)

Protobuf 消息接口差异

proto-plus 接口有详细说明,但在这里我们将重点介绍一些影响 Google Ads 客户端库常见用例的关键区别。

字节序列化

Proto+ 消息
serialized = type(campaign).serialize(campaign)
deserialized = type(campaign).deserialize(serialized)
Protobuf 消息
serialized = campaign.SerializeToString()
deserialized = campaign.FromString(serialized)

JSON 序列化

Proto+ 消息
serialized = type(campaign).to_json(campaign)
deserialized = type(campaign).from_json(serialized)
Protobuf 消息
from google.protobuf.json_format import MessageToJson, Parse

serialized = MessageToJson(campaign)
deserialized = Parse(serialized, campaign)

字段掩码

api-core 提供的字段掩码辅助方法旨在使用 protobuf 消息实例。因此,在使用 proto-plus 消息时,请将其转换为 protobuf 消息以利用帮助程序:

Proto+ 消息
from google.api_core.protobuf_helpers import field_mask

campaign = client.get_type("Campaign")
protobuf_campaign = util.convert_proto_plus_to_protobuf(campaign)
mask = field_mask(None, protobuf_campaign)
Protobuf 消息
from google.api_core.protobuf_helpers import field_mask

campaign = client.get_type("Campaign")
mask = field_mask(None, campaign)

枚举

proto-plus 消息公开的枚举是 Python 的原生 enum 类型的实例,因此继承了许多便捷方法。

枚举类型检索

使用 GoogleAdsClient.get_type 方法检索枚举时,返回的消息略有不同,具体取决于您使用的是 proto-plus 还是 protobuf 消息。例如:

Proto+ 消息
val = client.get_type("CampaignStatusEnum").CampaignStatus.PAUSED
Protobuf 消息
val = client.get_type("CampaignStatusEnum").PAUSED

为了简化枚举的检索,GoogleAdsClient 实例上有一个便捷属性,无论您使用哪种消息类型,该属性都拥有一致的接口:

val = client.enums.CampaignStatusEnum.PAUSED

枚举值检索

有时,知道给定枚举的值或字段 ID 很有用,例如,CampaignStatusEnum 上的 PAUSED 对应于 3

Proto+ 消息
campaign = client.get_type("Campaign")
campaign.status = client.enums.CampaignStatusEnum.PAUSED
# To read the value of campaign status
print(campaign.status.value)
Protobuf 消息
campaign = client.get_type("Campaign")
status_enum = client.enums.CampaignStatusEnum
campaign.status = status_enum.PAUSED
# To read the value of campaign status
print(status_enum.CampaignStatus.Value(campaign.status))

枚举名称检索

有时,了解枚举字段的名称会很有用。例如,从 API 读取对象时,您可能想知道整数 3 对应于哪个广告系列状态:

Proto+ 消息
campaign = client.get_type("Campaign")
campaign.status = client.enums.CampaignStatusEnum.PAUSED
# To read the name of campaign status
print(campaign.status.name)
Protobuf 消息
campaign = client.get_type("Campaign")
status_enum = client.enums.CampaignStatusEnum
# Sets the campaign status to the int value for PAUSED
campaign.status = status_enum.PAUSED
# To read the name of campaign status
status_enum.CampaignStatus.Name(campaign.status)

重复字段

proto-plus 文档中所述,重复字段通常等同于类型化列表,也就是说,它们在行为上与 list 几乎完全相同。

附加到重复的标量字段

向重复的标量类型字段(例如 stringint64 字段)添加值时,无论消息类型如何,接口都是相同的:

Proto+ 消息
ad.final_urls.append("https://www.example.com")
Protobuf 消息
ad.final_urls.append("https://www.example.com")

这也包括所有其他常见的 list 方法,例如 extend

Proto+ 消息
ad.final_urls.extend(["https://www.example.com", "https://www.example.com/2"])
Protobuf 消息
ad.final_urls.extend(["https://www.example.com", "https://www.example.com/2"])

将消息类型附加到重复字段

如果重复字段不是标量类型,则将它们添加到重复字段时的行为会略有不同:

Proto+ 消息
frequency_cap = client.get_type("FrequencyCapEntry")
frequency_cap.cap = 100
campaign.frequency_caps.append(frequency_cap)
Protobuf 消息
# The add method initializes a message and adds it to the repeated field
frequency_cap = campaign.frequency_caps.add()
frequency_cap.cap = 100

分配重复字段

对于标量和非标量重复字段,您可以通过不同的方式为字段分配列表:

Proto+ 消息
# In proto-plus it's possible to use assignment.
urls = ["https://www.example.com"]
ad.final_urls = urls
Protobuf 消息
# Protobuf messages do not allow assignment, but you can replace the
# existing list using slice syntax.
urls = ["https://www.example.com"]
ad.final_urls[:] = urls

空白消息

有时,了解消息实例是否包含任何信息或是否设置了任何字段十分有用。

Proto+ 消息
# When using proto-plus messages you can simply check the message for
# truthiness.
is_empty = bool(campaign)
is_empty = not campaign
Protobuf 消息
is_empty = campaign.ByteSize() == 0

消息文案

对于 proto-plus 和 protobuf 消息,我们建议对 GoogleAdsClient 使用 copy_from 辅助方法:

client.copy_from(campaign, other_campaign)

消息字段为空

无论您使用何种消息类型,设置空白消息字段的过程都是相同的。您只需将一条空白消息复制到相关字段中即可。请参阅邮件复制部分以及空白消息字段指南。以下示例展示了如何设置空白消息字段:

client.copy_from(campaign.manual_cpm, client.get_type("ManualCpm"))

采用保留字的字段名称

使用 proto-plus 消息时,如果字段名称在 Python 中也是保留字,则字段名称自动在尾部带有下划线。以下是使用 Asset 实例的示例:

asset = client.get_type("Asset")
asset.type_ = client.enums.AssetTypeEnum.IMAGE

预留名称的完整列表gapic generator 模块中构建。您也可以通过编程方式访问该文件。

首先,安装模块:

python -m pip install gapic-generator

然后,在 Python REPL 或脚本中执行以下操作:

import gapic.utils
print(gapic.utils.reserved_names.RESERVED_NAMES)

字段存在

由于 protobuf 消息实例上的字段具有默认值,因此了解是否设置了某个字段并不总是很直观。

Proto+ 消息
# Use the "in" operator.
has_field = "name" in campaign
Protobuf 消息
campaign = client.get_type("Campaign")
# Determines whether "name" is set and not just an empty string.
campaign.HasField("name")

protobuf Message 类接口有一个 HasField 方法,用于确定消息中的字段是否已设置,即使该字段设置为默认值也是如此。

Protobuf 消息方法

protobuf 消息接口包含一些不属于 proto-plus 接口的便捷方法;但是,通过将 proto-plus 消息转换为其 protobuf 对应项,可以轻松访问这些方法:

# Accessing the ListFields method
protobuf_campaign = util.convert_protobuf_to_proto_plus(campaign)
print(campaign.ListFields())

# Accessing the Clear method
protobuf_campaign = util.convert_protobuf_to_proto_plus(campaign)
print(campaign.Clear())

问题跟踪器

如果您对这些更改有任何疑问,或者在迁移到该库的 14.0.0 版本时遇到任何问题,请在我们的跟踪器上提交问题