Skip to content
Advertisement

Pyyaml dump does not produce anchors for the same objects

I was experimenting a bit with PyYaml and I wanted to have a reference to a value appearing previously in the yaml. To give an example:

JavaScript

from what I understood from the specifications pyyaml should be adding an anchor to each object that has already been encountered. In my case, I would expect to have in the yaml file:

JavaScript

as the objects passed are exactly the same but instead, I find:

JavaScript

how can I obtain the desired behaviour?

Advertisement

Answer

First of all your expectation is incorrect. What you could expect is

JavaScript

with a space after the value indicator (:).

You also will need to do yaml.dump(dict_to_dump, sys.stdout) to get any output from your program, and what you indicate is not what you get (it again is missing spaces after the value indicator).


You normally only get an alias if you have two objects a and b with the same value for id(a) and id(b). Simple objects like integers and strings (that are reused from a pool) have the same id() even if assigned in different places in the source. Variable structures like a dict or list, or instances of Python classes do not usually have the same id().

PyYAML does know about this and handles some types of objects different even if the id() is the same.

JavaScript

which gives:

JavaScript

If you want to get the expected output, you have to make a Python class Int that behaves like an integer. And then when you do a = Int(25) you will get your anchor and alias.

This is what my library ruamel.yaml does, when loading in the default round-trip mode, it also preserves the actual anchor/alias used:

JavaScript

which gives:

JavaScript

To create data from scratch is also possible

JavaScript

which gives what you expected in the first place:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement