protobuf/examples/list_people.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # See README.txt for information and build instructions.
4 
5 from __future__ import print_function
6 import addressbook_pb2
7 import sys
8 
9 
10 # Iterates though all people in the AddressBook and prints info about them.
11 def ListPeople(address_book):
12  for person in address_book.people:
13  print("Person ID:", person.id)
14  print(" Name:", person.name)
15  if person.email != "":
16  print(" E-mail address:", person.email)
17 
18  for phone_number in person.phones:
19  if phone_number.type == addressbook_pb2.Person.MOBILE:
20  print(" Mobile phone #:", end=" ")
21  elif phone_number.type == addressbook_pb2.Person.HOME:
22  print(" Home phone #:", end=" ")
23  elif phone_number.type == addressbook_pb2.Person.WORK:
24  print(" Work phone #:", end=" ")
25  print(phone_number.number)
26 
27 
28 # Main procedure: Reads the entire address book from a file and prints all
29 # the information inside.
30 if len(sys.argv) != 2:
31  print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
32  sys.exit(-1)
33 
34 address_book = addressbook_pb2.AddressBook()
35 
36 # Read the existing address book.
37 with open(sys.argv[1], "rb") as f:
38  address_book.ParseFromString(f.read())
39 
40 ListPeople(address_book)
list_people.ListPeople
def ListPeople(address_book)
Definition: bloaty/third_party/protobuf/examples/list_people.py:11
open
#define open
Definition: test-fs.c:46
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:15