摘要:

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.sql (一)

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

  6. 《Spark Python API 官方文档中文版》 之 pyspark.sql (二)

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

  7. HBase Python API

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

  8. 二、Blender/Python API总览

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

  9. Appium+python自动化8-Appium Python API

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

随机推荐

  1. Android学习笔记之Spinner

    pinner就相当于Html中的下拉列表框,在下面的例子里我们共同学习一下spinner的使用. 属性: 属性名称 描述 android:prompt 该提示在下拉列表对话框显示时显示.(译者注:对话 ...

  2. php 数组操作符

    1.数组操作符 数组运算符 例子 名称 结果 $a + $b 联合 $a 和 $b 的联合. $a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE. $a === $b 全等 ...

  3. MFC 透明内存DC

    在MFC中绘制比较复杂图形,通常采用双缓冲技术来绘图,的确可以大大加快绘制速度和减少闪烁,但是有些情况也不尽然. 我最近遇到了一个问题,采用的也是双缓冲来加快绘图,但是绘制效果还是不尽人意.A对象里大 ...

  4. java代码用dom4j解析xml文件的简单操作

    时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...

  5. C: define many functions using predefine..

    /* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ # ...

  6. Linux环境下Android JNI程序的编译

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8993493 在android开发中,有时候需要编写一些C/C++代码,这时候 ...

  7. SQL Server触发器的禁用和启用

    禁用: ALTER TABLE trig_example DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE trig_example ENABLE TRIGGER tr ...

  8. Windows、Linux -- 远程登录、文件传输、文件共享

    Linux  <--->  Linux 远程登录: ssh服务 ssh root@10.20.62.124 文件传输: scp服务 上传  scp /home/xxx.txt root@1 ...

  9. 转:Eclipse Debug 界面应用详解——Eclipse Debug不为人知的秘密

    今天浏览csdn,发现一文详细的描述了Eclipse Debug中的各个知识点,非常详尽!特此记录. Eclipse Debug不为人知的秘密 http://blog.csdn.net/mgoann/ ...

  10. 【啊哈!算法】算法7:Dijkstra最短路算法

    上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图 ...