摘要:

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

 

最新文章

  1. app后端设计--总目录 (转)
  2. 我的前端故事----优美的编辑器GitHub Atom
  3. ireport5.6+jasperreport6.3开发(四)--以javabean为基准的报表开发(ireport)
  4. Provisioning Services 7.6 入门到精通系列之一:PVS前期规划
  5. Android MediaPlayer的生命周期
  6. Python进阶之“属性(property)”详解
  7. 改变tableView索引颜色
  8. laravel 安装及入门
  9. git客户端
  10. MapReduce从输入文件到Mapper处理之间的过程
  11. [ 兼容 ] IE和Firefox的Javascript兼容性总结
  12. JavaScript之Loading进度条
  13. 基于Tengine的反向代理详细配置
  14. 了解负载均衡 会话保持 session同步(转)
  15. Android Map新用法:MapFragment应用
  16. 实战 iTextSharp
  17. 第3章2节《MonkeyRunner源码剖析》脚本编写示例: MonkeyDevice API使用示例(原创)
  18. linux下64位汇编的系统调用(3)
  19. centos7 ambari2.6.1.5+hdp2.6.4.0 大数据集群安装部署
  20. linux文件行首行尾添加或替换

热门文章

  1. WEBROOT根目录 &lt;%=request.getContextPath()%&gt;
  2. HDU 5963 博弈
  3. 最大独立集 HDU 1068
  4. nginx 正则表达式
  5. HUST - 1599 Multiple
  6. Swift POP+MVVM
  7. Android &amp;Swift iOS开发:语言与框架对比
  8. java 数据结构 图
  9. python smtplib.SMTPDataError: (554
  10. HDU-1301 Jungle Roads(最小生成树[Prim])