Re: [capnproto] Initialize list of lists

404 views
Skip to first unread message

Jason Paryani

unread,
May 4, 2015, 2:06:39 PM5/4/15
to davidr...@gmail.com, capnproto
Sorry, this was an oversight that is now fixed (https://github.com/jparyani/pycapnp/commit/a5bf532d53db37414d5e6b25f139ef237a854cc1). Thanks for reporting it!

However, you may not even need to use `init` in your case. A much simpler way to write this code is to set the field directly with the list, like so:

proto.myList = [[1], [2,3], [4,5,6]]

As long as the `self._myList` type in your examples is a Python built-in list() object, then you could do the following:

proto.myList = self._myList

On Mon, May 4, 2015 at 9:50 AM, <davidr...@gmail.com> wrote:
Hi guys,

I'm trying initialize a list of a lists in python but when I try initialize the sublist, pycapn emits this error:

AttributeError: 'capnp.lib.capnp._DynamicListBuilder' object has no attribute 'init'


My code (problematic line in blue):

myListProto = proto.init("myList", len(self._myList))
for i in xrange(len(self._myList)):
  subListProto = myListProto.init(i, len(self._myList[i]))
  for j in xrange(len(self._myList[i])):
    subListProto[i][j] = self._myList[i][j]



My proto class:

struct MyClassProto
{
  myList @0 :List(List(UInt32));
}


In sumary, how I initialize a sublist of variable length?

Thanks in advance,

David

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
Visit this group at http://groups.google.com/group/capnproto.

Kamal Marhubi

unread,
May 4, 2015, 6:50:40 PM5/4/15
to Jason Paryani, davidr...@gmail.com, capnproto
Jason—would you accept a patch to make that assignment work with any instance of `collections.Sequence`? It would increase pythonicity to not require it be a `__builtin__.list`.

Jason Paryani

unread,
May 4, 2015, 7:25:33 PM5/4/15
to Kamal Marhubi, davidragazzi, capnproto
I'd be open to it, but add them to the bottom of the if/else type checking chain since isinstance takes close to 100x as long as the current type checking. Take a look at https://github.com/jparyani/pycapnp/blob/a5bf532d53db37414d5e6b25f139ef237a854cc1/capnp/lib/capnp.pyx#L686 to see the logic for this. While you're at it, we should add the same for dict->collections.Mapping.

Kamal Marhubi

unread,
May 4, 2015, 7:31:10 PM5/4/15
to Jason Paryani, davidragazzi, capnproto
Sounds good. I'd found that part of the code from your patch. :-)

So are you suggesting that the special case for `list` and `dict` be left in place for performance reasons? Makes sense to me; just checking.

Jason Paryani

unread,
May 4, 2015, 7:32:42 PM5/4/15
to Kamal Marhubi, davidragazzi, capnproto
Yep, leave them in as fast paths.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages