def main(argv): """Returns an exit code.""" b = addressbook_pb2.AddressBook() p = b.person.add() p.name = 'yo' print p q = b.person.add() q.name = 'bar' print q for person in b.person: print type(person) print person t = en_pb2.Test1() t.s.append('foo') t.s.append('bar') print t for element in t.s: print type(element) print dir(element) print element bytes = t.SerializeToString() print 'BYTES', repr(bytes) print 't.s', type(t.s) for element in t.s: print type(element) #print dir(element) print element #pprint(sys.modules) return 0
def main(): print("start!!") address_book = addressbook_pb2.AddressBook() person1 = address_book.people.add() person1.id = 1234 person1.name = "John Doe" person1.email = "*****@*****.**" # person1.no_such_field = 1 # raises AttributeError # person1.id = "1234" # raises TypeError phone = person1.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME person2 = address_book.people.add() person2.id = 4321 person2.name = "Tom Ranger" person2.email = "*****@*****.**" phone = person2.phones.add() phone.number = "555-4322" phone.type = addressbook_pb2.Person.WORK print(address_book) # Human readable print # writing the data f = open("pbdata_py.dat", "wb") f.write(address_book.SerializeToString()) f.close() print("done!!")
def main(): addressBook = get_sample_AddressBook() with io.BytesIO() as f: f.write(addressBook.SerializeToString()) addressBook = addressbook_pb2.AddressBook() addressBook.ParseFromString(f.getvalue()) print(addressBook)
def Request(self, request, context): """ :param request: :param context: :return: """ print(request) print(context) address_book = addressbook_pb2.AddressBook() person1 = address_book.people.add() person1.id = 1234 person1.name = "John Doe" person1.email = "*****@*****.**" phone = person1.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME person2 = address_book.people.add() person2.id = 4321 person2.name = "Tom Ranger" person2.email = "*****@*****.**" phone = person2.phones.add() phone.number = "555-4322" phone.type = addressbook_pb2.Person.WORK print(address_book) # Human readable print return address_book
def printAddressBook(message_string): addressBook = addressbook.AddressBook() addressBook.ParseFromString(message_string) for person in addressBook.person: print(person.name, ':', person.email) for phone in person.phone: print(phone.type, ':', phone.number) print()
async def run(loop): address_book = addressbook_pb2.AddressBook() AddPerson1(address_book.people.add()) AddPerson2(address_book.people.add()) nc = NATS() await nc.connect("127.0.0.1:4222", loop=loop) await nc.publish("test_topic", address_book.SerializeToString()) await nc.close()
def get_address_book(): address_book = addressbook_pb2.AddressBook() person = address_book.people.add() person.id = 0 person.name = "example" person.email = "*****@*****.**" phone_number = person.phones.add() phone_number.number = "123456789" phone_number.type = addressbook_pb2.Person.MOBILE return address_book
def hello(): address_book = addressbook_pb2.AddressBook() address_book.ParseFromString(request.data) print(address_book) return send_file(io.BytesIO(address_book.SerializeToString()), as_attachment=True, attachment_filename='abc.abc', mimetype='attachment/x-protobuf')
def index(): print("RAW DATA: ") print(request.get_data()) address_book = addressbook_pb2.AddressBook() address_book.ParseFromString(request.get_data()) print("PRETTY DATA:") print(address_book) return request.get_data()
def printAddressBook(f): addresses = addressbook_pb2.AddressBook() s1 = datetime.now() addresses.ParseFromString(f.read()) s2 = datetime.now() print("Time to read: {}".format(s2 - s1)) for person in addresses.people: print(person.name, ':', person.email) for phone in person.phones: print(phone.type, ':', phone.number)
def get_test_data(): # Set up test data address_book = addressbook_pb2.AddressBook() person = address_book.people.add() person.id = 100 person.name = "First Last" person.email = "*****@*****.**" phone_number = person.phones.add() phone_number.number = "999-999-9999" phone_number.type = addressbook_pb2.Person.HOME # Serialize and return return address_book.SerializeToString()
def main(): address_book = addressbook_pb2.AddressBook() person = address_book.people.add() person.id = 1234 person.name = "John Doe" person.email = "*****@*****.**" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.PhoneType.HOME url = "http://localhost:5000" response = r.post(url, address_book.SerializeToString()) print("STATUS CODE: %d" % response.status_code) print("RESPONSE : \n" + response.text)
def get_protobuf_person(id: int) -> str: import addressbook_pb2 with open('resources/addressbook_pb', "rb") as f: address_book = addressbook_pb2.AddressBook() address_book.ParseFromString(f.read()) person = None for per in address_book.people: if per.id == id: break person_ser = person.SerializePartialToString() if person else b'' resp = app.make_response(person_ser) resp.mimetype = "application/x-protobuf" return resp
def main(): """ main func """ print("start!!") address_book = addressbook_pb2.AddressBook() # Read the existing address book. file_path = open("pbdata_py.dat", "rb") # read # file_path = open("../../sample1/python/pbdata_py.dat", # "rb") # read old data address_book.ParseFromString(file_path.read()) file_path.close() print(address_book) print("done!!")
def makeAddressBook(n): addressbook = addressbook_pb2.AddressBook() for i in xrange(n): person = addressbook.people.add() person.id = i person.name = uuid4().get_hex() person.email = person.name + '@abc.com' phone1 = person.phones.add() phone1.number = str(randint(1000000000, 7999999999)) phone1.type = addressbook_pb2.Person.HOME phone2 = person.phones.add() phone2.number = str(randint(1000000000, 7999999999)) phone2.type = addressbook_pb2.Person.MOBILE return addressbook
def run_until_save(): """ The main loop of this application. Runs until user selects to save current AddressBook instance to save file. """ addressbook = addressbook_pb2.AddressBook() read_address_book(addressbook) print(str(addressbook)) while True: print("Choose option:\n[1] - to enter new person data\n[2]- to save address book\n") option = int(input('[???]')) if option == 1: enter_person_data(addressbook.people.add()) elif option == 2: print(str(addressbook)) with open(save_addressbook_file, 'wb') as sf: sf.write(addressbook.SerializeToString()) break
def make_address_book_entry(): ab = addressbook_pb2.AddressBook() p1 = addressbook_pb2.Person() p1.name = 'rahul' p1.id = 1 p1.email = '*****@*****.**' phones = p1.phones phones.append( addressbook_pb2.Person.PhoneNumber(number='1234567890', type=addressbook_pb2.Person.MOBILE)) print(p1) ab.people.extend([p1]) print(ab) p2 = ab.people.add() p2.name = 'simran' p2.id = 2 p2.email = '*****@*****.**' p2.phones.add(number='0987654321', type=addressbook_pb2.Person.WORK) print(ab)
def main(): """ main func """ print("start!!") address_book = addressbook_pb2.AddressBook() # Read the existing address book. # file_path = open("pbdata_py.dat", "rb") # read data from python code # file_path = open("../cpp/pbdata_cpp.dat", "rb") # read data from cpp code # file_path = open("../julia/pbdata_julia.dat","rb") # read data from cpp code file_path = open("../java/pbdata_java.dat", "rb") # read data from java code address_book.ParseFromString(file_path.read()) file_path.close() print(address_book) print("done!!")
def get_protobuf_data(): """Need to protobuf-ize the data""" pb_contacts = [] for contact in contacts: address = addressbook_pb2.Address() if contact['address'].get('address_lines'): address.address_lines.extend(contact['address']['address_lines']) if 'postcode' in contact['address']: address.postcode = contact['address']['postcode'] pb_contacts.append( addressbook_pb2.Contact( first_name=contact['first_name'], last_name=contact['last_name'], address=address, phone_numbers=[ addressbook_pb2.Phone( type=addressbook_pb2.MOBILE if num['type'] == 'MOBILE' else addressbook_pb2.LANDLINE, number=num['number']) for num in contact['phone_numbers'] ])) return addressbook_pb2.AddressBook(contacts=pb_contacts)
def writeAddressBook(): addressBook = addressbook.AddressBook() alice = addressBook.person.add() alice.id = 123 alice.name = 'Alice' alice.email = '*****@*****.**' alicePhones = [alice.phone.add()] alicePhones[0].number = "555-1212" alicePhones[0].type = addressbook.Person.MOBILE bob = addressBook.person.add() bob.id = 456 bob.name = 'Bob' bob.email = '*****@*****.**' bobPhones = [bob.phone.add(), bob.phone.add()] bobPhones[0].number = "555-4567" bobPhones[0].type = addressbook.Person.HOME bobPhones[1].number = "555-7654" bobPhones[1].type = addressbook.Person.WORK message_string = addressBook.SerializeToString() return message_string
async def message_handler(msg): address_book = addressbook_pb2.AddressBook() address_book.ParseFromString(msg.data) ListPeople(address_book)
for person in address_book.people: print("Person ID:", person.id) print(" Name:", person.name) if person.HasField('email'): print(" E-mail address:", person.email) for phone_number in person.phones: 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)
return addressbook.SerializeToString() def ReadFromAB(addressbook): i = 1 for people in addressbook.people: print('\n====== {} ======'.format(i)) print('Person ID: ', people.id) print('Person Name: ', people.name) print('Person Email: ', people.email) print('Person PhoneNumber:') for phone in people.phones: if phone.type == addressbook_pb2.Person.MOBILE: print('\t type:Mobile number:{}'.format(phone.number)) elif phone.type == addressbook_pb2.Person.HOME: print('\t type:Home number:{}'.format(phone.number)) if phone.type == addressbook_pb2.Person.WORK: print('\t type:work number:{}'.format(phone.number)) i += 1 if __name__ == "__main__": ab = addressbook_pb2.AddressBook() abString = Write2AB(ab) print(abString) # from string to protobuf ab.ParseFromString(abString) ReadFromAB(ab)
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 addressbook = addressbook_pb2.AddressBook() person = addressbook.person.add() person.id = 1234 person.name = "John Doe" person.email = "*****@*****.**" phone = person.phone.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME phone = person.phone.add() phone.number = "555-4322" phone.type = addressbook_pb2.Person.WORK print addressbook
def create_AddressBook(personList): addressBook = addressbook_pb2.AddressBook() for person in personList: addressBook.people.append(person) return addressBook
''' Created on 12 Apr 2017 @author: mozat ''' import addressbook_pb2 as ad addbook = ad.AddressBook() person = addbook.person.add() phone1 = person.phone.add() phone1.number = "90082418" phone1.type = ad.Person.MOBILE phone2 = person.phone.add() phone2.number = "90082418" phone2.type = ad.Person.HOME print len(person.phone) print person.phone print addbook
for phone_count in range(0, 2): pphone = p.phone.add() jphone = {} j['phone'].append(jphone) pphone.number = jphone['number'] = c['%d-number' % phone_count] = ''.join( random.choice('0123456789') for x in range(0, 10)) pphone.type = jphone['type'] = c['%d-type' % phone_count] = random.choice( [0, 1, 2]) return j, c addressbook, jsonseq, csvseq = addressbook_pb2.AddressBook(), [], [] for x in range(0, 100000): p = addressbook.person.add() j, c = person(p) jsonseq.append(j) csvseq.append(c) # How long does it take to write protobufs? t0 = time.time() open('output.json', 'w').write(json.dumps(jsonseq, separators=(',', ':'))) t1 = time.time() open('output.pb', 'wb').write(addressbook.SerializeToString()) t2 = time.time() gzip.open('output.jsz', 'wb').write(json.dumps(jsonseq, separators=(',', ':'))) t3 = time.time() out = csv.DictWriter(gzip.open('output.csvz', 'wb'), csvseq[0].keys())
def deserialize(str): addressbook = addressbook_pb2.AddressBook() addressbook.ParseFromString(str) return addressbook
def __init__(self): self.db = addressbook_pb2.AddressBook()
import addressbook_pb2 as addressbook_pb2 addressYellowBook = addressbook_pb2.AddressBook() addressYellowBook.people.add(name="marian kovalczyk", id=234, email="*****@*****.**") #franekD = addressbook_pb2.Person() franekD = addressYellowBook.people.add() franekD.name="franek Dolas" franekD.id=3434 franekD.email="frade@fgeewe" franekPhone = franekD.phones.add() franekPhone.number = "2345453442342" franekPhone.type = franekD.WORK # extend list of phones for another phone def extendPhones(num, typ): franekPhone2 = addressbook_pb2.Person().PhoneNumber() franekPhone2.number = num if typ == "HOME": franekPhone2.type = franekD.HOME else: franekPhone2.type = franekD.WORK franekD.phones.extend([franekPhone2])