Khung xác thực dữ liệu python
GHI CHÚ. Trang web này không được dùng nữa. Trang web sẽ ngừng hoạt động sau ngày 31 tháng 1 năm 2023 và lưu lượng truy cập sẽ chuyển hướng đến trang web mới tại https. // protobuf. nhà phát triển. Trong thời gian chờ đợi, các bản cập nhật sẽ chỉ được thực hiện cho protobuf. nhà phát triển Show
Hướng dẫn này cung cấp phần giới thiệu cơ bản của lập trình viên Python về cách làm việc với bộ đệm giao thức. Bằng cách đi qua việc tạo một ứng dụng ví dụ đơn giản, nó chỉ cho bạn cách
Đây không phải là hướng dẫn toàn diện về cách sử dụng bộ đệm giao thức trong Python. Để biết thông tin tham khảo chi tiết hơn, hãy xem Hướng dẫn ngôn ngữ bộ đệm giao thức (proto2), Hướng dẫn ngôn ngữ bộ đệm giao thức (proto3), Tham chiếu API Python, Hướng dẫn mã được tạo bằng Python và Tham chiếu mã hóa Miền vấn đềVí dụ chúng ta sẽ sử dụng là một ứng dụng "sổ địa chỉ" rất đơn giản có thể đọc và ghi chi tiết liên hệ của mọi người vào và từ một tệp. Mỗi người trong sổ địa chỉ đều có tên, ID, địa chỉ email và số điện thoại liên hệ Làm cách nào để bạn tuần tự hóa và truy xuất dữ liệu có cấu trúc như thế này?
Thay vì các tùy chọn này, bạn có thể sử dụng bộ đệm giao thức. Bộ đệm giao thức là giải pháp linh hoạt, hiệu quả, tự động để giải quyết chính xác vấn đề này. Với bộ đệm giao thức, bạn viết mô tả Nơi tìm mã ví dụMã ví dụ được bao gồm trong gói mã nguồn, trong thư mục "ví dụ". Tải về tại đây Xác định định dạng giao thức của bạnĐể tạo ứng dụng sổ địa chỉ, bạn cần bắt đầu với tệp protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto2 syntax = "proto2"; package tutorial; message Person { optional string name = 1; optional int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { optional string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phones = 4; } message AddressBook { repeated Person people = 1; } Như bạn có thể thấy, cú pháp tương tự như C++ hoặc Java. Hãy xem qua từng phần của tệp và xem nó làm gì Tệp Tiếp theo, bạn có định nghĩa tin nhắn của mình. Một tin nhắn chỉ là một tổng hợp chứa một tập hợp các trường đã nhập. Nhiều kiểu dữ liệu đơn giản tiêu chuẩn có sẵn dưới dạng các kiểu trường, bao gồm protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto6, protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto7, protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto8, protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto9 và class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK0. Bạn cũng có thể thêm cấu trúc khác vào thư của mình bằng cách sử dụng các loại thư khác làm loại trường – trong ví dụ trên, thư class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1 chứa class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK2 thư, trong khi thư class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK3 chứa class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1 thư. Bạn thậm chí có thể xác định các loại thư được lồng bên trong các thư khác – như bạn có thể thấy, loại class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK2 được xác định bên trong class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1. Bạn cũng có thể xác định các loại class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK7 nếu bạn muốn một trong các trường của mình có một trong các danh sách giá trị được xác định trước – ở đây bạn muốn chỉ định rằng một số điện thoại có thể là một trong các loại điện thoại sau. class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK8, class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK9, hoặc import addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "[email protected]" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME0 Các dấu "= 1", "= 2" trên mỗi phần tử xác định "thẻ" duy nhất mà trường đó sử dụng trong mã hóa nhị phân. Số thẻ 1-15 yêu cầu ít byte hơn để mã hóa so với số cao hơn, do đó, để tối ưu hóa, bạn có thể quyết định sử dụng các thẻ đó cho các phần tử thường được sử dụng hoặc lặp lại, để lại các thẻ 16 trở lên cho các phần tử tùy chọn ít được sử dụng hơn. Mỗi phần tử trong một trường lặp lại yêu cầu mã hóa lại số thẻ, vì vậy các trường lặp lại là những ứng cử viên đặc biệt tốt cho việc tối ưu hóa này Mỗi trường phải được chú thích bằng một trong các công cụ sửa đổi sau
Bắt buộc là Mãi mãi Bạn nên rất cẩn thận khi đánh dấu các trường là import addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "[email protected]" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME4. Nếu tại một thời điểm nào đó bạn muốn dừng viết hoặc gửi trường bắt buộc, sẽ có vấn đề khi thay đổi trường này thành trường tùy chọn – người đọc cũ sẽ coi thư không có trường này là không đầy đủ và có thể vô tình từ chối hoặc loại bỏ chúng. Thay vào đó, bạn nên xem xét việc viết các quy trình xác thực tùy chỉnh dành riêng cho ứng dụng cho bộ đệm của mình. Trong Google, các trường import addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "[email protected]" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME4 không được ưa chuộng lắm; . (Proto3 hoàn toàn không hỗ trợ các trường import addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "[email protected]" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME4. ) Bạn sẽ tìm thấy hướng dẫn đầy đủ để viết tệp Biên dịch bộ đệm giao thức của bạnBây giờ bạn đã có một class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK3 (và do đó là các thông báo class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1 và class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK2). Để thực hiện việc này, bạn cần chạy trình biên dịch bộ đệm giao thức person.no_such_field = 1 # raises AttributeError person.id = "1234" # raises TypeError5 trên .proto của mình
Điều này tạo ra #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()0 trong thư mục đích được chỉ định của bạn API bộ đệm giao thứcKhông giống như khi bạn tạo mã bộ đệm giao thức Java và C++, trình biên dịch bộ đệm giao thức Python không trực tiếp tạo mã truy cập dữ liệu cho bạn. Thay vào đó (như bạn sẽ thấy nếu bạn nhìn vào #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()0), nó tạo ra các bộ mô tả đặc biệt cho tất cả các thư, bảng liệt kê và trường của bạn và một số lớp trống bí ẩn, mỗi lớp cho một loại thư class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK Dòng quan trọng trong mỗi lớp là #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()2. Mặc dù các chi tiết về cách thức hoạt động của các siêu dữ liệu Python nằm ngoài phạm vi của hướng dẫn này, nhưng bạn có thể coi chúng giống như một khuôn mẫu để tạo các lớp. Tại thời điểm tải, siêu lớp #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()3 sử dụng các bộ mô tả đã chỉ định để tạo tất cả các phương thức Python mà bạn cần để làm việc với từng loại thông báo và thêm chúng vào các lớp có liên quan. Sau đó, bạn có thể sử dụng các lớp được điền đầy đủ trong mã của mình Kết quả cuối cùng của tất cả những điều này là bạn có thể sử dụng lớp class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1 như thể nó định nghĩa từng trường của lớp cơ sở #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()5 như một trường thông thường. Ví dụ, bạn có thể viết import addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "[email protected]" phone = person.phones.add() phone.number = "555-4321" phone.type = addressbook_pb2.Person.HOME Lưu ý rằng các nhiệm vụ này không chỉ thêm các trường mới tùy ý vào một đối tượng Python chung. Nếu bạn cố gắng chỉ định một trường không được xác định trong tệp #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()7. Nếu bạn chỉ định một trường cho một giá trị không đúng loại, một số #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()8 sẽ xuất hiện. Ngoài ra, việc đọc giá trị của một trường trước khi nó được đặt sẽ trả về giá trị mặc định person.no_such_field = 1 # raises AttributeError person.id = "1234" # raises TypeError Để biết thêm thông tin về chính xác những thành viên mà trình biên dịch giao thức tạo ra cho bất kỳ định nghĩa trường cụ thể nào, hãy xem tham chiếu mã do Python tạo ra liệt kêEnums được siêu dữ liệu mở rộng thành một tập hợp các hằng số tượng trưng với các giá trị nguyên. Vì vậy, ví dụ, hằng số #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()9 có giá trị 2 Phương thức nhắn tin tiêu chuẩnMỗi lớp thông báo cũng chứa một số phương thức khác cho phép bạn kiểm tra hoặc thao tác với toàn bộ thông báo, bao gồm
Các phương thức này triển khai giao diện #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close()5. Để biết thêm thông tin, hãy xem Phân tích cú pháp và tuần tự hóaCuối cùng, mỗi lớp bộ đệm giao thức có các phương thức để viết và đọc thông báo thuộc loại bạn đã chọn bằng cách sử dụng định dạng nhị phân của bộ đệm giao thức. Bao gồm các
Đây chỉ là một vài tùy chọn được cung cấp để phân tích cú pháp và tuần tự hóa. Một lần nữa, hãy xem danh sách đầy đủ Bộ đệm giao thức và thiết kế hướng đối tượng Các lớp bộ đệm giao thức về cơ bản là các bộ chứa dữ liệu (như các cấu trúc trong C) không cung cấp chức năng bổ sung; . Nếu bạn muốn thêm hành vi phong phú hơn vào một lớp đã tạo, cách tốt nhất để làm điều này là bọc lớp bộ đệm giao thức đã tạo trong một lớp dành riêng cho ứng dụng. Gói bộ đệm giao thức cũng là một ý tưởng hay nếu bạn không có quyền kiểm soát thiết kế của tệp Viết một tin nhắnBây giờ, hãy thử sử dụng các lớp đệm giao thức của bạn. Điều đầu tiên bạn muốn ứng dụng sổ địa chỉ của mình có thể thực hiện là ghi thông tin cá nhân vào tệp sổ địa chỉ của bạn. Để thực hiện việc này, bạn cần tạo và điền các phiên bản của các lớp bộ đệm giao thức của mình, sau đó ghi chúng vào luồng đầu ra Đây là một chương trình đọc một class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK3 từ một tệp, thêm một class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK1 mới vào nó dựa trên đầu vào của người dùng và ghi lại class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType class PhoneNumber(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON_PHONENUMBER DESCRIPTOR = _PERSON class AddressBook(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _ADDRESSBOOK3 mới vào tệp một lần nữa. Các phần gọi trực tiếp hoặc mã tham chiếu do trình biên dịch giao thức tạo ra được tô sáng #! /usr/bin/python import addressbook_pb2 import sys # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) person.name = raw_input("Enter name: ") 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.phones.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.PhoneType.MOBILE elif type == "home": phone_number.type = addressbook_pb2.Person.PhoneType.HOME elif type == "work": phone_number.type = addressbook_pb2.Person.PhoneType.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.people.add()) # Write the new address book back to disk. f = open(sys.argv[1], "wb") f.write(address_book.SerializeToString()) f.close() đọc tin nhắnTất nhiên, một cuốn sổ địa chỉ sẽ không được sử dụng nhiều nếu bạn không thể lấy bất kỳ thông tin nào từ nó. Ví dụ này đọc tệp được tạo bởi ví dụ trên và in tất cả thông tin trong đó #! /usr/bin/python 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.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.PhoneType.MOBILE: print " Mobile phone #: ", elif phone_number.type == addressbook_pb2.Person.PhoneType.HOME: print " Home phone #: ", elif phone_number.type == addressbook_pb2.Person.PhoneType.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) Mở rộng bộ đệm giao thứcSớm hay muộn sau khi bạn phát hành mã sử dụng bộ đệm giao thức của mình, chắc chắn bạn sẽ muốn "cải thiện" định nghĩa của bộ đệm giao thức. Nếu bạn muốn bộ đệm mới của mình tương thích ngược và bộ đệm cũ của bạn tương thích về phía trước – và bạn gần như chắc chắn muốn điều này – thì có một số quy tắc bạn cần tuân theo. Trong phiên bản mới của bộ đệm giao thức
(Có những quy tắc này, nhưng chúng hiếm khi được sử dụng. ) Nếu bạn tuân theo các quy tắc này, mã cũ sẽ vui vẻ đọc thư mới và chỉ cần bỏ qua mọi trường mới. Đối với mã cũ, các trường tùy chọn đã bị xóa sẽ chỉ có giá trị mặc định và các trường lặp lại đã xóa sẽ trống. Mã mới cũng sẽ đọc rõ ràng các tin nhắn cũ. Tuy nhiên, hãy nhớ rằng các trường tùy chọn mới sẽ không xuất hiện trong thư cũ, vì vậy bạn cần kiểm tra rõ ràng xem chúng có được đặt bằng Sử dụng nâng caoBộ đệm giao thức có những công dụng vượt xa các trình truy cập và tuần tự hóa đơn giản. Hãy chắc chắn khám phá tài liệu tham khảo API Python để xem bạn có thể làm gì khác với chúng Một tính năng chính được cung cấp bởi các lớp bản tin giao thức là phản ánh. Bạn có thể lặp lại các trường của một thông báo và thao tác các giá trị của chúng mà không cần viết mã của bạn đối với bất kỳ loại thông báo cụ thể nào. Một cách rất hữu ích để sử dụng phản chiếu là chuyển đổi các thông báo giao thức sang và từ các mã hóa khác, chẳng hạn như XML hoặc JSON. Việc sử dụng phản xạ nâng cao hơn có thể là để tìm sự khác biệt giữa hai thông báo cùng loại hoặc để phát triển một loại "biểu thức chính quy cho thông báo giao thức" trong đó bạn có thể viết các biểu thức khớp với nội dung thông báo nhất định. Nếu bạn sử dụng trí tưởng tượng của mình, bạn có thể áp dụng Bộ đệm giao thức cho nhiều vấn đề hơn bạn có thể mong đợi ban đầu |