A hashable key for a proto message capturing its type and content. Messages of the same type (we use the short type name here, e.g. AttrSpec) that serialize to the same byte string are considered the same.
| 75 | |
| 76 | |
| 77 | class MessageKey(object): |
| 78 | """A hashable key for a proto message capturing its type and content. |
| 79 | |
| 80 | Messages of the same type (we use the short type name here, e.g. AttrSpec) |
| 81 | that serialize to the same byte string are considered the same. |
| 82 | """ |
| 83 | |
| 84 | def __init__(self, proto_message): |
| 85 | self.type_name = proto_message.DESCRIPTOR.name |
| 86 | # While it's not strictly necessary to use a digest here, we do so |
| 87 | # to avoid carrying around the whole serialized string all the time. |
| 88 | self.digest = hashlib.sha1(proto_message.SerializeToString()).hexdigest() |
| 89 | |
| 90 | def __hash__(self): |
| 91 | return hash((self.type_name, self.digest)) |
| 92 | |
| 93 | def __eq__(self, other): |
| 94 | return (self.type_name, self.digest) == (other.type_name, other.digest) |
| 95 | |
| 96 | def __ne__(self, other): |
| 97 | return not self == other |
| 98 | |
| 99 | |
| 100 | class MessageRegistry(object): |
no outgoing calls
no test coverage detected