Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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++

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -122,21 +122,21 @@ 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
```

#### 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
Expand Down
4 changes: 4 additions & 0 deletions examples-python/add_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion examples-python/list_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down