protobuf python api

时间:2023-02-22 17:46:25

摘要:

python中一切都可以看作类。那么如何查看每个类的API。使用ipython

python  protobuf 的函数在message中定义

此处所有的api说明:https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class

编译

protoc -I=./ --python_out=./ people.proto

1  GidChannelInfo.proto

package bfd.gidchannelinfo;

//定义 gid的来源,是dsp还是电商或者媒体等

message Channel

{

required string name = 1; //来源,例如dsp_Cbehe

required int64 timestamp = 2; //时间戳

}





//key为 G:Channel:gid

message GidChannelInfo

{

required Channel init_channel = 1; //gid初次产生的channel来源

repeated Channel channel = 2; //包含的所有channel渠道

}

2  例子

gidchannelinfo_tmp = GidChannelInfo_pb2.GidChannelInfo()

channel_tmp = GidChannelInfo_pb2.Channel()





DB_SERVER = 'app-2'

db = "DMP_GDMP_Cbehe"

conn = mdb.Connect(DB_SERVER,'bfdroot','qianfendian',db)

cur = conn.cursor()

#cur.execute("select gid,update_time from Mapping_gid where id >0 and id<20000000")

cur.execute("select gid,update_time from Mapping_gid limit 1")

results = cur.fetchall()

for result in results:

    print "result :",str(result)

    gid = result[0]

date_tmp = result[1]

    key = "G:GidChannelInfo:"+gid

    #d = datetime.datetime.strptime(date_tmp,"%Y-%m-%d %H:%M:%S")

    timestamp_tmp = int(time.mktime(date_tmp.timetuple()))

    channel_tmp.name = "dsp_baifendian"

    channel_tmp.timestamp = timestamp_tmp

    print "timestamp_tmp :",timestamp_tmp

    gidchannelinfo_tmp.init_channel.CopyFrom(channel_tmp)

    channel_tmp = gidchannelinfo_tmp.channel.add()

    channel_tmp.name = "dsp_baifendian"

    channel_tmp.timestamp = timestamp_tmp

    channel_tmp = gidchannelinfo_tmp.channel.add()

    channel_tmp.name = "dsp_behe"

    channel_tmp.timestamp = timestamp_tmp

    print "gidchannelinfo_tmp: ",gidchannelinfo_tmp

    mystr = gidchannelinfo_tmp.SerializeToString()

3 注意 :

对于 Singular fields  可以使用 gci.init_channel.MergeFrom赋值

或者对于每个属性赋值。gci.init_channel.name=name

对于repeated fileds可以使用

CopyFrom    MergeFrom

4 api 说明

MergeFrom(self, other_msg)

source code 
Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current
message. Singular fields that are set in the specified message overwrite
the corresponding fields in the current message. Repeated fields are
appended. Singular sub-messages and groups are recursively merged. Args:
other_msg: Message to merge into the current message.

CopyFrom(self, other_msg)

source code 
Copies the content of the specified message into the current message.

The method clears the current message and then merges the specified
message using MergeFrom. Args:
other_msg: Message to copy into the current one.

此处所有的api说明:https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class

 

protobuf python api的更多相关文章

  1. Appium python API 总结

    Appium python api 根据testerhome的文章,再补充一些文章里面没有提及的API [TOC] [1]find element driver 的方法 注意:这几个方法只能通过sel ...

  2. The novaclient Python API

    The novaclient Python API Usage First create a client instance with your credentials: >>> f ...

  3. Openstack python api 学习文档 api创建虚拟机

    Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...

  4. BotVS开发基础—Python API

    代码 import json def main(): # python API列表 https://www.botvs.com/bbs-topic/443 #状态信息 LogStatus(" ...

  5. 《Spark Python API 官方文档中文版》 之 pyspark&period;sql &lpar;一&rpar;

    摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少.每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需, ...

  6. 《Spark Python API 官方文档中文版》 之 pyspark&period;sql &lpar;二&rpar;

    摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少.每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需, ...

  7. HBase Python API

    HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...

  8. 二、Blender&sol;Python API总览

    原文:https://docs.blender.org/api/blender_python_api_current/info_overview.html Python in Blender  Ble ...

  9. Appium&plus;python自动化8-Appium Python API

    Appium+python自动化8-AppiumPython API   前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...

随机推荐

  1. HTML标记之Form表单

    一.表单的作用 从访问的Web站点的用户那里获得信息.访问者可以使用诸如文本域.列表框.复选框以及单选按钮之类的表单元素输入信息,然后单击某个按钮提交这些信息.是客户端与服务器端的交流途径. 二.说明 ...

  2. c 函数调用产生的汇编指令和数据在内存情况&lpar;1&rpar;

    一直对函数调用的具体汇编指令和各种变量在内存的具体分配,一知半解.各种资料都很详细,但是不实践,不亲自查看下内存总不能笃定.那就自己做下. 两个目的: 一,函数和函数调用编译后的汇编指令基本样貌 二, ...

  3. hdoj 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...

  4. WPF Effect 造成的字体模糊

    WPF 里面有个Effect ,暂且可以理解为 "特效" 分类. 但是有时候使用不恰当,容易出现各种毛病. 例如: 代码如下: <StackPanel HorizontalA ...

  5. REdis Asynchronous AOF fsync is taking too long

    redis.conf中的no-appendfsync-on-rewrite默认值为no,表示在重写AOF文件或RDB文件时阻塞fsync. 如果重写AOF或RDB文件时长过长,则在日志中可以看到如下信 ...

  6. Core 接口发布报错

    An error occurred while starting the application 提示:启动应用程序时发生错误 这个错误在Startup启动项里面 要找具体的报错位置 如下 这样就可以 ...

  7. 【MyBatis学习06】&lowbar;parameter:解决There is no getter for property named in class java&period;lang&period;String

    我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...

  8. Asp&period;Net Core &plus; Ocelot 网关搭建:路由简单配置

    前言   Ocelot是一个基于中间件的网关实现,功能有很多.从浅入深简单学习并记录一下吧.本篇就是一个简单的路由配置实现. DEMO 搭建   首先建立三个项目.Api.User,Api.Artic ...

  9. ubuntu&sol;linux系统中安装jdk以及eclipse&lpar;附图解详细步骤&rpar;

    1.首先得先下载JDK和eclipsejdk下载网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21 ...

  10. GridControl详解(八)菜单

    菜单控件 拖入窗口中 显示如下 设置popupMenu 设置barManager 设置controller 增加菜单项 弹出配置窗口 一般菜单项设置 对应属性如下: 对应事件: 选择菜单项设置 事件同 ...