1. 程式人生 > 其它 >python 處理protobuf 介面常見錯誤

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