python 处理protobuf 接口常见错误

python 处理protobuf 接口常见错误
1.问题 :
Assignment not allowed to repeated field '> http://www.coin163.com/it/x3098736972800887904/python-protocol buffers-googleexceptionprotobuf
原理:
Python3.5 使用 protobuf3.0.0 赋值

解决:

  1:普通对象 直接赋值即可。
      `article = Article()a
      article.id = 121212a
      article.readCount = 0`
  2:含有组合字段的对象(包含了一个Organize对象)
  * 方法一
  'article = Article()a
    article.id = 123123a
    article.organize.organizeId = 212121a
    article.organize.name = "haha"'
  * 方法二 
    在实际使用过程中你还可以把organize往底层传,在底层对organize的字段赋值。
  article = Article()i
  initOrganize(1212, 'haha', article.organize)
  def initOrganize(id, name, organize):
      organize.id = id
      organize.name = name

  3:包含repeated的字段
      imageAndTexts = organize.imageAndTexts
      for data in datas:          
        imageAndText = imageAndTexts.add()

        convertDataToImageAndText(data, imageAndtext)
      def convertDataToImageAndText(data, it):
        it.image = data.image
       it.text = data.text
posted @ 2021-07-08 10:16  悠然采  阅读(653)  评论(0编辑  收藏  举报