yookyung1310 / labstreaminglayer

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

[pylsl] How to correctly iterate through xml descriptor siblings? #38

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
My outlet has 6 channels that I add to the XML descriptor as follows:

for channame in channels:
    info_xml = info1.desc()
    chn = info_xml.append_child("channel")
    chn.append_child_value("name", channame)
    chn.append_child_value("unit", "microvolts")
    chn.append_child_value("type", "generated")

In a separate process, I resolve the inlet then do the following:

inlet_info = inlet.info()
chans_xml = [inlet_info.desc().first_child(), ]
while chans_xml[-1].next_sibling(name="channel") is not None:
    chans_xml.append(chan_xml.next_sibling(name="channel"))

But this loop never ends. If I manually stop it (after half a million 
iterations) then I see that I can get the correct channel name out of 
[chans_xml[x].child_value("name") for x in range(6)], but everything beyond 
that has an empty channel name.

next_sibling is supposed to return None if there is no sibling, but somehow 
there is always an empty sibling.

Original issue reported on code.google.com by chadwick...@gmail.com on 16 Jun 2015 at 5:20

GoogleCodeExporter commented 8 years ago
There is a small typo in the last line of the above code. It should read:
chans_xml.append(chans_xml[-1].next_sibling(name="channel"))

The problem is that next_sibling is returning XMLElement(None), so it is always 
returning an XMLElement. Is that the desired behaviour? Am I expected to check 
to see if the returned XMLElement is not empty? Or would it be better if 
next_sibling returned None if nothing is found?

There might be a more general solution, but for now I modified pylsl.py as 
follows:

    def next_sibling(self,name=None):
        """Get the next sibling in the children list of the parent node.
        If a name is provided, the next sibling with the given name is returned.
        """
        if name is None:
            handle = lib.lsl_next_sibling(self.e)
        else:
            handle = lib.lsl_next_sibling_n(self.e,name)
        return XMLElement(handle) if handle else handle

Original comment by chadwick...@gmail.com on 16 Jun 2015 at 6:58