Exemplo n.º 1
0
def read_proto_from_binary():
    """
    Reads a binary file and converts i
    :return:
    """
    with open("simple.bin", "rb") as fh:
        simple_message_read = simple_pb2.SimpleMessage().FromString(fh.read())

    return simple_message_read
Exemplo n.º 2
0
def create_simple_protobuf() -> simple_pb2.SimpleMessage:
    """
    Creates a simple protobuf with content filled.

    :return: A newly create simple_pb2 Protobuf.
    """
    simple_message = simple_pb2.SimpleMessage()
    simple_message.id = 123
    simple_message.is_simple = True
    simple_message.name = "This is a simple Message"
    sample_list = simple_message.sample_list
    sample_list.append(3)
    sample_list.append(4)
    sample_list.append(5)
    sample_list.append(6)

    return simple_message
Exemplo n.º 3
0
import simple.simple_pb2 as simple_pb2

simple_message = simple_pb2.SimpleMessage()

simple_message.id = 123
simple_message.is_simple = True
simple_message.name = "This is a simple message"
sample_list = simple_message.sample_list
sample_list.append(3)
sample_list.append(4)
sample_list.append(5)

print(simple_message)

with open("sample.bin", "wb") as file:
    bytesAsString = simple_message.SerializeToString()
    file.write(bytesAsString)

with open("sample.bin", "rb") as file:
    print("Read Values")
    simple_message_read = simple_pb2.SimpleMessage().FromString(file.read())

print(simple_message_read)
print(f"Is Simple?: {simple_message_read.is_simple}")
Exemplo n.º 4
0
import simple.simple_pb2 as simple_pb2

sm = simple_pb2.SimpleMessage()

sm.id = 23
sm.is_simple = True
sm.name = "This is the name of the message"
sml = sm.sample_list
sml.append(3)
sml.append(4)
sml.append(5)

print("created message:")
print(sm)

with open("simple.bin", "wb") as f:
    bytesAsString = sm.SerializeToString()
    f.write(bytesAsString)
    print('message was written to file')
    f.close()

with open("simple.bin", "rb") as f:
    sm_msg_read = simple_pb2.SimpleMessage().FromString(f.read())
    f.close()
    print(sm_msg_read)
    print("Only one value:", sm_msg_read.is_simple)
    print("Only one value:", sm_msg_read.name)