Is it possible to emit valid YAML with anchors / references disabled using Ruby or Python?
Asked Answered
I

1

5

Is it possible to disable creating anchors and references (and effectively list redundant data explicitly) either in PyYAML or Ruby's Psych engine?

Perhaps I missed something while searching the web, but it seems there are not many options available in Psych and I was not able to determine if PyYAML allows for that either.

The rationale is I have to serialize some data and pass it in a readable form to a not-really-technical co-worker for manual validation. Some data is redundant but I need it listed in a most explicit manner for readability (anchors and references are a nice concept for efficiency, but not for human-readability).

Ruby and Python are my tools of choice, but if there is some other reasonably simple way of 'unfolding' YAML documents, it might just do.

Infuscate answered 9/1, 2014 at 9:29 Comment(6)
could you explain that with the expamples...? I don't see the connection of web, yaml, and anchors.Club
@majioa There's no 'web' involved - anchor in this context is an internal element of YAML syntax. See wiki. It's apparently possible to disable them by duplicating objects (i.e. they are certainly used in case of object identity, but it seems I can't get a consistent pattern in case of object equality, e.g. for Ruby arrays), but it becomes a tedious task in case of deeply nested structures. I didn't try deep-copy libraries though.Infuscate
I don't see the problem with storing/reading the array, they will have elements as you specify in a YAML file. Or you mean something else?Club
I see, you want to reuse a YAML code, right?Club
I just tried to load YAML file that is specified in example in the wiki. It correctly use anchors.Club
@majioa The point is I don't want them. Apparently defining your own dumper (while using PyYAML) and setting ignore_aliases to True, as described in Brett's answer, disables anchors and renders explicit (if redundant / duplicate) data, just as I need it.Infuscate
M
9

I found this related ticket on the PyYAML website (http://pyyaml.org/ticket/91), it looks like anchors can be disabled by using a custom dumper along the lines of:

import yaml

class ExplicitDumper(yaml.SafeDumper):
    """
    A dumper that will never emit aliases.
    """

    def ignore_aliases(self, data):
        return True

So, for example, the following outputs can be achieved using the standard dumper and the new explicit dumper:

>>> yaml.dump([1L, 1L])
"[&id001 !!python/long '1', *id001]\n"

>>> yaml.dump([1L, 1L], Dumper=ExplicitDumper)
'[1, 1]\n'

You can customise further properties to ensure pretty-printing etc. in the yaml.dump(...) call.

Mortie answered 9/1, 2014 at 11:33 Comment(2)
For all I see it works very well. Thank you - +1 and accept from me ;)Infuscate
For Ruby: #24508864Namely

© 2022 - 2024 — McMap. All rights reserved.