Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
7daedbd8
Commit
7daedbd8
authored
Aug 17, 2017
by
cclauss
Committed by
GitHub
Aug 17, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
print() function & define raw_input() for Python 3
parent
45483fd1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
3 deletions
+10
-3
add_person.py
examples/add_person.py
+10
-3
No files found.
examples/add_person.py
View file @
7daedbd8
...
...
@@ -5,6 +5,12 @@
import
addressbook_pb2
import
sys
try
:
raw_input
# Python 2
except
NameError
:
raw_input
=
input
# Python 3
# This function fills in a Person message based on user input.
def
PromptForAddress
(
person
):
person
.
id
=
int
(
raw_input
(
"Enter person ID number: "
))
...
...
@@ -30,13 +36,14 @@ def PromptForAddress(person):
elif
type
==
"work"
:
phone_number
.
type
=
addressbook_pb2
.
Person
.
WORK
else
:
print
"Unknown phone type; leaving as default value."
print
(
"Unknown phone type; leaving as default value."
)
# 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
# file.
if
len
(
sys
.
argv
)
!=
2
:
print
"Usage:"
,
sys
.
argv
[
0
],
"ADDRESS_BOOK_FILE"
print
(
"Usage:"
,
sys
.
argv
[
0
],
"ADDRESS_BOOK_FILE"
)
sys
.
exit
(
-
1
)
address_book
=
addressbook_pb2
.
AddressBook
()
...
...
@@ -46,7 +53,7 @@ try:
with
open
(
sys
.
argv
[
1
],
"rb"
)
as
f
:
address_book
.
ParseFromString
(
f
.
read
())
except
IOError
:
print
sys
.
argv
[
1
]
+
": File not found. Creating a new file."
print
(
sys
.
argv
[
1
]
+
": File not found. Creating a new file."
)
# Add an address.
PromptForAddress
(
address_book
.
people
.
add
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment