Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign to repeated field?

I am using protocol buffers in python and I have a Person message

repeated uint64 id 

but when I try to assign a value to it like:

person.id = [1, 32, 43432] 

I get an error: Assigment not allowed for repeated field "id" in protocol message object How to assign a value to a repeated field ?

like image 800
PaolaJ. Avatar asked May 18 '14 20:05

PaolaJ.


People also ask

What is a repeated field?

A repeated field can be accessed as an ARRAY type in standard SQL. A RECORD column can have REPEATED mode, which is represented as an array of STRUCT types. Also, a field within a record can be repeated, which is represented as a STRUCT that contains an ARRAY . An array cannot contain another array directly.

What is repeated in Protobuf?

repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.

What is repeated in gRPC?

gRPC services provide two ways of returning datasets, or lists of objects. The Protocol Buffers message specification uses the repeated keyword for declaring lists or arrays of messages within another message. The gRPC service specification uses the stream keyword to declare a long-running persistent connection.

What is Protobuf Python?

Protocol buffers (Protobuf) are a language-agnostic data serialization format developed by Google. Protobuf is great for the following reasons: Low data volume: Protobuf makes use of a binary format, which is more compact than other formats such as JSON. Persistence: Protobuf serialization is backward-compatible.


Video Answer


2 Answers

As per the documentation, you aren't able to directly assign to a repeated field. In this case, you can call extend to add all of the elements in the list to the field.

person.id.extend([1, 32, 43432]) 
like image 156
Tim Avatar answered Nov 15 '22 16:11

Tim


If you don't want to extend but overwrite it completely, you can do:

person.id[:] = [1, 32, 43432] 

This approach will also work to clear the field entirely:

del person.id[:] 
like image 32
kirpit Avatar answered Nov 15 '22 18:11

kirpit