Skip to content

Commit

Permalink
Add init method to lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jparyani committed May 4, 2015
1 parent 3d77722 commit a5bf532
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions capnp/lib/capnp.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ cdef class _DynamicListBuilder:
cpdef adopt(self, index, _DynamicOrphan orphan)
cpdef disown(self, index)

cpdef init(self, index, size)

cdef to_python_reader(C_DynamicValue.Reader self, object parent)
cdef to_python_builder(C_DynamicValue.Builder self, object parent)
cdef _to_dict(msg, bint verbose, bint ordered)
Expand Down
11 changes: 11 additions & 0 deletions capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,17 @@ cdef class _DynamicListBuilder:
"""
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):
return <char*>printListBuilder(self.thisptr).flatten().cStr()

Expand Down
3 changes: 3 additions & 0 deletions test/addressbook.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ struct AddressBook {
people @0 :List(Person);
}

struct NestedList {
list @0 :List(List(Int32));
}
13 changes: 13 additions & 0 deletions test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,16 @@ def test_to_dict_ordered(addressbook):
else:
with pytest.raises(Exception):
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]]

0 comments on commit a5bf532

Please sign in to comment.