Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
定義模組函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
protorpc.definition
套件提供下列函式:
- define_enum(enum_descriptor, module_name)
-
定義描述元的 Enum 類別。
引數
- enum_descriptor
- EnumDescriptor,用於建構 Enum 類別。
- module_name
- 要提供給新描述元類別的模組名稱。
傳回新的 messages.Enum 子類別,如 enum_descriptor
所述。
- define_field(field_descriptor)
-
以佈建的描述元定義 Field 執行個體。
引數
- field_descriptor
- FieldDescriptor 類別,用於建構欄位例項。
傳回 field_descriptor
所述的新欄位例項。
- define_message(message_descriptor, module_name)
-
以佈建的描述元定義 Message 類別。
引數
- message_descriptor
- MessageDescriptor,用於描述訊息類別。
- module_name
- 要提供給新描述元類別的模組名稱。
傳回 message_descriptor
所述的全新 messages.Message 子類別。
- define_service(service_descriptor, module)
-
定義新的服務 Proxy。
引數
- service_descriptor
- 用來描述服務的 ServiceDescriptor 類別。
- module
- 要新增服務的模組。要求和回應類型與這個模組具有相對關係。
回傳能與遠端伺服器通訊的類別 Proxy。
- define_file(file_descriptor, module=None)
-
以指定的檔案描述元定義模組。
引數
- file_descriptor
- FileDescriptor 例項,用於描述模組。
- module=None
- 要新增所含物件的模組。模組名稱會覆寫 file_descriptor.package 中的值。若提供現有模組,會將定義新增至該現有模組。
如果沒有提供模組,請建立一個新模組,將其名稱設為檔案描述元的套件。如有提供模組,會傳回同一個模組。
Utility 函式
- import_file(file_descriptor, modules=None)
-
會將 FileDescriptor 匯入模組空間。類似於 define_file(),但會建立新模組和任何必要的父項模組,並新增至模組參數,如未提供模組參數,則會新增至 sys.modules。
引數
- file_descriptor
- FileDescriptor 例項,用於描述模組。
- modulesmodules=None
- 要更新的模組字典。將會建立不存在的模組及其父項。如找到與
file_descriptor.package
相符的現有模組,則會依據 FileDescriptor 內容更新該模組。
傳回與 modules
引數相符的項目,如果找不到相符項目,則傳回新模組。
- import_file_set(file_set, modules=None)
-
引數
- file_set
- 字串或 FileSet 例項。如果是字串,import_file_set() 會開啟檔案並且讀取序列化的 FileSet。如果是 FileSet 執行個體,則會自該執行個體匯入定義。
- 模組
- 要更新的模組字典。將會建立不存在的模組及其父項。如找到與
file_descriptor.package
相符的現有模組,則會依據 FileDescriptor 內容更新該模組。
除非另有註明,否則本頁面中的內容是採用創用 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\u003eprotorpc.definition\u003c/code\u003e package provides functions for defining various components like Enums, Fields, Messages, and Services from their respective descriptors.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003edefine_file\u003c/code\u003e and \u003ccode\u003eimport_file\u003c/code\u003e allow creating or importing modules based on a \u003ccode\u003eFileDescriptor\u003c/code\u003e, with the latter automatically creating parent modules if necessary.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003edefine_enum\u003c/code\u003e, \u003ccode\u003edefine_field\u003c/code\u003e, \u003ccode\u003edefine_message\u003c/code\u003e, and \u003ccode\u003edefine_service\u003c/code\u003e each creates a new class or instance based on a descriptor, such as \u003ccode\u003eEnumDescriptor\u003c/code\u003e, \u003ccode\u003eFieldDescriptor\u003c/code\u003e, \u003ccode\u003eMessageDescriptor\u003c/code\u003e and \u003ccode\u003eServiceDescriptor\u003c/code\u003e respectively.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eimport_file_set\u003c/code\u003e can import definitions from a \u003ccode\u003eFileSet\u003c/code\u003e instance or a serialized \u003ccode\u003eFileSet\u003c/code\u003e string, updating a provided module dictionary or creating new modules if needed.\u003c/p\u003e\n"]]],[],null,["# Definition Module Functions\n\nThe `protorpc.definition` package provides the following functions:\n\ndefine_enum(enum_descriptor, module_name)\n\n: Defines an Enum class for a descriptor.\n\n **Arguments**\n\n enum_descriptor\n : EnumDescriptor to use to build the Enum class.\n\n module_name\n : Module name to give the new descriptor class.\n\n Returns a new [messages.Enum](/appengine/docs/legacy/standard/python/tools/protorpc/messages/enumclass) subclass as described by `enum_descriptor`.\n\ndefine_field(field_descriptor)\n\n: Defines a Field instance from the provided descriptor.\n\n **Arguments**\n\n field_descriptor\n : FieldDescriptor class to use to build the Field instance.\n\n Returns a new field instance as described by `field_descriptor`.\n\ndefine_message(message_descriptor, module_name)\n\n: Defines a Message class from the provided descriptor.\n\n **Arguments**\n\n message_descriptor\n : MessageDescriptor to use to describe the message class.\n\n module_name\n : The module name to give the new descriptor class.\n\n Returns a new [messages.Message sub-class](/appengine/docs/legacy/standard/python/tools/protorpc/messages/messageclass) as described by `message_descriptor`.\n\ndefine_service(service_descriptor, module)\n\n: Defines a new service proxy.\n\n **Arguments**\n\n service_descriptor\n : ServiceDescriptor class that describes the service.\n\n module\n : The module to add the service to. Request and response types are found relative to this module.\n\n Returns a class proxy capable of communicating with a remote server.\n\ndefine_file(file_descriptor, module=None)\n\n: Defines a module from the given file descriptor.\n\n **Arguments**\n\n file_descriptor\n : FileDescriptor instance from which to describe the module.\n\n module=None\n : Module to which to add contained objects. Module name overrides value in file_descriptor.package. Definitions are added to the existing module if provided.\n\n If no module is provided, creates a new module with its name set to the file descriptor's package. If a module is provided, returns the same module.\n\nimport_file(file_descriptor, modules=None)\n\n: Imports a FileDescriptor into a module space. Similar to [define_file()](#define_file), except that a new module and any required parent modules are created and added to the modules parameter, or sys.modules if not provided.\n\n **Arguments**\n\n file_descriptor\n : FileDescriptor instance from which to describe the module.\n\n modulesmodules=None\n : Dictionary of modules to update. Modules and their parents that\n do not exist will be created. If an existing module is found that\n matches `file_descriptor.package`, that module is updated with the FileDescriptor contents.\n\n Returns matches to the `modules` argument, or a new module if no matches are found.\n\nimport_file_set(file_set, modules=None)\n\n: \u003cbr /\u003e\n\n **Arguments**\n\n file_set\n : Either a string or a FileSet instance. If a string, import_file_set() opens the file and reads the serialized FileSet. If a FileSet instance, imports definitions from this instance.\n\n modules\n : Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches the `file_descriptor.package`, that module is updated with the FileDescriptor contents."]]