Initialize list of lists

355 views
Skip to first unread message

davidr...@gmail.com

unread,
May 4, 2015, 12:58:18 PM5/4/15
to capn...@googlegroups.com
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

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

--
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.

David Ragazzi

unread,
May 4, 2015, 7:40:31 PM5/4/15
to Kamal Marhubi, Jason Paryani, capnproto
Thanks for the quick response.

This direct approach didn't worked for me at first time:


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

But the problem is that I was converting a deque to list only using list(), now I'm converting manually and then settinh the proto with the generated list.

Thanks!
--
David Ragazzi
MSc in Sofware Engineer (University of Liverpool)
OS Community Commiter at Numenta.org
--
"I think James
Connolly, the Irish revolutionary, is right when he says that the only prophets are those who make their future. So we're not anticipating, we're working for it."
Reply all
Reply to author
Forward
0 new messages