list_people.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #! /usr/bin/env python
  2. # See README.txt for information and build instructions.
  3. from __future__ import print_function
  4. import addressbook_pb2
  5. import sys
  6. # Iterates though all people in the AddressBook and prints info about them.
  7. def ListPeople(address_book):
  8. for person in address_book.people:
  9. print("Person ID:", person.id)
  10. print(" Name:", person.name)
  11. if person.email != "":
  12. print(" E-mail address:", person.email)
  13. for phone_number in person.phones:
  14. if phone_number.type == addressbook_pb2.Person.MOBILE:
  15. print(" Mobile phone #:", end=" ")
  16. elif phone_number.type == addressbook_pb2.Person.HOME:
  17. print(" Home phone #:", end=" ")
  18. elif phone_number.type == addressbook_pb2.Person.WORK:
  19. print(" Work phone #:", end=" ")
  20. print(phone_number.number)
  21. # Main procedure: Reads the entire address book from a file and prints all
  22. # the information inside.
  23. if len(sys.argv) != 2:
  24. print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
  25. sys.exit(-1)
  26. address_book = addressbook_pb2.AddressBook()
  27. # Read the existing address book.
  28. with open(sys.argv[1], "rb") as f:
  29. address_book.ParseFromString(f.read())
  30. ListPeople(address_book)