Proto

โมดูลสำหรับการประมวลผลข้อความโปรโตคอล

สมาชิก

encode_text

string proto.encode_text(x)

แสดงผลการเข้ารหัสของอาร์กิวเมนต์ struct เป็นข้อความโปรโตคอลรูปแบบข้อความ โครงสร้างข้อมูลต้องประกอบด้วยสตริง, จำนวนเต็ม, จำนวนทศนิยม หรือบูลีน หรือ struct, ลำดับ และ dict ของประเภทเหล่านี้แบบเรียกซ้ำ

ระบบจะแปลง struct เป็นข้อความ และแสดงผลฟิลด์ตามลำดับชื่อ โดยจะละเว้นฟิลด์ struct แต่ละรายการที่มีค่าเป็น None

ระบบจะแปลงลำดับ (เช่น รายการหรือทูเพิล) เป็นฟิลด์ที่ทำซ้ำ โดยองค์ประกอบต้องไม่เป็นลำดับหรือ dict

ระบบจะแปลง dict เป็นฟิลด์ที่ทำซ้ำของข้อความที่มีฟิลด์ชื่อ "key" และ "value" และแสดงผลรายการตามลำดับการทำซ้ำ (การแทรก) โดยคีย์ของ dict ต้องเป็นสตริงหรือจำนวนเต็ม และค่าต้องไม่เป็นลำดับหรือ dict ตัวอย่าง

proto.encode_text(struct(field=123))
# field: 123

proto.encode_text(struct(field=True))
# field: true

proto.encode_text(struct(field=[1, 2, 3]))
# field: 1
# field: 2
# field: 3

proto.encode_text(struct(field='text', ignored_field=None))
# field: "text"

proto.encode_text(struct(field=struct(inner_field='text', ignored_field=None)))
# field {
#   inner_field: "text"
# }

proto.encode_text(struct(field=[struct(inner_field=1), struct(inner_field=2)]))
# field {
#   inner_field: 1
# }
# field {
#   inner_field: 2
# }

proto.encode_text(struct(field=struct(inner_field=struct(inner_inner_field='text'))))
# field {
#    inner_field {
#     inner_inner_field: "text"
#   }
# }

proto.encode_text(struct(foo={4: 3, 2: 1}))
# foo: {
#   key: 4
#   value: 3
# }
# foo: {
#   key: 2
#   value: 1
# }

พารามิเตอร์

พารามิเตอร์ คำอธิบาย
x โครงสร้าง; ต้องระบุ