コード例 #1
0
ファイル: enum_demo.py プロジェクト: codefoxut/pyexamples
def make_enum():
    enum_msg = enum_example_pb2.EnumMessage()
    enum_msg.id = 234

    print(enum_msg)

    enum_msg.day_of_the_week = enum_example_pb2.THURSDAY

    print(enum_msg)
    print(enum_example_pb2.WEDNESDAY)

    with open('enums.bin', "wb") as f:
        print("write as binary.")
        bytes_string = enum_msg.SerializeToString()
        f.write(bytes_string)

    with open("enums.bin", "rb") as f:
        print("reading binary file.")
        simple_msg_read = enum_example_pb2.EnumMessage().FromString(f.read())
        print(simple_msg_read)
コード例 #2
0
import enum_example_pb2 as enum_example_pb2

enum_message = enum_example_pb2.EnumMessage()
enum_message.id = 345
enum_message.day_of_the_week = enum_example_pb2.THURSDAY

print(enum_message)

# Enums are represented by numerical value. The below demonstrates how this works.
# print(enum_message.day_of_the_week)
# print(enum_message.day_of_the_week == enum_example_pb2.THURSDAY)

with open("./protobuf-python/enums/enums.bin", "wb") as f:
    f.write(enum_message.SerializeToString())
    print("Wrote to a file")

with open("./protobuf-python/enums/enums.bin", "rb") as f:
    enum_message_read = enum_example_pb2.EnumMessage().FromString(f.read())
    print("Read a file")

print(enum_message_read)
コード例 #3
0
import enum_example_pb2 as enum_example_pb2

enum_message = enum_example_pb2.EnumMessage()
enum_message.id = 345
enum_message.day_of_the_week = enum_example_pb2.THURSDAY

print(enum_message)
'''
print(enum_message.day_of_the_week == enum_example_pb2.THURSDAY)

with open("enums.bin", "wb") as f:
    f.write(enum_message.SerializeToString())
    print("wrote to a file")

with open("enums.bin", "rb") as f:
    enum_message_read = enum_example_pb2.EnumMessage().FromString(f.read())
    print("read file")

print(enum_message_read)
'''