Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Last active August 29, 2015 14:09
Show Gist options
  • Save hyunjun/82055f45b9c066c141a4 to your computer and use it in GitHub Desktop.
Save hyunjun/82055f45b9c066c141a4 to your computer and use it in GitHub Desktop.
protocol-buffer
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
message AddressBook {
repeated Person person = 1;
}

tutorial

  • https://developers.google.com/protocol-buffers/docs/pythontutorial?hl=ko
    • writer.py 한글 사용을 위해 person.name 입력 시 unicode 사용
    • reader.py, addressbook.proto 그대로 사용
    $ protoc -I=./ --python_out=./ ./addressbook.proto
    $ python writer.py addressbook.file
    $ python2.7 reader.py addressbook.file
    Person ID: 1
      Name: haha
      E-mail address: seoul
      Mobile phone #:  123
      Home phone #:  234
      Work phone #:  333
    ...
    

installation

  • 최신은 현재 2.6.1이나 CentOS 6.3에서 yum으로 기본 설치되는 건 2.3.0
    • yum install protobuf-devel python-protobuf
# -*- coding: utf8 -*-
import addressbook_pb2
import sys
# Iterates though all people in the AddressBook and prints info about them.
def ListPeople(address_book):
for person in address_book.person:
print "Person ID:", person.id
print " Name:", person.name
if person.HasField('email'):
print " E-mail address:", person.email
for phone_number in person.phone:
if phone_number.type == addressbook_pb2.Person.MOBILE:
print " Mobile phone #: ",
elif phone_number.type == addressbook_pb2.Person.HOME:
print " Home phone #: ",
elif phone_number.type == addressbook_pb2.Person.WORK:
print " Work phone #: ",
print phone_number.number
# Main procedure: Reads the entire address book from a file and prints all
# the information inside.
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
ListPeople(address_book)
# -*- coding: utf8 -*-
import addressbook_pb2
import sys
# This function fills in a Person message based on user input.
def PromptForAddress(person, id=None, name=None, email=None):
if id is None:
person.id = int(raw_input("Enter person ID number: "))
else:
person.id = id
if name is None:
person.name = unicode(raw_input("Enter name: "))
else:
person.name = name
if email is None:
email = raw_input("Enter email address (blank for none): ")
if email != "":
person.email = email
'''while True:
number = raw_input("Enter a phone number (or leave blank to finish): ")
if number == "":
break
phone_number = person.phone.add()
phone_number.number = number
type = raw_input("Is this a mobile, home, or work phone? ")
if type == "mobile":
phone_number.type = addressbook_pb2.Person.MOBILE
elif type == "home":
phone_number.type = addressbook_pb2.Person.HOME
elif type == "work":
phone_number.type = addressbook_pb2.Person.WORK
else:
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"
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
try:
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
except IOError:
print sys.argv[1] + ": Could not open file. Creating a new one."
# Add an address.
PromptForAddress(address_book.person.add(), 555, u'하하하', '[email protected]')
# Write the new address book back to disk.
f = open(sys.argv[1], "wb")
f.write(address_book.SerializeToString())
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment