1

I am trying to submit a response using protocol buffers in python. Below is the structure.

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;
}

Now, below is my python code that I am trying to submit.

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

When I submit using the below:

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

All that is returned is the bid_response.processing_time_ms.

I suspect I am nt doing repeated and messages correctly.

2 Answers 2

8

I remember spending a while hunting this down in my python code a while back. You can find the documentation for the fields interface in python here : http://code.google.com/apis/protocolbuffers/docs/reference/python-generated.html#fields.

What you want is the add member function - using that your code would look like this:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Wow...that kinda worked! But for hte ad.clickthoru it is repeated. I get this error. '"%s" in protocol message object.' % proto_field_name) AttributeError: Assignment not allowed to repeated field "click_through_url" in protocol message object. How to I make click though repeated? Thanks
Thanks..got it...haha...repeated is a list and just use append.
0

Did you read the documentation? It's quite clear.

You should replace

ad = bid_response.Ad()

with

ad = bid_response.ad.add()

and likewise for AdSlot.

2 Comments

Here's a slightly more appropriate documentation link: code.google.com/apis/protocolbuffers/docs/reference/…
it is the new document here for proto3. developers.google.com/protocol-buffers/docs/reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.