@@ -326,62 +326,60 @@ the file again. The parts which directly call or reference code generated by the
326326protocol compiler are highlighted.
327327
328328```python
329- #! /usr/bin/python
329+ #!/usr/bin/env python3
330330
331331import addressbook_pb2
332332import sys
333333
334334# This function fills in a Person message based on user input.
335335def PromptForAddress(person):
336- person.id = int(raw_input ("Enter person ID number: "))
337- person.name = raw_input ("Enter name: ")
336+ person.id = int(input ("Enter person ID number: "))
337+ person.name = input ("Enter name: ")
338338
339- email = raw_input ("Enter email address (blank for none): ")
339+ email = input ("Enter email address (blank for none): ")
340340 if email != "":
341341 person.email = email
342342
343343 while True:
344- number = raw_input ("Enter a phone number (or leave blank to finish): ")
344+ number = input ("Enter a phone number (or leave blank to finish): ")
345345 if number == "":
346346 break
347347
348348 phone_number = person.phones.add()
349349 phone_number.number = number
350350
351- type = raw_input ("Is this a mobile, home, or work phone? ")
352- if type == "mobile":
351+ phone_type = input ("Is this a mobile, home, or work phone? ")
352+ if phone_type == "mobile":
353353 phone_number.type = addressbook_pb2.Person.PhoneType.MOBILE
354- elif type == "home":
354+ elif phone_type == "home":
355355 phone_number.type = addressbook_pb2.Person.PhoneType.HOME
356- elif type == "work":
356+ elif phone_type == "work":
357357 phone_number.type = addressbook_pb2.Person.PhoneType.WORK
358358 else:
359- print "Unknown phone type; leaving as default value."
359+ print( "Unknown phone type; leaving as default value.")
360360
361361# Main procedure: Reads the entire address book from a file,
362362# adds one person based on user input, then writes it back out to the same
363363# file.
364364if len(sys.argv) != 2:
365- print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
365+ print( "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
366366 sys.exit(-1)
367367
368368address_book = addressbook_pb2.AddressBook()
369369
370370# Read the existing address book.
371371try:
372- f = open(sys.argv[1], "rb")
373- address_book.ParseFromString(f.read())
374- f.close()
372+ with open(sys.argv[1], "rb") as f:
373+ address_book.ParseFromString(f.read())
375374except IOError:
376- print sys.argv[1] + ": Could not open file. Creating a new one."
375+ print( sys.argv[1] + ": Could not open file. Creating a new one.")
377376
378377# Add an address.
379378PromptForAddress(address_book.people.add())
380379
381380# Write the new address book back to disk.
382- f = open(sys.argv[1], "wb")
383- f.write(address_book.SerializeToString())
384- f.close()
381+ with open(sys.argv[1], "wb") as f:
382+ f.write(address_book.SerializeToString())
385383```
386384
387385## Reading a Message {#reading-a-message}
@@ -391,40 +389,39 @@ information out of it! This example reads the file created by the above example
391389and prints all the information in it.
392390
393391```python
394- #! /usr/bin/python
392+ #!/usr/bin/env python3
395393
396394import addressbook_pb2
397395import sys
398396
399397# Iterates though all people in the AddressBook and prints info about them.
400398def ListPeople(address_book):
401399 for person in address_book.people:
402- print "Person ID:", person.id
403- print " Name:", person.name
400+ print( "Person ID:", person.id)
401+ print( " Name:", person.name)
404402 if person.HasField(' email' ):
405- print " E-mail address:", person.email
403+ print( " E-mail address:", person.email)
406404
407405 for phone_number in person.phones:
408406 if phone_number.type == addressbook_pb2.Person.PhoneType.MOBILE:
409- print " Mobile phone #: ",
407+ print( " Mobile phone #: ", end="")
410408 elif phone_number.type == addressbook_pb2.Person.PhoneType.HOME:
411- print " Home phone #: ",
409+ print( " Home phone #: ", end="")
412410 elif phone_number.type == addressbook_pb2.Person.PhoneType.WORK:
413- print " Work phone #: ",
414- print phone_number.number
411+ print( " Work phone #: ", end="")
412+ print( phone_number.number)
415413
416414# Main procedure: Reads the entire address book from a file and prints all
417415# the information inside.
418416if len(sys.argv) != 2:
419- print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
417+ print( "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
420418 sys.exit(-1)
421419
422420address_book = addressbook_pb2.AddressBook()
423421
424422# Read the existing address book.
425- f = open(sys.argv[1], "rb")
426- address_book.ParseFromString(f.read())
427- f.close()
423+ with open(sys.argv[1], "rb") as f:
424+ address_book.ParseFromString(f.read())
428425
429426ListPeople(address_book)
430427```
0 commit comments