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 ?
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.
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.
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.
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.
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])
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[:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With