Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode protobuf map in python?

I am using protobuf and grpc as interface between a client and server. The server is written in C and the client uses python to communicate to the server.

I have a message created in protobuf like below.

message value_obj {
    uint32 code = 1;
    uint32 value = 2;
}

message list_of_maps {    
    map<uint32, value_obj> mapObj1 = 1;    
    map<uint32, value_obj> mapObj2 = 2; 
}

I tried creating objects in Python like below:

obj = list_of_maps()
mapObjToStore = value_obj()
mapObjToStore.code = 10
obj.mapObj1[1].CopyFrom(mapObjToStore)

When I try to receive the message in server, I get wrong values (huge numbers!). Any help on this will be greatly appreciated.

like image 714
Sidhu Avatar asked Jan 17 '18 04:01

Sidhu


1 Answers

You can try using python dictionary for that:

map1 = {}
obj1 = value_obj()
map1[1] =  obj1
map2 = {}
listOfMaps = list_of_maps(mapObj1=map1, mapObj2=map2)
like image 159
Viacheslav Shalamov Avatar answered Sep 28 '22 17:09

Viacheslav Shalamov