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.
http://www.ros.org/wiki/xacro
BSD 3-Clause "New" or "Revised" License
85 stars 99 forks source link

load_yaml dot accessor breaks down in loops #317

Closed Doomerdinger closed 2 years ago

Doomerdinger commented 2 years ago

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.

rhaschke commented 2 years ago

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

Doomerdinger commented 2 years ago

Your change seems to make this work properly

rhaschke commented 2 years ago

Fixed via #318