ros / xacro

Xacro is an XML macro language. With xacro, you can construct shorter and more readable XML files by using macros that expand to larger XML expressions.

Home Page:http://www.ros.org/wiki/xacro

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

load_yaml dot accessor breaks down in loops

Doomerdinger opened this issue · comments

I've notice that if I call (from python code, not within a xacro file) load_yaml the dot accessor does not work in for loops.

Given this yaml file:

---
list_ex:
  - name: foo
    val:
      x: 1
  - name: bar
    val:
      x: 2

The following will not work correctly:

b = 0
loaded_yaml = load_yaml(file)
for o in loaded_yaml.list_ex:
    b = b + o.val.x

It will complain that 'dict' has no value 'val'

However, the following will work fine

b = 0
loaded_yaml = load_yaml(file)
for x in range(0, len(loaded_yaml.list_ex)):
    o = loaded_yaml[x]
    b = b + o.val.x

Calling this code directly is not the intended use case, so this may not be worth addressing.

load_yaml uses two wrapper classes, YamlListWrapper and YamlDictWrapper to allow dotted access to elements. To this end, the actually retrieved element is wrapped within one of these classes depending on its type.
For now, element access is correctly implemented. To enable your use case, you would need to implement a wrapper for the iterator returned via __iter__() as well. Otherwise, the info that a wrapper is desired is lost. I will have a look...

Your change seems to make this work properly

Fixed via #318