谷歌协议缓冲工具。

pbtools的Python项目详细描述


buildstatuscoverage

关于

python 3中的google protocol buffers工具。

  • 源代码生成器。
  • 仅支持Proto3。

项目主页:https://github.com/eerimoq/pbtools

文档:http://pbtools.readthedocs.org/en/latest

安装

pipinstallpbtools

示例用法

C源代码

在本例中,我们使用简单的proto文件hello_world.proto

syntax="proto3";packagehello_world;messageFoo{int32bar=1;}

接下来,我们从proto文件生成c源代码。

$ pbtools generate_c_source examples/hello_world/hello_world.proto
Successfully generated hello_world.h and hello_world.c.

请参阅hello_world.hhello_world.c了解 生成的文件。

我们将使用下面生成的类型和函数。

structhello_world_foo_t{structhello_world_heap_t*heap_p;int32_tbar;};structhello_world_foo_t*hello_world_foo_new(void*workspace_p,size_tsize);inthello_world_foo_encode(structhello_world_foo_t*self_p,void*encoded_p,size_tsize);inthello_world_foo_decode(structhello_world_foo_t*self_p,constuint8_t*encoded_p,size_tsize);

对foo消息进行编码和解码。

#include<string.h>
#include<stdio.h>
#include"hello_world.h"intmain(intargc,constchar*argv[]){intsize;uint8_tworkspace[64];uint8_tencoded[16];structhello_world_foo_t*foo_p;/* Encode. */foo_p=hello_world_foo_new(&workspace[0],sizeof(workspace));if(foo_p!=NULL){foo_p->bar=78;size=hello_world_foo_encode(foo_p,&encoded[0],sizeof(encoded));if(size>=0){printf("Successfully encoded Foo into %d bytes.\n",size);}}/* Decode. */foo_p=hello_world_foo_new(&workspace[0],sizeof(workspace));if(foo_p!=NULL){size=hello_world_foo_decode(foo_p,&encoded[0],sizeof(encoded));if(size>=0){printf("Successfully decoded %d bytes into Foo.\n",size);printf("Foo.bar: %d\n",foo_p->bar);}}return(0);}

有关此示例中使用的所有文件,请参见hello_world

命令行工具

生成c源子命令

下面是一个如何从 原始文件。

$ pbtools generate_c_source examples/address_book/address_book.proto
Successfully generated address_book.h and address_book.c.

请参阅address_book.haddress_book.c了解 生成的文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我能在这个程序中更好地使用内存吗?   为什么我的Java while循环迭代了一半   java IntelliJ IDEA不在构建时复制资源   socket仅在Java TCP服务器输出上检查客户端断开连接   java游戏物理摩擦   java片段onClick调用方法   symja数学分析器中无法识别java Abs[x]   java在使用泛型时创建二进制搜索树类的实例?   java在外键约束表上的添加和删除   语法java表达式的含义,如果有条件   java创建内联对象并作为参数传递   是否有相当于Redis排序集(zset)的Java数据结构   java找不到适合的方法(无参数)   音频文件操作给定字节帧的音量Java   Eclipse4不以JavaWebStart启动   java如何使用org在JSON对象中获取嵌套的键元素。json?   java与Jackson的反序列化:“org.codehaus.Jackson.map.JsonMappingException:无法反序列化[projectname]的实例。”   字符串的Java正则表达式   spring集成上的java检测缺火指令