-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_protobuf.py
executable file
·37 lines (29 loc) · 1.31 KB
/
example_protobuf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from kv_db_interface import KeyValueDatabaseInterface
import annoucement_pb2 as announcement_message
def main():
kv_db = KeyValueDatabaseInterface(connection_string="sqlite:///proto_buf.db")
message_to_serialize = announcement_message.Annoucement()
message_to_serialize.sender = "Mikey"
message_to_serialize.recipients.extend(['Joey', 'Sammy'])
message_to_serialize.message = "S.O.S."
print("The following the printed Protbuf object:")
print(message_to_serialize)
print("This is how it showed up serialized:")
print(message_to_serialize.SerializeToString())
print("Inserting the message...")
kv_db.insert("message1", message_to_serialize)
print("Retrieving the message...")
serialized_message_from_db = kv_db.get("message1").value
print("This is how it looks like in after it is retrieve from the database:")
print(serialized_message_from_db)
print("Deserializing...")
deserialized_object = announcement_message.Annoucement()
deserialized_object.ParseFromString(serialized_message_from_db)
print("Done. This is the deserialized message from the database:")
print(deserialized_object)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Program ended by user.")
exit(0)