protobuf python api
来源:互联网 发布:南京厚蕴网络 编辑:程序博客网 时间:2024/06/05 04:10
摘要:
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 codeMerges the contents of the specified message into current message.This method merges the contents of the specified message into the currentmessage. Singular fields that are set in the specified message overwritethe corresponding fields in the current message. Repeated fields areappended. Singular sub-messages and groups are recursively merged.Args: other_msg: Message to merge into the current message.
CopyFrom(self, other_msg)
source codeCopies the content of the specified message into the current message.The method clears the current message and then merges the specifiedmessage using MergeFrom.Args: other_msg: Message to copy into the current one.
- protobuf python api
- Lua protobuf相关API
- protobuf c++ API
- protobuf c++ API
- protobuf c++ api 简介
- python操作protobuf
- python使用protobuf
- python版protobuf安装
- python读写protobuf
- python下使用protobuf
- python读写protobuf
- Python操作protobuf示例
- Python中protobuf
- 安装protobuf Python支持
- python使用protobuf
- python protobuf 实践
- python 如何使用 protobuf
- python读写protobuf
- NEFU474 The Perfect StallHal Burch 二分图最大匹配
- 数据流和十六进制转换
- 由于插件问题导致iPhone无限重启且进不了安全模式,如何删掉问题插件
- 二分图匹配题集
- CodeForce 375B D - Maximum Submatrix 2
- protobuf python api
- Dropbox CEO Drew Houston在MIT 2013年毕业典礼上的演讲:关于一个网球、一个圆圈和一个数字
- 数据挖掘面试题
- syntax: command not found
- 修复U盘
- Jena TDB的使用简介
- 极大似然估计
- 搜索份额之“皇帝的新衣”闹剧
- Valid Palindrome