protobuf
来源:互联网 发布:俄罗斯域名后缀 编辑:程序博客网 时间:2024/06/06 08:35
Overview
对多个proto文件同时编译;指定include目录或out目录。
文件
命令
s$ lsout src$ ls src/bar.proto foo.proto$ ls out/$ protoc -I=./src --python_out=./out ./src/*.proto$ ls out src$ ls out/bar_pb2.py foo_pb2.py$
foo.proto
syntax = "proto2";message foo { required string name = 1; required int32 id = 2; optional string comment = 3;}
bar.proto
syntax = "proto2";import "foo.proto";message bar { required string name = 1; required int32 id = 2; optional string comment = 3; optional foo foo2 = 4;}
Python Code (src/test.py)
#!/usr/bin/python'''src$ ./test.py serialized string: 0a0342617210011a0f42617220436f6d 6d656e74202e2e2e22180a03466f6f10 021a0f466f6f20436f6d6d656e74202e 2e2eparsed object: name: "Bar"id: 1comment: "Bar Comment ..."foo { name: "Foo" id: 2 comment: "Foo Comment ..."}src$ hexdump -C temp.dat00000000 0a 03 42 61 72 10 01 1a 0f 42 61 72 20 43 6f 6d |..Bar....Bar Com|00000010 6d 65 6e 74 20 2e 2e 2e 22 18 0a 03 46 6f 6f 10 |ment ..."...Foo.|00000020 02 1a 0f 46 6f 6f 20 43 6f 6d 6d 65 6e 74 20 2e |...Foo Comment .|00000030 2e 2e |..|00000032src$ '''import binasciiimport syssys.path.append("../out")import foo_pb2import bar_pb2bar = bar_pb2.bar()bar.name = "Bar"bar.id = 1bar.comment = "Bar Comment ..."bar.foo.name = "Foo"bar.foo.id = 2bar.foo.comment = "Foo Comment ..."s = bar.SerializeToString()f = open("temp.dat", "wb")f.write(s)f.close()print "serialized string:", print binascii.b2a_hex(bytearray(s))bar2 = bar_pb2.bar()bar2.ParseFromString(s)print "parsed object:", bar2
说明
这里把pb2.py放在out目录,仅仅是为了演示include, out等目录设置的方法。实际项目中不建议这种做法。
阅读全文
0 0
- protobuf
- ProtoBuf
- protobuf
- protobuf
- protobuf
- Protobuf
- protobuf
- Protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- ProtoBuf
- 【Protobuf】
- 【Protobuf】
- protobuf
- 【调试技巧】一种针对正在运行的进程中途写值快速调试的方法
- JAVA的Hello World程序
- 经典DP——最长上升子序列
- C++按值传参、按地址传参、按引用传参外加const比较详解
- 【HDU 5327】Olympiad(前缀和)
- protobuf
- java中的乐观锁与悲观锁
- Cocos Creator Animation
- java枚举enum
- 【C语言】【unix c】计算机语言的发展史
- 常用的前端开发工具
- zigbee 协议栈 SamPle App工程(转自天运科技)第十五讲 SerialApp工程
- JDK动态代理的实践
- iOS 一一 AutoLayout之Masonry实现