Example #1
0
def read():
    file = open("simple.bin", "rb")
    # instanc - fill values and reading
    simple_message_read = simple_pb2.SimpleMessage().FromString(file.read())
    # showing
    print(simple_message_read)
    # particular value
    print("Is Simple?: " + str(simple_message_read.id))
def parse_input(input):
    # Create mesage object
    simple_received = simple_pb2.SimpleMessage()

    # Parse message object
    simple_received.ParseFromString(input)

    # Print fields
    print(
        f"Frequency = {simple_received.frequency}, duty_cycle = {simple_received.duty_cycle}"
    )
Example #3
0
import 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'

simple_message.sample_list.append(3) 
simple_message.sample_list.append(4)
simple_message.sample_list.append(5)    

print (simple_message)

with open('simple.bin', 'wb') as f:
	print ("write as binary")
	bytesAsString = simple_message.SerializeToString()
	f.write(bytesAsString)

with open('simple.bin', 'rb') as f:
	print ("read values")
	simple_message_read = simple_pb2.SimpleMessage().FromString(f.read())

print (simple_message_read)
Example #4
0
    file.close()


def read():
    file = open("simple.bin", "rb")
    # instanc - fill values and reading
    simple_message_read = simple_pb2.SimpleMessage().FromString(file.read())
    # showing
    print(simple_message_read)
    # particular value
    print("Is Simple?: " + str(simple_message_read.id))


if __name__ == "__main__":
    # object
    simple_message = simple_pb2.SimpleMessage()
    # calling attributes
    simple_message.id = 123
    simple_message.is_simple = True
    simple_message.name = "Python's simple message"
    # repeated
    sample_list = simple_message.sample_list
    sample_list.append(3)
    sample_list.append(4)
    sample_list.append(5)
    # printing object
    print(simple_message)
    # writing mode
    print("Write as binary")
    write()
    # reading
Example #5
0
import simple_pb2

PB_FILE_PATH = 'simple.pb'
JSON_FILE_PATH = 'simple.json'

simpleb = simple_pb2.SimpleMessage()
simpleb.id = 12345
simpleb.is_simple = True
simpleb.name = 'John'
simpleb.sample_list.extend([1, 4, 7, 8])


# 以二进制形式保存
f = open(PB_FILE_PATH, "wb")
f.write(simpleb.SerializeToString())
f.close()

# 读simpleb
simpleb = simple_pb2.SimpleMessage()
f = open(PB_FILE_PATH, "rb")
simpleb.ParseFromString(f.read())
f.close()
from google.protobuf.json_format import MessageToJson   # 关键
json = MessageToJson(simpleb)
print(json)

# 保存json
f = open(JSON_FILE_PATH, "w")
f.write(json)
f.close()
Example #6
0
import simple_pb2

'''
we shouldnt edit the proto buf created files
using reflection is the most important option in python

it is not like other languages like java and golang to create Classes

Descriptor is the metadata

the reflection is using dictionary to generate the type

'''
message = simple_pb2.SimpleMessage() # initiating an instance from SimpleMessage class
message.id = 10
message.is_simple = True
message.sample_list.append(1)
message.sample_list.append(2)
message.sample_list.append(3)
sample_list = message.sample_list

sample_list.append(10)

print(message)

with open("simple.bin", "wb") as f:
    bytesString = message.SerializeToString()
    f.write(bytesString)

with open("simple.bin", "rb") as f:
    simple_read = simple_pb2.SimpleMessage().FromString(f.read())