diff --git a/README.md b/README.md index 7bc2d32..02bed9f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Lei Mao Protocol Buffer, also known as Protobuf, is Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. Protocol Buffers Version 3 (Proto3) was released in 2016. However, the official [tutorials](https://developers.google.com/protocol-buffers/docs/cpptutorial) on the Google protobuf website are still for Proto2. The official [samples](https://github.com/protocolbuffers/protobuf/tree/master/examples) are using Proto3 and CMake, however, its CMake is not standard and many of its macros were not documented on the official CMake website. -In this repository, I re-implemented Google's official Protobuf C++ and Python example using Proto3 and CMake with the best practice. I also put code comments as many as possible to make sure the code is human readable and easy to understand. +In this repository, I re-implemented Google's official Protobuf C++ and Python example using Proto3 and CMake with the best practice. I also put code comments as many as possible to make sure the code is human readable and easy to understand. ## Dependencies @@ -60,7 +60,7 @@ All the executable files would be generated in `build/bin` directory. ### Run Examples -The C++ and Python serialization and deserialization can be used interchangeably (C++ serialization + C++ deserialization, C++ serialization + Python deserialization, Python serialization + Python deserialization, Python serialization + C++ deserialization), as the data formats are all generated from the same `proto` file and they are all the same. +The C++ and Python serialization and deserialization can be used interchangeably (C++ serialization + C++ deserialization, C++ serialization + Python deserialization, Python serialization + Python deserialization, Python serialization + C++ deserialization), as the data formats are all generated from the same `proto` file and they are all the same. #### Add People Using C++ @@ -69,27 +69,27 @@ $ ./build/bin/add_people build/data.pb # Add people to data build/data.pb: File not found. Creating a new file. Add person to address book? yes | no yes -Enter person ID number: +Enter person ID number: 12345 -Enter name: +Enter name: Lei Mao -Enter email address (blank for none): +Enter email address (blank for none): dukeleimao@gmail.com -Enter a phone number (or leave blank to finish): +Enter a phone number (or leave blank to finish): +1123456789 -Is this a mobile, home, or work phone? mobile | home | work +Is this a mobile, home, or work phone? mobile | home | work home -Enter a phone number (or leave blank to finish): +Enter a phone number (or leave blank to finish): Add person to address book? yes | no yes -Enter person ID number: +Enter person ID number: 54321 -Enter name: +Enter name: Andrew Ng -Enter email address (blank for none): +Enter email address (blank for none): andrew.ng@stanford.edu -Enter a phone number (or leave blank to finish): +Enter a phone number (or leave blank to finish): Add person to address book? yes | no no @@ -98,7 +98,7 @@ no #### List People Using C++ ```bash -$ python examples-python/add_people.py build/data.pb # Read data +$ ./build/bin/list_people build/data.pb # Read data Person ID: 12345 Name: Lei Mao E-mail address: dukeleimao@gmail.com @@ -113,7 +113,7 @@ Person ID: 54321 #### Add People Using Python ```bash -$ python examples-python/list_people.py build/data.pb +$ python examples-python/add_people.py build/data.pb build/data.pb: File not found. Creating a new file. Add person to address book? yes | no yes @@ -122,13 +122,13 @@ Enter name: Lei Mao Enter email address (blank for none): dukeleimao@gmail.com Enter a phone number (or leave blank to finish): +1123456789 Is this a mobile, home, or work phone? home -Enter a phone number (or leave blank to finish): +Enter a phone number (or leave blank to finish): Add person to address book? yes | no yes Enter person ID number: 54321 Enter name: Andrew Ng Enter email address (blank for none): andrew.ng@stanford.edu -Enter a phone number (or leave blank to finish): +Enter a phone number (or leave blank to finish): Add person to address book? yes | no no ``` @@ -136,7 +136,7 @@ no #### List People Using Python ```bash -$ python examples-python/list_people.py build/data.pb +$ python examples-python/list_people.py build/data.pb Person ID: 12345 Name: Lei Mao E-mail address: dukeleimao@gmail.com diff --git a/examples-python/add_people.py b/examples-python/add_people.py index 3af8852..cfbba23 100644 --- a/examples-python/add_people.py +++ b/examples-python/add_people.py @@ -2,6 +2,8 @@ import sys sys.path.append("build/protos") import addressbook_pb2 +from time import time +from addressbook_pb2 import google_dot_protobuf_dot_timestamp__pb2 as timestamp try: raw_input # Python 2 @@ -36,6 +38,8 @@ def PromptForAddress(person): else: print("Unknown phone type; leaving as default value.") + person.last_updated.CopyFrom(timestamp.Timestamp(seconds=int(time()))) + # Main procedure: Reads the entire address book from a file, # adds one person based on user input, then writes it back out to the same diff --git a/examples-python/list_people.py b/examples-python/list_people.py index 4700e50..ea23677 100644 --- a/examples-python/list_people.py +++ b/examples-python/list_people.py @@ -3,7 +3,7 @@ import sys sys.path.append("build/protos") import addressbook_pb2 - +from datetime import datetime # Iterates though all people in the AddressBook and prints info about them. def ListPeople(address_book): @@ -22,6 +22,9 @@ def ListPeople(address_book): print(" Work phone #:", end=" ") print(phone_number.number) + last_update = person.last_updated + if last_update.seconds != 0: + print(" Updated", datetime.fromtimestamp(last_update.seconds)) # Main procedure: Reads the entire address book from a file and prints all # the information inside.