I need to record a scene hierarchy to a YAML file and then replicate it in the exact order in Maya. The real data is more complex tree and iteration is recursive function, but this is simplified version:
import yaml
dictionary = {
"PARENT": {"B": {}, "A": {}, "C": {}, "D": {}}
}
data = yaml.safe_load(yaml.dump(dictionary))
for key_parent, value_parent in data.iteritems():
for key_child, value_child in value_parent.iteritems():
print key_child
The result is A C B D. Why I am not getting B A C D? Is there a way to control this?
Also, if I print yaml.dump(dictionary) I will get:
PARENT:
A: {}
B: {}
C: {}
D: {}
which looks like an alphabetically sorted source dictionary. Why during iteration I don’t get sorted output?
In Python, dictionaries are arbitrarily ordered, so when you define dictionary, the B, A, C, D ordering gets lost.
If you want to look up how dictionaries work, the generic term for them is a “Hash Map” or “Hash table”: Hash table - Wikipedia
So parsing YAML in Pyhton is the same as a parsing dictionary? OrderedDict should be used for YAML generation as well, or just reading would be enough?
I don’t know my way around YAML very well, but from what I can find, yes.
I’ve found in a StackOverflow question, and a pyyaml PR that the yaml spec does not guarantee an order of keys in a mapping.
I found in the PyYAML PR that it automatically sorts the keys when you run .dump(), so the YAML file itself is already in a different order.
Then I read the SO accepted answer, and it explains how to create an extension that preserves order.
All that said fact that you are creating dictionary as a dict in your example means that the keys aren’t in the order you’re defining before you even dump them to YAML.
Yeah, 3.6 is when they started tracking insertion order by default. There are still some behavioral differences between dict and OrderedDict but for simple things they are pretty interchangeable.