Add init method to lists
This commit is contained in:
@@ -115,6 +115,8 @@ cdef class _DynamicListBuilder:
|
|||||||
cpdef adopt(self, index, _DynamicOrphan orphan)
|
cpdef adopt(self, index, _DynamicOrphan orphan)
|
||||||
cpdef disown(self, index)
|
cpdef disown(self, index)
|
||||||
|
|
||||||
|
cpdef init(self, index, size)
|
||||||
|
|
||||||
cdef to_python_reader(C_DynamicValue.Reader self, object parent)
|
cdef to_python_reader(C_DynamicValue.Reader self, object parent)
|
||||||
cdef to_python_builder(C_DynamicValue.Builder self, object parent)
|
cdef to_python_builder(C_DynamicValue.Builder self, object parent)
|
||||||
cdef _to_dict(msg, bint verbose, bint ordered)
|
cdef _to_dict(msg, bint verbose, bint ordered)
|
||||||
|
|||||||
@@ -534,6 +534,17 @@ cdef class _DynamicListBuilder:
|
|||||||
"""
|
"""
|
||||||
return _DynamicOrphan()._init(self.thisptr.disown(index), self._parent)
|
return _DynamicOrphan()._init(self.thisptr.disown(index), self._parent)
|
||||||
|
|
||||||
|
cpdef init(self, index, size):
|
||||||
|
"""A method for initializing an element in a list
|
||||||
|
|
||||||
|
:type index: int
|
||||||
|
:param index: The index of the element in the list
|
||||||
|
|
||||||
|
:type size: int
|
||||||
|
:param size: Size of the element to be initialized.
|
||||||
|
"""
|
||||||
|
return to_python_builder(self.thisptr.init(index, size), self._parent)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return <char*>printListBuilder(self.thisptr).flatten().cStr()
|
return <char*>printListBuilder(self.thisptr).flatten().cStr()
|
||||||
|
|
||||||
|
|||||||
@@ -37,3 +37,6 @@ struct AddressBook {
|
|||||||
people @0 :List(Person);
|
people @0 :List(Person);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct NestedList {
|
||||||
|
list @0 :List(List(Int32));
|
||||||
|
}
|
||||||
|
|||||||
@@ -218,3 +218,16 @@ def test_to_dict_ordered(addressbook):
|
|||||||
else:
|
else:
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
person.to_dict(ordered=True)
|
person.to_dict(ordered=True)
|
||||||
|
|
||||||
|
def test_nested_list(addressbook):
|
||||||
|
struct = addressbook.NestedList.new_message()
|
||||||
|
struct.init('list', 2)
|
||||||
|
|
||||||
|
struct.list.init(0, 1)
|
||||||
|
struct.list.init(1, 2)
|
||||||
|
|
||||||
|
struct.list[0][0] = 1
|
||||||
|
struct.list[1][0] = 2
|
||||||
|
struct.list[1][1] = 3
|
||||||
|
|
||||||
|
assert struct.to_dict()["list"] == [[1], [2,3]]
|
||||||
|
|||||||
Reference in New Issue
Block a user