Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
描述元模組函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
descriptor.py
模組包含訊息定義與函式,用於將 Google Protocol RPC 定義轉換為可傳輸的訊息格式。
描述 Enum 執行個體、Enum 類別、Field 類別或 Message 類別後,即可產生適當的描述元物件。描述元物件是一個用來描述其他 Google RPC 定義 (例如 Enum、訊息以及服務) 的物件。您可以使用這項訊息,將資訊傳送給想要知道 Enum 值、Enum、欄位或訊息之描述的用戶端,而不需要下載原始碼。這種格式也與其他非 Python 的程式語言相容。
描述符的模擬方式與 https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto
的二進位相容
注意!descriptor.py
中定義的類型和欄位名稱不一定與 descriptor.proto
中定義的名稱相符。Google Protocol RPC 以這種方式設計,讓使用這些描述元的原始碼更便於閱讀。舉例來說,在 descriptor.proto
中,欄位描述元需要在前方加上 TYPE
,但在 descriptor.py
中則不需要。
下列程式碼範例示範如何使用描述元模組,使用 MessageDescriptor
類別描述名為 Pixel
的類別。
from protorpc import descriptor
from protorpc import messages
class Pixel(messages.Message):
x = messages.IntegerField(1, required=True)
y = messages.IntegerField(2, required=True)
color = messages.BytesField(3)
# Describe Pixel class using message descriptor.
fields = [
descriptor.FieldDescriptor(name='x',
number=1,
label=descriptor.FieldDescriptor.Label.REQUIRED,
variant=descriptor.FieldDescriptor.Variant.INT64),
descriptor.FieldDescriptor(name='y',
number=2,
label=descriptor.FieldDescriptor.Label.REQUIRED,
variant=descriptor.FieldDescriptor.Variant.INT64),
descriptor.FieldDescriptor(name='color',
number = 3,
label=descriptor.FieldDescriptor.Label.OPTIONAL,
variant=descriptor.FieldDescriptor.Variant.BYTES)]
message = descriptor.MessageDescriptor(name='Pixel',
fields=fields)
# Describing is the equivalent of building the above message.
message == descriptor.describe_message(Pixel)
protorpc.descriptor
套件提供下列函式:
- describe(value)
-
將任何值描述為描述元。您可以使用這個輔助程式函式來描述任何具有適當描述元物件的物件。
引數
- value
- 要描述為描述符的值。
如果該物件可描述為描述元,就傳回其描述元,否則傳回 None。
- describe_enum(enum_definition)
-
從 Enum 類別中建立描述元。
引數
- enum_value
- 要描述的列舉類別。
傳回已初始化的 EnumDescriptor 執行個體,描述 Enum 執行個體。
EnumDescriptor 類別的欄位包括:
name
- 指定的列舉名稱,不含任何限定條件。
values
- 由 Enum 類別定義的值。
- describe_enum_value(enum_value)
-
根據 Enumerator 例項建構描述元。引數
- enum_value
- 要說明的列舉值。
傳回已初始化的 EnumValueDescriptor 執行個體,描述 Enum 執行個體。
EnumValueDescriptor 類別的欄位包括:
name
- 列舉值的名稱。
number
- 列舉值的數字。
- describe_field(field_definition)
-
從 Field 執行個體中建立描述元。
引數
- field_definition
- 要描述的欄位例項。
傳回已初始化的 FieldDescriptor 執行個體,描述 Field 執行個體,並附上 Enum 和欄位的清單。
FieldDescriptor 類別的 Enum 包括:
Variant
- 指定欄位的線路格式提示子類型。
Label
- 選用、必填和重複欄位的值。
FieldDescriptor 類別的欄位包括:
name
- 指定欄位的名稱。
number
- 指定欄位的編號。
variant
- 指定欄位的變化版本。
type_name
- 輸入訊息和列舉欄位的名稱。
default_value
- 預設值的字串表示法。
- describe_file(module)
-
從所指定的 Python 模組中建立檔案。
引數
- module
- 要說明的 Python。
傳回已初始化的 FileDescriptor 執行個體,描述模組。
FileDescriptor 類別的欄位包括:
package
- 定義所屬套件的完整名稱。
message_types
- 檔案中包含的訊息定義。
enum_types
- 檔案中包含的列舉定義。
service_types
- 檔案中包含的服務定義。
- describe_file_set(modules)
-
從所指定的 Python 模組中建立檔案集。
引數
- 模組
- 要說明的 Python 模組可枚舉項目。
傳回已初始化的 FileSet 執行個體,描述模組。
FileSet 類別的欄位包括:
files
- 檔案集中的檔案。
- describe_message(message_definition)
-
從 Message 類別中建立描述元。
引數
- message_definition
- 要描述的 Message 類別。
傳回已初始化的 MessageDescriptor 執行個體,描述 Message 執行個體。
MessageDescriptor 類別的欄位包括:
name
- 定義所屬套件的完整名稱。
fields
- 為訊息定義的欄位。
message_types
- 在訊息上定義的巢狀訊息類別。
enum_types
- 在訊息中定義的巢狀列舉類別。
- describe_method(method)
-
從遠端服務方法中建立描述元。
引數
- method
- 要說明的遠端服務方法。
傳回已初始化的 MethodDescriptor 執行個體,描述遠端服務方法。
MethodDescriptor 的欄位包括:
name
- 服務方法的名稱。
request_type
- 要求訊息類型的完整名稱或相對名稱。
response_type
- 回應訊息類型的完整合格名稱或相對名稱。
- describe_service(service_class)
-
從 Service 類別中建立描述元。
引數
- service_class
- 要描述的服務類別。
傳回已初始化的 ServiceDescriptor 執行個體,描述服務。
ServiceDescriptor 的欄位包括:
name
- 未限定的服務名稱。
methods
- 服務的遠端方法。
- import_descriptor_loader(definition_name, importer=__import__ )
-
視需要匯入模組,藉以尋找物件。定義載入器屬於函式,可將定義名稱解析至描述元。匯入尋找器可視需要匯入模組,以將定義解析至其名稱。
引數
- definition_name
- 要查找的定義名稱。
- importer=__import__ )
- 用於匯入新模組的匯入函式。
從任何按名稱找到的可描述類型,傳回適當的描述元。
當名稱不是以定義或模組為主時,則引發 DefinitionNotFoundError。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003edescriptor.py\u003c/code\u003e module is used to convert Google Protocol RPC definitions into a transmittable message format, allowing for the description of enums, messages, and services.\u003c/p\u003e\n"],["\u003cp\u003eDescriptor objects can be created for Enum instances, Enum classes, Field classes, and Message classes, allowing transmission of information about these definitions without needing the source code.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003edescribe()\u003c/code\u003e function serves as a helper for generating descriptors for various objects, and functions like \u003ccode\u003edescribe_enum()\u003c/code\u003e, \u003ccode\u003edescribe_message()\u003c/code\u003e, and \u003ccode\u003edescribe_field()\u003c/code\u003e are used to describe specific types of objects.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eimport_descriptor_loader\u003c/code\u003e function is able to find objects by importing modules as needed and resolve a definition name to a descriptor, returning an appropriate descriptor from any describable type located by name.\u003c/p\u003e\n"],["\u003cp\u003eThe module defines several descriptor classes, such as \u003ccode\u003eFileDescriptor\u003c/code\u003e, \u003ccode\u003eMessageDescriptor\u003c/code\u003e, and \u003ccode\u003eFieldDescriptor\u003c/code\u003e, which contain information like names, types, and values, mirroring the structure of Google Protocol RPC definitions.\u003c/p\u003e\n"]]],[],null,["# Descriptor Module Functions\n\nThe `descriptor.py` module contains message definitions and functions for converting Google Protocol RPC definitions into a transmittable message format.\n\nDescribing an Enum instance, Enum class, Field class, or Message class generates an appropriate descriptor object. A descriptor object is an object that describes other Google Protocol RPC definitions such as enums, messages, and services. You can use this message to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages.\n\nThe descriptors are modeled to be binary compatible with `https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto`\n\n**Note!** The names of types and fields defined in `descriptor.py` do not necessarily match those defined in `descriptor.proto`. Google Protocol RPC is designed this way to make source code files that use these descriptors easier to read. For example, FieldDescriptors need to be prefixed with `TYPE` in `descriptor.proto`, but not in `descriptor.py`.\n\nThe following code sample demonstrates use of the descriptor module to describe a class called `Pixel` using the `MessageDescriptor` class. \n\n```python\nfrom protorpc import descriptor\nfrom protorpc import messages\n\nclass Pixel(messages.Message):\n x = messages.IntegerField(1, required=True)\n y = messages.IntegerField(2, required=True)\n\n color = messages.BytesField(3)\n\n# Describe Pixel class using message descriptor. \nfields = [\n descriptor.FieldDescriptor(name='x', \n number=1,\n label=descriptor.FieldDescriptor.Label.REQUIRED,\n variant=descriptor.FieldDescriptor.Variant.INT64),\n\n descriptor.FieldDescriptor(name='y', \n number=2,\n label=descriptor.FieldDescriptor.Label.REQUIRED,\n variant=descriptor.FieldDescriptor.Variant.INT64),\n\n descriptor.FieldDescriptor(name='color',\n number = 3,\n label=descriptor.FieldDescriptor.Label.OPTIONAL,\n variant=descriptor.FieldDescriptor.Variant.BYTES)]\n\nmessage = descriptor.MessageDescriptor(name='Pixel',\n fields=fields)\n\n# Describing is the equivalent of building the above message.\nmessage == descriptor.describe_message(Pixel)\n```\n\nThe `protorpc.descriptor` package provides the following functions:\n\ndescribe(value)\n\n: Describes any value as a descriptor. You can use this helper function to describe any object with an appropriate descriptor object.\n\n **Arguments**\n\n value\n : Value to describe as a descriptor.\n\n Returns a descriptor if the object is describable as a descriptor, else None.\n\ndescribe_enum(enum_definition)\n\n: Builds a descriptor from an Enum class.\n\n **Arguments**\n\n enum_value\n : The Enum class to describe.\n\n Returns an initialized EnumDescriptor instance describing the Enum instance.\n\n EnumDescriptor Class fields:\n\n `name`\n : Name of the specified Enum without any qualifications.\n\n `values`\n : Values defined by the Enum class.\n\ndescribe_enum_value(enum_value)\n\n: Builds a descriptor from an Enum instance.\n **Arguments**\n\n enum_value\n : The Enum value to describe.\n\n Returns an initialized EnumValueDescriptor instance describing the Enum instance.\n\n EnumValueDescriptor Class fields:\n\n `name`\n : Name of the enumeration value.\n\n `number`\n : Number of the enumeration value.\n\ndescribe_field(field_definition)\n\n: Builds a descriptor from a Field instance.\n\n **Arguments**\n\n field_definition\n : The Field instance to describe.\n\n Returns an initialized FieldDescriptor instance describing the Field instance with a list of Enums and fields.\n\n FieldDescriptor Class Enums:\n\n `Variant`\n : Wire format hint subtypes for the specified field.\n\n `Label`\n : Values for optional, required, and repeated fields.\n\n FieldDescriptor Class fields:\n\n `name`\n : Name of the specified field.\n\n `number`\n : Number of the specified field.\n\n `variant`\n : Variant of the specified field.\n\n `type_name`\n : Type name for the message and enum fields.\n\n `default_value`\n : String representation of the default value.\n\ndescribe_file(module)\n\n: Builds a file from a specified Python module.\n\n **Arguments**\n\n module\n : The Python to describe.\n\n Returns an initialized FileDescriptor instance describing the module.\n\n FileDescriptor Class fields\n\n `package`\n : Fully qualified name of the package that the definitions belong to.\n\n `message_types`\n : Message definitions contained in the file.\n\n `enum_types`\n : Enum definitions contained in the file.\n\n `service_types`\n : Service definitions contained in the file.\n\ndescribe_file_set(modules)\n\n: Builds a file set from the specified Python modules.\n\n **Arguments**\n\n modules\n : Iterable of the Python module to describe.\n\n Returns an initialized FileSet instance describing the module.\n\n FileSet Class fields:\n\n `files`\n : Files in the file set.\n\ndescribe_message(message_definition)\n\n: Builds a descriptor from a Message class.\n\n **Arguments**\n\n message_definition\n : The Message class to describe.\n\n Returns an initialized MessageDescriptor instance describing the Message class.\n\n MessageDescriptor Class fields:\n\n `name`\n : Fully qualified name of the package that the definitions belong to.\n\n `fields`\n : Fields defined for the message.\n\n `message_types`\n : Nested Message classes defined on the message.\n\n `enum_types`\n : Nested Enum classes defined on the message.\n\ndescribe_method(method)\n\n: Builds a descriptor from a remote service method.\n\n **Arguments**\n\n method\n : The remote service method to describe.\n\n Returns an initialized MethodDescriptor instance describing the remote service method.\n\n MethodDescriptor fields:\n\n `name`\n : Name of the service method.\n\n `request_type`\n : Fully qualified name or relative name of the request message type.\n\n `response_type`\n : Fully qualified or relative name of the response message type.\n\ndescribe_service(service_class)\n\n: Builds a descriptor from a Service class.\n\n **Arguments**\n\n service_class\n : The Service class to describe.\n\n Returns an initialized ServiceDescriptor instance describing the service.\n\n ServiceDescriptor fields:\n\n `name`\n : Unqualified name of the Service.\n\n `methods`\n : Remote methods of the Service.\n\nimport_descriptor_loader(definition_name, importer=__import__ )\n\n: Finds objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary.\n\n **Arguments**\n\n definition_name\n : Name of the definition to find.\n\n importer=__import__ )\n : Import function used for importing new modules.\n\n Returns an appropriate descriptor from any describable type located by name.\n\n Raises a [DefinitionNotFoundError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#DefinitionNotFoundError) when a name does not refer to either a definition or a module."]]