2012-02-21 8 views
0

私はPythonでプロトコルバッファーを使って応答を提出しようとしています。以下はその構造です。Python、プロトコルバッファー、ネストされたメッセージと繰り返しフィールドが機能するようにする

message BidResponse { 
    message Ad { 
    optional string html_snippet = 1; 
    message TemplateParameter { 
     optional string parameter_value = 1; 
     optional string blank_ad_parameter_value = 8; 
     optional string buyer_creative_id = 2; 
     optional string click_through_url = 3; 
     optional int32 left = 4; 
     optional int32 right = 5; 
     optional int32 top = 6; 
     optional int32 bottom = 7; 
     optional int32 backup_index = 9; 
    }; 
    repeated TemplateParameter template_parameter = 13; 
    repeated string click_through_url = 4; 
    repeated int32 vendor_type = 5; 
    message AdSlot { 
     required int32 id = 1; 
     required int64 max_cpm_micros = 2; 
    } 
    repeated AdSlot adslot = 3; 
    } 
    repeated Ad ad = 2; 
    optional int32 processing_time_ms = 4; 
} 

以下は、私が提出しようとしている私のpythonコードです。

ms = (time.time() - start)*1000 
bid_response = realtime_bidding_pb2.BidResponse() 
bid_response.processing_time_ms = int(ms) 
ad = bid_response.Ad() 
ad.html_snippet = """<img src='http://cdn.test.com/test.gif' />""" 
ad.click_through_url = """test.com""" 

adslot = ad.AdSlot() 
adslot.id = adslots_id[0]    
adslot.max_cpm_micros=150000000 

私は以下の使用して提出:

'Content-Type', 'application/octet-stream' 
bid_response.SerializeToString() 

を返されるすべてbid_response.processing_time_msです。

私は、繰り返しとメッセージを正しく実行しているとは思えません。

答えて

6

私はこの間、私のpythonコードでこれを狩るのを覚えています。フィールドインタフェースのドキュメントは、Pythonのhttp://code.google.com/apis/protocolbuffers/docs/reference/python-generated.html#fieldsにあります。何をしたい

addメンバ関数である - あなたのコードは次のようになりますことを使用して:

ms = (time.time() - start)*1000 
bid_response = realtime_bidding_pb2.BidResponse() 
bid_response.processing_time_ms = int(ms) 
ad = bid_response.ad.add() 
ad.html_snippet = """<img src='http://cdn.test.com/test.gif' />""" 
ad.click_through_url = """test.com""" 

adslot = ad.adslot.add() 
adslot.id = adslots_id[0]    
adslot.max_cpm_micros=150000000 
+0

うわー...ちょっと働いた!しかし、hte ad.clickthoruのために繰り返されます。私はこのエラーが発生します。プロトコルメッセージオブジェクトの '%s' ' %proto_field_name) AttributeError:プロトコルメッセージオブジェクトの繰り返しフィールド「click_through_url」に割り当てが許可されていません。私はどのようにしてクリックを繰り返すのですか?おかげで – Tampa

+0

ありがとう..それを忘れて... haha​​ ...繰り返しリストがあり、ちょうど追加を使用します。 – Tampa

0

the documentationを読んだことがありますか?それはかなり明確です。

あなたは、アドスロット用

ad = bid_response.ad.add() 

と同様にして

ad = bid_response.Ad() 

を交換する必要があります。

+0

もう少し適切なドキュメントのリンクがあります:http://code.google.com/apis/protocolbuffers/ docs/reference/python-generated.html#fields –

関連する問題