谷歌协议缓冲工具。

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:字符串。RTL设备语言用标志“+”格式化,数字后加符号   java GAE作为桌面应用程序(Swing)的服务提供商   java将InputStream转换为FileInputStream不适用于Apache POI   java外部Voronoi库“网格”:什么是草图和处理?   重载重写的泛型方法java   java显示组织上设置的错误。springframework。验证。jsp中的错误对象   java一些Spring模型属性没有显示在我的JSP中   java无法编译Guava 23的SimpleTimeLimiter示例   java如何更改JTree中的“根”目录名?   java如何在安卓中对相对布局产生连锁反应?   java错误:org。冬眠例外SQLGrammarException:无法提取结果集,dateAccessed是未知列   如何使用java监听JSON文件更新   由抽象封闭类创建的匿名内部类能否通过反射确定实现类?