I Get An Error "CMessage' Objects Doesn't Apply To A 'str' Object" When I Am Trying To Parse A Dict-structure With Repeating Elements
Can you help me understand why this is crashing: I'm trying to use the protobuf ParseDict function to turn a structure of Python objects into single Protocol Buffer object. As an e
Solution 1:
The google.protobuf.json_format.ParseDict function takes, as its second parameter, a message in which to merge the result. It then returns that message. In your example, you're giving it a type (MsgGrpType
) instead. If you want to merge the results into a frew new ("empty") message, you need to change MsgGrpType
to MsgGrpType()
, so that a new instance of the class gets passed to ParseDict
.
(The error message is pretty unfortunately obscure, though.)
The following fragment of Python would seem to work as expected:
from com_elexeon_boalf_pb2 import MsgGrpType
import datetime
from google.protobuf.json_format import MessageToDict, ParseDict
import pprint
msg0=MsgGrpType.MsgType(subject="test_subject0")
msg1=MsgGrpType.MsgType(subject="test_subject1")
msg2=MsgGrpType.MsgType(subject="test_subject3")
msgGrp = MsgGrpType(flow="BOALF", msg=[msg0,msg1,msg2], pub_ts=datetime.datetime.now().isoformat())
msgDict = MessageToDict(msgGrp)
result = ParseDict(msgDict, MsgGrpType(), ignore_unknown_fields=True)
pprint.pprint(result)
Producing the following output when executed:
pub_ts: "2021-08-08T01:15:44.232683"
flow: "BOALF"
msg {
subject: "test_subject0"
}
msg {
subject: "test_subject1"
}
msg {
subject: "test_subject3"
}
Post a Comment for "I Get An Error "CMessage' Objects Doesn't Apply To A 'str' Object" When I Am Trying To Parse A Dict-structure With Repeating Elements"