### Setup Project with Pyrobuf Modules Source: https://context7.com/appnexus/pyrobuf/llms.txt Configures a Python project's setup.py to automatically compile proto files using pyrobuf during package installation. It specifies the directory containing the proto files and lists pyrobuf as a dependency. ```python from setuptools import setup, find_packages setup( name="my_project", version="0.1", packages=find_packages(), description="A project using pyrobuf", install_requires=['pyrobuf'], setup_requires=['pyrobuf'], # Compile all .proto files in the 'proto' directory pyrobuf_modules="proto" ) ``` -------------------------------- ### Setuptools Integration - pyrobuf_modules Source: https://context7.com/appnexus/pyrobuf/llms.txt Integrate pyrobuf compilation into your package's setup.py for automatic proto compilation during installation. ```APIDOC ## Setuptools Integration - pyrobuf_modules ### Description This section outlines how to integrate Pyrobuf compilation into your Python package's `setup.py` file. This ensures that your Protocol Buffer (`.proto`) files are automatically compiled into Python code whenever your package is installed or built. ### Usage To enable this integration, you need to modify your `setup.py` file to include the `pyrobuf_modules` build command. This typically involves specifying the `cmdclass` argument in your `setup()` function. ### Example `setup.py` ```python from setuptools import setup, find_packages from pyrobuf.setuptools import pyrobuf_modules setup( name='my_protobuf_package', version='0.1.0', packages=find_packages(), # Specify pyrobuf_modules in cmdclass to enable proto compilation cmdclass={ 'build_py': pyrobuf_modules.BuildPy }, # Add your proto files directory to package_data if they are not in a package # package_data={ # '': ['*.proto'] # }, # Or specify a dedicated directory for proto files # data_files=[('protos', ['path/to/your/protos/*.proto'])] ) ``` ### Explanation - `from pyrobuf.setuptools import pyrobuf_modules`: Imports the necessary module for setuptools integration. - `cmdclass={'build_py': pyrobuf_modules.BuildPy}`: This is the core of the integration. It tells setuptools to use Pyrobuf's `BuildPy` command (which extends the default `build_py` command) during the build process. `BuildPy` will automatically find and compile `.proto` files within your package structure. ### Configuration - **Proto File Location**: By default, `pyrobuf_modules.BuildPy` looks for `.proto` files within your Python packages. If your `.proto` files are located in a separate directory (e.g., a `protos/` directory at the root of your project), you might need to adjust `package_data` or `data_files` in your `setup()` call to ensure they are included and accessible during the build. - **Customization**: For more advanced use cases, you can subclass `pyrobuf_modules.BuildPy` or configure specific directories to scan for `.proto` files. ``` -------------------------------- ### Setuptools Integration for Pyrobuf Compilation - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Demonstrates how to integrate pyrobuf compilation into a Python package's setup.py file. This ensures that Protocol Buffer definitions are automatically compiled into Python code during the package installation process, simplifying the build and distribution workflow. ```python # Example setup.py integration (content not provided in source) ``` -------------------------------- ### Install and Verify Pyrobuf Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Commands to install the Pyrobuf library via pip and verify the installation by importing the core module. ```bash pip install pyrobuf python -c "import pyrobuf_list" ``` -------------------------------- ### Compile Protobuf Files with Pyrobuf CLI Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Usage examples for the pyrobuf CLI tool to compile .proto files or directories into Cython-based code. ```bash pyrobuf --help python -m pyrobuf --help ``` -------------------------------- ### Install Pyrobuf using pip Source: https://context7.com/appnexus/pyrobuf/llms.txt Installs the pyrobuf library using the Python package installer. ```bash pip install pyrobuf ``` -------------------------------- ### Compile Proto Files using Compiler API Source: https://context7.com/appnexus/pyrobuf/llms.txt Demonstrates programmatic compilation of proto files using the pyrobuf.compile.Compiler class. This allows for advanced build workflows, specifying source files, output directories, include paths, and build options like installing the extension. ```python from pyrobuf.compile import Compiler # Create compiler instance compiler = Compiler( sources=['messages.proto', 'other.proto'], out='generated', # Output directory for .pyx files build='build', # C compiler build directory install=True, # Install the extension after build proto3=False, # Use proto2 syntax (default) package='my_messages', # Combine all messages into single package includes=['/path/to/deps'] # Include paths for imports ) # Run compilation compiler.compile() # Or extend an existing setuptools distribution # compiler.extend(dist) ``` -------------------------------- ### Pyrobuf CLI Compiler Usage Source: https://context7.com/appnexus/pyrobuf/llms.txt Demonstrates various ways to use the pyrobuf command-line interface to compile .proto files into Python/Cython modules. Supports single file compilation, custom output directories, direct installation, proto3 syntax, directory compilation into packages, and specifying include paths. ```bash # Basic compilation of a single proto file pyrobuf my_message.proto # Compile with custom output directory pyrobuf my_message.proto --out-dir generated # Compile and install the extension directly pyrobuf my_message.proto --install # Compile proto3 syntax files pyrobuf my_message.proto --proto3 # Compile entire directory of proto files into a single package pyrobuf /path/to/proto/specs --install --package=my_messages # Compile with include paths for imports pyrobuf my_message.proto --include /path/to/imports --include /other/path ``` -------------------------------- ### Serialize and Deserialize Messages in Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Demonstrates the core functionality of serializing a pyrobuf message object into a binary string format using `SerializeToString` and deserializing it back using `ParseFromString` or `FromString`. Includes examples of checking bytes consumed and accessing deserialized fields. ```python from test_message_proto import Test # Create and populate message test = Test() test.timestamp = 539395200 test.field = 10689 test.string_field = "hello" test.req_field = 100 # Serialize to binary format binary_data = test.SerializeToString() # Returns: bytearray(b'\x08\x80\x89\x9a\x81\x02\x10\xc1S\x1a\x05helloP\xc8\x01') # Deserialize from binary format test2 = Test() bytes_consumed = test2.ParseFromString(binary_data) print(bytes_consumed) # Number of bytes consumed print(test2.string_field) # Output: "hello" # Alternative: Create directly from binary data test3 = Test.FromString(binary_data) print(test3.timestamp) # Output: 539395200 ``` -------------------------------- ### Nested Messages and Substructures in Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Explains how to access and manipulate nested message fields and repeated fields within nested structures using dot notation. Parent structures are automatically created as needed. Includes examples of setting scalar, repeated, and nested message fields. ```python from test_message_proto import Test test = Test() # Set nested message fields (parent messages are auto-created) test.substruct.field1 = 12345 test.substruct.field2 = "hello" # Access deeply nested structures test.substruct.field3.field1 = 1419.67 test.substruct.field3.ss2_field2 = "goodbye" # Add to nested repeated fields test.substruct.list.append(354.94) test.substruct.list_string.append("something") # Add message objects to nested repeated message fields obj = test.substruct.list_object.add() obj.field1 = 3.14159 obj.ss2_field2 = "pi" ``` -------------------------------- ### Python: Distribute Package with Pyrobuf Modules using setup.py Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Demonstrates how to include Pyrobuf-compiled Protobuf messages in a Python package distribution by using the `pyrobuf_modules` keyword argument in `setup.py`. This creates a separate package for the compiled Protobuf code. ```python from setuptools import setup, find_packages setup( name="sample", version="0.1", packages=find_packages(), description="A sample package", install_requires=['pyrobuf'], setup_requires=['pyrobuf'], pyrobuf_modules="proto" ) ``` -------------------------------- ### Python: Create, Serialize, and Deserialize Protobuf Messages Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Demonstrates how to import a Protobuf message class, instantiate it, set fields, serialize to bytes, and deserialize from bytes. It also shows serialization/deserialization to JSON and dictionaries. ```python from test_message_proto import Test test = Test() test.field = 5 test.req_field = 2 test.string_field = "hello!" test.list_fieldx.append(12) test.test_ref.field2 = 3.14 print(test.string_field) serialized_bytes = bytes(test.SerializeToBytes()) print(serialized_bytes) test.ParseFromString(serialized_bytes) json_string = test.SerializeToJson() print(json_string) test.ParseFromJson('{"field": 5, "req_field": 2, "list_fieldx": [12], "string_field": "hello!", "test_ref": {"field2": 3.14}}') dict_repr = test.SerializeToDict() print(dict_repr) ``` -------------------------------- ### Command Line: Package Multiple Protobuf Specs into a Single Namespace Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Illustrates how to use the `pyrobuf` command-line tool to compile multiple .proto files into a single Python package, allowing for a unified namespace for all generated message classes. ```bash pyrobuf /path/to/proto/specs --install --package=my_messages ``` -------------------------------- ### Develop and Clean Pyrobuf Environment Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Commands to set up the development environment for contributing to Pyrobuf and cleaning up generated build artifacts. ```bash python setup.py develop python setup.py clean ``` -------------------------------- ### Create and Use Protocol Buffer Messages in Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Shows how to import a compiled message class from pyrobuf, create an instance, set scalar fields, and access field values. It highlights the requirement to set 'required' fields before serialization. ```python # Given a compiled test_message.proto with message Test from test_message_proto import Test # Create a new message instance test = Test() # Set scalar fields test.timestamp = 539395200 test.field = 10689 test.string_field = "go goats!" # Set required fields (must be set before serialization) test.req_field = -80914 # Access field values print(test.string_field) # Output: "go goats!" print(test.timestamp) # Output: 539395200 ``` -------------------------------- ### Python: Encode and Decode Integers with pyrobuf_util Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Shows how to use the `pyrobuf_util` module to encode integers into variable-length byte arrays (varints) and decode them back, including support for signed integers. ```python import pyrobuf_util encoded_unsigned = pyrobuf_util.to_varint(2**16-1) print(encoded_unsigned) decoded_unsigned, offset_unsigned = pyrobuf_util.from_varint(b'\xff\xff\x03', offset=0) print(f"Decoded: {decoded_unsigned}, Offset: {offset_unsigned}") encoded_signed = pyrobuf_util.to_signed_varint(-2**16) print(encoded_signed) decoded_signed, offset_signed = pyrobuf_util.from_signed_varint(b'\xff\xff\x07', offset=0) print(f"Decoded: {decoded_signed}, Offset: {offset_signed}") ``` -------------------------------- ### Run Pyrobuf Tests Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Methods to execute the Pyrobuf test suite, ensuring all protobuf message specifications are built correctly before validation. ```bash py.test python setup.py test ``` -------------------------------- ### Python: Import and Use Packaged Pyrobuf Modules Source: https://github.com/appnexus/pyrobuf/blob/master/README.md Shows how to import and use Protobuf message classes that have been compiled and packaged using Pyrobuf, typically from a package named like 'your_package_name_proto'. ```python from sample_proto import MyMessage my_message = MyMessage() # Further operations with my_message... ``` -------------------------------- ### Check Required Fields Initialization - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Verifies if all required fields in a Protocol Buffer message (using proto2 syntax) have been set. This is crucial for ensuring data integrity before serialization or further processing. Requires the message class definition (e.g., from test_is_initialized_proto). ```python from test_is_initialized_proto import TestIsInitialized # New message with required fields is not initialized message = TestIsInitialized() print(message.IsInitialized()) # Output: False # Set required field message.req_field = 1 print(message.IsInitialized()) # Output: True # Submessages with required fields must also be initialized message.sub_message.opt_field = 2 # Sets optional field only print(message.IsInitialized()) # Output: False (sub_message.req_field not set) message.sub_message.req_field = 2 print(message.IsInitialized()) # Output: True ``` -------------------------------- ### IsInitialized Source: https://context7.com/appnexus/pyrobuf/llms.txt Check if all required fields in a message (and its submessages) are set. Primarily for proto2 syntax. ```APIDOC ## IsInitialized ### Description Check if all required fields in a message, including those in nested messages, are set. This method is particularly relevant for messages defined with proto2 syntax. ### Method `IsInitialized()` ### Endpoint N/A (Method calls on message objects) ### Request Example ```python from test_is_initialized_proto import TestIsInitialized # New message with required fields is not initialized message = TestIsInitialized() print(message.IsInitialized()) # Output: False # Set required field message.req_field = 1 print(message.IsInitialized()) # Output: True # Submessages with required fields must also be initialized message.sub_message.opt_field = 2 # Sets optional field only print(message.IsInitialized()) # Output: False (sub_message.req_field not set) message.sub_message.req_field = 2 print(message.IsInitialized()) # Output: True ``` ### Response Example (printed output) ``` False True False True ``` ``` -------------------------------- ### Compiler API - pyrobuf.compile.Compiler Source: https://context7.com/appnexus/pyrobuf/llms.txt The Compiler class allows for programmatic compilation of .proto files into Python extensions, supporting custom build directories, output paths, and proto syntax versions. ```APIDOC ## Compiler API - pyrobuf.compile.Compiler ### Description Programmatically compile .proto files into Python extensions. This is useful for advanced build workflows or dynamic code generation. ### Method N/A (Class instantiation and method invocation) ### Parameters #### Constructor Arguments - **sources** (list) - Required - List of .proto file paths to compile. - **out** (string) - Optional - Output directory for generated .pyx files. - **build** (string) - Optional - C compiler build directory. - **install** (boolean) - Optional - Whether to install the extension after build. - **proto3** (boolean) - Optional - Set to True to use proto3 syntax. - **package** (string) - Optional - Package name to group generated messages. - **includes** (list) - Optional - List of include paths for proto imports. ### Request Example compiler = Compiler(sources=['messages.proto'], out='generated', proto3=False) compiler.compile() ### Response - **Success** (void) - Generates Python extension modules in the specified output directory. ``` -------------------------------- ### Parse Proto Definitions using Parser API Source: https://context7.com/appnexus/pyrobuf/llms.txt Shows how to programmatically parse proto files to extract message and enum definitions using pyrobuf.parse_proto.Parser and Proto3Parser. It covers parsing from filenames and from strings, and extracting imports, messages, and enums. ```python from pyrobuf.parse_proto import Parser, Proto3Parser # Parse a proto2 file msg_def = Parser.parse_from_filename('message.proto', includes=[]) # Returns dict with keys: 'imports', 'messages', 'enums' print(msg_def['messages']) # List of parsed message definitions print(msg_def['enums']) # List of parsed enum definitions print(msg_def['imports']) # List of imported proto files # Parse proto3 files msg_def = Proto3Parser.parse_from_filename('message3.proto', includes=[]) # Parse from string parser = Parser(proto_content_string) msg_def = parser.parse(fname='inline.proto', includes=[]) ``` -------------------------------- ### Varint Encoding/Decoding Utilities - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Provides low-level utilities for encoding integers into Protocol Buffer's varint format and decoding them back. It supports both signed and unsigned integers, returning the decoded value and the new offset within the byte stream. Requires the pyrobuf_util module. ```python import pyrobuf_util # Encode integer to varint encoded = pyrobuf_util.to_varint(2**16 - 1) print(encoded) # Output: bytearray(b'\xff\xff\x03') # Decode varint, returns (value, new_offset) value, offset = pyrobuf_util.from_varint(b'\xff\xff\x03', offset=0) print(value) # Output: 65535 print(offset) # Output: 3 # Encode signed integer (zigzag encoding) encoded = pyrobuf_util.to_signed_varint(-2**16) print(encoded) # Output: bytearray(b'\xff\xff\x07') # Decode signed varint value, offset = pyrobuf_util.from_signed_varint(b'\xff\xff\x07', offset=0) print(value) # Output: -65536 print(offset) # Output: 3 # Get varint value without offset tracking value = pyrobuf_util.get_varint(b'\xff\xff\x03') print(value) # Output: 65535 value = pyrobuf_util.get_signed_varint(b'\xff\xff\x07') print(value) # Output: -65536 ``` -------------------------------- ### Varint Encoding Utilities - pyrobuf_util Source: https://context7.com/appnexus/pyrobuf/llms.txt Low-level utilities for encoding and decoding protobuf varint integers, including signed integers using zigzag encoding. ```APIDOC ## Varint Encoding Utilities - pyrobuf_util ### Description Provides low-level functions for encoding integers into the protobuf varint format and decoding them back. Supports both signed and unsigned integers, utilizing zigzag encoding for signed values. ### Module `pyrobuf_util` ### Functions - `to_varint(value)`: Encodes an unsigned integer to a varint bytearray. - `from_varint(buffer, offset)`: Decodes a varint from a buffer, returning the value and the new offset. - `to_signed_varint(value)`: Encodes a signed integer to a varint bytearray using zigzag encoding. - `from_signed_varint(buffer, offset)`: Decodes a signed varint from a buffer using zigzag encoding, returning the value and the new offset. - `get_varint(buffer)`: Decodes a varint from a buffer without returning the offset. - `get_signed_varint(buffer)`: Decodes a signed varint from a buffer without returning the offset. ### Request Example (Encoding/Decoding Unsigned Varint) ```python import pyrobuf_util # Encode integer to varint encoded = pyrobuf_util.to_varint(2**16 - 1) print(encoded) # Output: bytearray(b'\xff\xff\x03') # Decode varint, returns (value, new_offset) value, offset = pyrobuf_util.from_varint(b'\xff\xff\x03', offset=0) print(value) # Output: 65535 print(offset) # Output: 3 # Get varint value without offset tracking value = pyrobuf_util.get_varint(b'\xff\xff\x03') print(value) # Output: 65535 ``` ### Request Example (Encoding/Decoding Signed Varint) ```python import pyrobuf_util # Encode signed integer (zigzag encoding) encoded = pyrobuf_util.to_signed_varint(-2**16) print(encoded) # Output: bytearray(b'\xff\xff\x07') # Decode signed varint value, offset = pyrobuf_util.from_signed_varint(b'\xff\xff\x07', offset=0) print(value) # Output: -65536 print(offset) # Output: 3 # Get signed varint value without offset tracking value = pyrobuf_util.get_signed_varint(b'\xff\xff\x07') print(value) # Output: -65536 ``` ### Response Example (printed output) ``` bytearray(b'\xff\xff\x03') 65535 3 65535 bytearray(b'\xff\xff\x07') -65536 3 -65536 ``` ``` -------------------------------- ### Parser API - pyrobuf.parse_proto.Parser Source: https://context7.com/appnexus/pyrobuf/llms.txt The Parser API enables the extraction of message and enum definitions from .proto files or raw strings, returning a structured dictionary. ```APIDOC ## Parser API - pyrobuf.parse_proto.Parser ### Description Parse proto files to extract message definitions programmatically. Supports both proto2 and proto3 syntax. ### Method N/A (Static methods and class instantiation) ### Parameters #### Method Arguments - **filename** (string) - Required - Path to the .proto file. - **includes** (list) - Optional - List of include paths for resolving imports. ### Request Example from pyrobuf.parse_proto import Parser msg_def = Parser.parse_from_filename('message.proto', includes=[]) ### Response #### Success Response (200) - **imports** (list) - List of imported proto files. - **messages** (list) - List of parsed message definitions. - **enums** (list) - List of parsed enum definitions. ``` -------------------------------- ### Working with Repeated Fields in Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Demonstrates how to handle repeated fields in pyrobuf messages, which behave like Python lists. Includes appending scalar values, accessing elements by index, and adding message objects to repeated message fields. ```python from test_message_proto import Test test = Test() # Append scalar values to repeated field for i in range(5): test.list_fieldx.append(i * 100) # Access repeated field values print(len(test.list_fieldx)) # Output: 5 print(test.list_fieldx[0]) # Output: 0 print(test.list_fieldx[2]) # Output: 200 # Add message objects to repeated message fields obj = test.list_ref.add() obj.timestamp = 539395200 obj.field1 = 1111 obj.field2 = 1.2345 obj.field3 = "foo" # Access repeated message objects print(len(test.list_ref)) # Output: 1 print(test.list_ref[0].field1) # Output: 1111 ``` -------------------------------- ### SerializeToDict / ParseFromDict Source: https://context7.com/appnexus/pyrobuf/llms.txt Convert messages to and from native Python dictionaries for easy manipulation. ```APIDOC ## SerializeToDict / ParseFromDict ### Description Convert messages to native Python dictionaries for easy manipulation, and deserialize from dictionaries back into message objects. ### Method `SerializeToDict()` and `ParseFromDict(dictionary)` ### Endpoint N/A (Method calls on message objects) ### Parameters #### Request Body (for ParseFromDict) - **dictionary** (dict) - Required - The Python dictionary to parse. ### Request Example (SerializeToDict) ```python from test_message_proto import Test test = Test() test.timestamp = 539395200 test.field = 10689 test.string_field = "hello" test.req_field = 100 test.substruct.field1 = 12345 test.substruct.field2 = "nested" data = test.SerializeToDict() print(data) ``` ### Response Example (SerializeToDict - printed output) ```python { 'timestamp': 539395200, 'field': 10689, 'string_field': 'hello', 'req_field': 100, 'substruct': { 'field1': 12345, 'field2': 'nested' } } ``` ### Request Example (ParseFromDict) ```python from test_message_proto import Test test2 = Test() data = { 'timestamp': 539395200, 'field': 10689, 'string_field': 'hello', 'req_field': 100, 'substruct': { 'field1': 12345, 'field2': 'nested' } } test2.ParseFromDict(data) print(test2.substruct.field2) ``` ### Response Example (ParseFromDict - printed output) ``` nested ``` ``` -------------------------------- ### HasField / ClearField / Clear Source: https://context7.com/appnexus/pyrobuf/llms.txt Check if fields are set and clear individual or all fields. ```APIDOC ## HasField / ClearField / Clear ### Description Check if scalar or message fields are set using `HasField()`, clear individual fields using `ClearField()`, or clear all fields using `Clear()`. ### Method `HasField(field_name)`, `ClearField(field_name)`, `Clear()` ### Endpoint N/A (Method calls on message objects) ### Parameters #### Path Parameters - **field_name** (string) - Required - The name of the field to check or clear. ### Request Example (HasField) ```python from test_message_proto import Test test = Test() print(test.HasField('timestamp')) # Output: False test.timestamp = 123 print(test.HasField('timestamp')) # Output: True test.substruct.field2 = "something" print(test.HasField('substruct')) # Output: True ``` ### Request Example (ClearField) ```python from test_message_proto import Test test = Test() test.timestamp = 123 print(test.HasField('timestamp')) # Output: True test.ClearField('timestamp') print(test.HasField('timestamp')) # Output: False test.substruct.field2 = "something" print(test.HasField('substruct')) # Output: True test.ClearField('substruct') print(test.HasField('substruct')) # Output: False ``` ### Request Example (Clear) ```python from test_message_proto import Test test = Test() test.timestamp = 456 test.string_field = "hello" print(test.HasField('timestamp')) # Output: True print(test.HasField('string_field')) # Output: True test.Clear() print(test.HasField('timestamp')) # Output: False print(test.HasField('string_field')) # Output: False ``` ### Note `HasField()` raises `ValueError` for repeated fields. ``` -------------------------------- ### Manage Message Fields (HasField, ClearField, Clear) - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Provides methods to check if a field is set (HasField), clear individual fields (ClearField), or clear all fields in a message (Clear). Note that HasField raises a ValueError for repeated fields. Requires the message class definition. ```python from test_message_proto import Test test = Test() # Check if scalar field is set print(test.HasField('timestamp')) # Output: False test.timestamp = 123 print(test.HasField('timestamp')) # Output: True # Clear individual field test.ClearField('timestamp') print(test.HasField('timestamp')) # Output: False # Check message fields test.substruct.field2 = "something" print(test.HasField('substruct')) # Output: True # Clear nested message field test.ClearField('substruct') print(test.HasField('substruct')) # Output: False # Clear all fields at once test.timestamp = 456 test.string_field = "hello" test.Clear() print(test.HasField('timestamp')) # Output: False print(test.HasField('string_field')) # Output: False # Note: HasField raises ValueError for repeated fields # test.HasField('list_fieldx') # Raises ValueError ``` -------------------------------- ### Serialize/Deserialize Messages to Dictionary - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Converts Protocol Buffer messages to and from native Python dictionaries, facilitating easy data manipulation. This function requires the message class definition (e.g., from test_message_proto). ```python from test_message_proto import Test test = Test() test.timestamp = 539395200 test.field = 10689 test.string_field = "hello" test.req_field = 100 test.substruct.field1 = 12345 test.substruct.field2 = "nested" # Serialize to Python dictionary data = test.SerializeToDict() # Output: {'timestamp': 539395200, 'field': 10689, 'string_field': 'hello', # 'req_field': 100, 'substruct': {'field1': 12345, 'field2': 'nested'}} # Deserialize from dictionary test2 = Test() test2.ParseFromDict(data) print(test2.substruct.field2) # Output: "nested" ``` -------------------------------- ### SerializeToJson / ParseFromJson Source: https://context7.com/appnexus/pyrobuf/llms.txt Serialize messages to JSON format for human-readable output or web APIs, and deserialize them back into message objects. ```APIDOC ## SerializeToJson / ParseFromJson ### Description Serialize messages to JSON format for human-readable output or web APIs. Deserialize from JSON string back into message objects. ### Method `SerializeToJson()` and `ParseFromJson(json_string)` ### Endpoint N/A (Method calls on message objects) ### Parameters #### Request Body (for ParseFromJson) - **json_string** (string) - Required - The JSON string to parse. ### Request Example (SerializeToJson) ```python from test_message_proto import Test test = Test() test.timestamp = 539395200 test.field = 10689 test.string_field = "hello" test.req_field = 100 test.list_fieldx.append(100) test.list_fieldx.append(200) json_str = test.SerializeToJson() print(json_str) ``` ### Response Example (SerializeToJson) ```json { "timestamp": 539395200, "field": 10689, "string_field": "hello", "req_field": 100, "list_fieldx": [ 100, 200 ] } ``` ### Request Example (ParseFromJson) ```python from test_message_proto import Test test2 = Test() json_str = '{"timestamp": 539395200, "field": 10689, "string_field": "hello", "req_field": 100, "list_fieldx": [100, 200]}' test2.ParseFromJson(json_str) print(test2.timestamp) print(test2.list_fieldx[0]) ``` ### Response Example (ParseFromJson - printed output) ``` 539395200 100 ``` ``` -------------------------------- ### Serialize/Deserialize Messages to JSON - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Serializes Protocol Buffer messages to a JSON string for human-readable output or web APIs, and deserializes JSON strings back into messages. It requires the message class definition (e.g., from test_message_proto). ```python from test_message_proto import Test test = Test() test.timestamp = 539395200 test.field = 10689 test.string_field = "hello" test.req_field = 100 test.list_fieldx.append(100) test.list_fieldx.append(200) # Serialize to JSON string json_str = test.SerializeToJson() # Output: '{"timestamp": 539395200, "field": 10689, "string_field": "hello", "req_field": 100, "list_fieldx": [100, 200]}' # Deserialize from JSON string test2 = Test() test2.ParseFromJson(json_str) print(test2.timestamp) # Output: 539395200 print(test2.list_fieldx[0]) # Output: 100 ``` -------------------------------- ### MergeFrom Source: https://context7.com/appnexus/pyrobuf/llms.txt Merge fields from another message, extending repeated fields and overwriting scalar fields. ```APIDOC ## MergeFrom ### Description Merge fields from a source message into a destination message. Scalar fields in the source overwrite those in the destination. Repeated fields are extended with elements from the source. ### Method `MergeFrom(source_message)` ### Endpoint N/A (Method calls on message objects) ### Parameters #### Request Body - **source_message** (Message) - Required - The message to merge from. ### Request Example ```python from test_message_proto import Test source = Test() source.timestamp = 123 source.list_fieldx.append(100) source.list_ref.add().field1 = 3 dest = Test() dest.field = 456 dest.list_fieldx.append(200) dest.MergeFrom(source) print(dest.timestamp) # Output: 123 print(dest.field) # Output: 456 print(len(dest.list_fieldx)) # Output: 2 print(dest.list_fieldx[0]) # Output: 200 print(dest.list_fieldx[1]) # Output: 100 print(len(dest.list_ref)) # Output: 1 print(dest.list_ref[0].field1) # Output: 3 ``` ### Response Example (printed output) ``` 123 456 2 200 100 1 3 ``` ``` -------------------------------- ### Merge Message Fields - Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Merges fields from a source message into a destination message. Scalar fields in the source overwrite those in the destination, while repeated fields are extended. Requires the message class definition. ```python from test_message_proto import Test source = Test() source.timestamp = 123 source.list_fieldx.append(100) source.list_ref.add().field1 = 3 dest = Test() dest.field = 456 dest.list_fieldx.append(200) # Merge source into dest dest.MergeFrom(source) # Scalar fields from source overwrite dest print(dest.timestamp) # Output: 123 # Unset fields in source don't affect dest print(dest.field) # Output: 456 # Repeated fields are extended print(len(dest.list_fieldx)) # Output: 2 print(dest.list_fieldx[0]) # Output: 200 (original) print(dest.list_fieldx[1]) # Output: 100 (merged) # Repeated message fields are also extended print(len(dest.list_ref)) # Output: 1 print(dest.list_ref[0].field1) # Output: 3 ``` -------------------------------- ### Enum Fields Usage in Python Source: https://context7.com/appnexus/pyrobuf/llms.txt Illustrates how to use enum fields within pyrobuf messages. Enums are accessed as class attributes of the message type, and their values can be assigned and compared. ```python from test_message_proto import Test test = Test() # Access enum values as class attributes test.enum_field = Test.TEST_ENUM_FIELD_0 test.enum_field = Test.TEST_ENUM_FIELD_1 test.enum_field = Test.TEST_ENUM_FIELD_2 # Compare enum values if test.enum_field == Test.TEST_ENUM_FIELD_2: print("Enum is set to field 2") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.