How to assign to repeated field?

PythonProtocol Buffers

Python Problem Overview


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 ?

Python Solutions


Solution 1 - Python

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])

Solution 2 - Python

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[:]

Solution 3 - Python

For repeated composite types this is what worked for me.

del person.things[:]
person.things.extend([thing1, thing2, ..])

taken from these comments https://stackoverflow.com/questions/23726335/how-to-assign-to-repeated-field#comment98134436_47748153 https://stackoverflow.com/questions/23726335/how-to-assign-to-repeated-field#comment83313602_23726417

Solution 4 - Python

After loosing a lot of sleep trying to get a basic example for repeated fields working, I finally got it.

The problem:

  • Create a calculator.py with two functions, square and multiplier.
  • Using GRPC, create a Proto file for the same.
  • Write a server, a client.
  • Run the server, and run the client to get correct results.

The Proto file:

syntax = "proto3";

message Number {
    int32 value = 1;
}

message NumList {
    string name = 1;
    repeated Number nums = 2;
}

service Calculator {
    rpc Multiplier(NumList) returns (Number) {}
    rpc Square(Number) returns (Number) {}
}

Now the square part is easy, but for the Multiplier, I wanted to pass a list of Numbers (as in Number type as defined in the proto file).

The problem was with the repeated field. And here is the ultimate solution in short.

The solution:

import grpc

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc
# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')

# create a stub (client)
stub = calculator_pb2_grpc.CalculatorStub(channel)
num_list = calculator_pb2.NumList()
num_list.name = 'MyFirstList'
n1 = num_list.nums.add()
n2 = num_list.nums.add()
n3 = num_list.nums.add()
n1.value = 10
n2.value = 20
n3.value = 30

assert len(num_list.nums) == 3

response = stub.Multiplier(num_list)
print(response.value)

The Calculator Multiplier function (because this needs to be shown):

def multiplier(numlist, name):
    mul = 1
    for num in numlist:
        mul = mul * num.value
    print(f'Result of list {name}')
    return mul

Hope this helps someone. Hope this is as descriptive as it should be.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaolaJ.View Question on Stackoverflow
Solution 1 - PythonTimView Answer on Stackoverflow
Solution 2 - PythonkirpitView Answer on Stackoverflow
Solution 3 - PythonChandlerView Answer on Stackoverflow
Solution 4 - PythonArindam RoychowdhuryView Answer on Stackoverflow