jasonjack2015 / protobuf

Automatically exported from code.google.com/p/protobuf
Other
0 stars 0 forks source link

Repeated fields in python do not implement the "index()" function #653

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Define a protobuf message that uses a repeated field.
2. Compile the protobuf to python code.
3. Attempt to search for a value in the repeated field "list" using the "index" 
function.

What is the expected output?

Return the matching value from the repeated field list, or ValueError as 
appropriate. (The specified behavior of the "index" function.)

What do you see instead?

AttributeError: 'RepeatedCompositeFieldContainer' object has no attribute 
'index'

What version of the product are you using? On what operating system?

Protoc 2.5 on Windows 7.

Please provide any additional information below.

Repeated fields purport to generally function as python lists, in the duck 
typing sense. Not implementing support for something relatively basic like the 
"index" function definitely violates the principle of least surprise.

It appears that the solution would be as straightforward as adding the 
following method definition to RepeatedScalarFieldContainer in containers.py:

  def index(self, elem):
    """Finds the index of a given item in the list. Similar to list.index(elem)."""
    return self._values.index(elem)

Original issue reported on code.google.com by ajkinn...@gmail.com on 10 Jul 2014 at 7:11

GoogleCodeExporter commented 9 years ago
Correction to expected output:

Return the *index of the* matching value from the repeated field list, or 
ValueError as appropriate.

Original comment by ajkinn...@gmail.com on 10 Jul 2014 at 8:04