python protobuf cant deserialize message

2024/9/16 23:36:11

Getting started with protobuf in python I face a strange issue:

a simple message proto definition is:

syntax = "proto3";
package test;message Message {string message = 1;string sender = 2;
}

generated via protoc -I . --python_out=generated message.proto and accessed in Python like:

from generated.message_pb2 import Message

Then I can construct a message

m = Message()
m.sender = 'foo'
m.message = 'bar'print(str(m))

but de-serializing will not return a result

s_m = m.SerializeToString()
print(s_m) # prints fine
a = m.ParseFromString(s_m)
a.foo #fails with error - no attributes deserialized
Answer

Instead of

a = m.ParseFromString(s_m)
a.foo

do this

a = m.FromString(s_m)
print a.sender

alternatively you can do this

m2 = Message()
m2.ParseFromString(s_m)
print m2.sender

The difference is that FromString returns a new object deserialized from the string whereas ParseFromString parses the string and sets the fields on the object.

https://en.xdnf.cn/q/73044.html

Related Q&A

Seaborn: title and subtitle placement

H all,Id like to create a scatterplot with a title, subtitle, colours corresponding to a specific variable and size corresponding to another variable. I want to display the colour legend but not the si…

Calculate a rolling regression in Pandas and store the slope

I have some time series data and I want to calculate a groupwise rolling regression of the last n days in Pandas and store the slope of that regression in a new column.I searched the older questions an…

Python read microphone

I am trying to make python grab data from my microphone, as I want to make a random generator which will use noise from it. So basically I dont want to record the sounds, but rather read it in as a da…

How to tell pytest-xdist to run tests from one folder sequencially and the rest in parallel?

Imagine that I have test/unit/... which are safe to run in parallel and test/functional/... which cannot be run in parallel yet.Is there an easy way to convince pytest to run the functional ones sequen…

PyPDF4 - Exported PDF file size too big

I have a PDF file of around 7000 pages and 479 MB. I have create a python script using PyPDF4 to extract only specific pages if the pages contain specific words. The script works but the new PDF file,…

Jupyter install fails on Mac

Im trying to install Jupyter on my Mac (OS X El Capitan) and Im getting an error in response to:sudo pip install -U jupyterAt first the download/install starts fine, but then I run into this:Installing…

Python Error Codes are upshifted

Consider a python script error.pyimport sys sys.exit(3)Invokingpython error.py; echo $?yields the expected "3". However, consider runner.pyimport os result = os.system("python error.py&…

Running dozens of Scrapy spiders in a controlled manner

Im trying to build a system to run a few dozen Scrapy spiders, save the results to S3, and let me know when it finishes. There are several similar questions on StackOverflow (e.g. this one and this oth…

How to merge two DataFrame columns and apply pandas.to_datetime to it?

Im learning to use pandas, to use it for some data analysis. The data is supplied as a csv file, with several columns, of which i only need to use 4 (date, time, o, c). Ill like to create a new DataFr…

Breaking a parent function from within a child function (PHP Preferrably)

I was challenged how to break or end execution of a parent function without modifying the code of the parent, using PHPI cannot figure out any solution, other than die(); in the child, which would end …