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
635ce854
Commit
635ce854
authored
Feb 11, 2016
by
Joshua Haberman
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1179 from ParthKolekar/python-examples-with
Updated python examples to use with.
parents
d2c7fe6b
21e1f1d2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
9 deletions
+6
-9
add_person.py
examples/add_person.py
+4
-6
list_people.py
examples/list_people.py
+2
-3
No files found.
examples/add_person.py
View file @
635ce854
...
...
@@ -43,9 +43,8 @@ address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
try
:
f
=
open
(
sys
.
argv
[
1
],
"rb"
)
address_book
.
ParseFromString
(
f
.
read
())
f
.
close
()
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."
...
...
@@ -53,6 +52,5 @@ except IOError:
PromptForAddress
(
address_book
.
people
.
add
())
# Write the new address book back to disk.
f
=
open
(
sys
.
argv
[
1
],
"wb"
)
f
.
write
(
address_book
.
SerializeToString
())
f
.
close
()
with
open
(
sys
.
argv
[
1
],
"wb"
)
as
f
:
f
.
write
(
address_book
.
SerializeToString
())
examples/list_people.py
View file @
635ce854
...
...
@@ -31,8 +31,7 @@ if len(sys.argv) != 2:
address_book
=
addressbook_pb2
.
AddressBook
()
# Read the existing address book.
f
=
open
(
sys
.
argv
[
1
],
"rb"
)
address_book
.
ParseFromString
(
f
.
read
())
f
.
close
()
with
open
(
sys
.
argv
[
1
],
"rb"
)
as
f
:
address_book
.
ParseFromString
(
f
.
read
())
ListPeople
(
address_book
)
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