Given a proto definition
message EndpointResult {
int32 endpoint_id = 1;
// property id as key
map<int32, TimeSeries> properties = 2;
}
message TimeSeries {
repeated TimeEntry value = 2;
}
message TimeEntry {
int32 time_unit = 1;
float value = 2;
}
I wish to populate the map in the EndpointResult class. I have tried different approaches suggested in the docs but all raise a error for me.
Setting up a test class
end_point_rslt = nom.EndpointResult()
end_point_rslt.endpoint_id=0
ts = nom.TimeSeries()
te = ts.value.add()
te.time_unit = 0
te.value = 5.
Then trying the different approaches:
end_point_rslt.properties[0] = ts
ValueError: Direct assignment of submessage not allowed
end_point_rslt.properties[0].submessage_field = ts
AttributeError: Assignment not allowed (no field "submessage_field" in protocol message object).
end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties[0] = ts
ValueError: Direct assignment of submessage not allowed
end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties[0].submessage_field = ts
AttributeError: Assignment not allowed (no field "submessage_field" in protocol message object).
end_point_rslt.properties = {0 : ts}
AttributeError: Assignment not allowed to repeated field "properties" in protocol message object.
end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties = {0 : ts}
TypeError: Can't set composite field
Any example of how to use a protocol buffer map in python would be greatly appreciate!