Protobuf 消息

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

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

Proto+ 消息与 protobuf 消息

在版本 10.0.0 中,Python 客户端库迁移到了新的代码生成器流水线,该流水线集成了 proto-plus 作为 protobuf 消息接口的人体工学设计,方法是使它们的行为更像原生 Python 对象。这种改进的代价是 proto-plus 会带来性能开销。

Proto+ 性能

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

在 proto+ 消息实例上访问字段时,特别是在读取或设置字段时,例如在 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+ 消息用例
相较于 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+ 消息时,请将其转换为 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+ 消息还是 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 读取对象时,您可能想要知道 int 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 生成器模块中构建。也可以通过编程方式访问。

首先,安装模块:

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 版时遇到任何问题,请在我们的跟踪器上提交问题