list_people.py 1.1 KB

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