I made a Message.proto
file and compiled it to generate python file, named message_pb2
.
Then in a scenario, I wanted to put my message objects into a set, in order to make them unique.
But the __hash__
function of protobuf message raise a TypeError, then I think I can implement and override this function in my code.
I made a new class that inherits message_pb2.Message()
, but when running code, I got another error:
KeyError: 'DESCRIPTOR'
Now I don't have any other idea!
I made some changes and use below code, and it worked:
import hashlib
from myproto_pb2 import MyMessage # Import your generated protobuf message class
class HashableMessage:
def __init__(self, message):
self._message = message
def __hash__(self):
# Define the fields to include in the hash calculation
fields_to_hash = [self._message.field1, self._message.field2, self._message.field3]
# Convert the field values to bytes and concatenate them
data = b"".join(str(field).encode() for field in fields_to_hash)
# Calculate the hash value using SHA-256
hash_value = hashlib.sha256(data).hexdigest()
return int(hash_value, 16) # Convert the hex hash to an integer
def __eq__(self, other):
return isinstance(other, HashableMessage) and self._message == other._message
# Create a set to store protobuf messages
message_set = set()
# Create an instance of the protobuf message
protobuf_message1 = MyMessage()
protobuf_message1.field1 = "value1"
protobuf_message1.field2 = 42
protobuf_message1.field3 = True
message_set.add(HashableMessage(protobuf_message1))