Now that the anchors survive a round trip with standard loading and dumping, how would I preserve them with custom from_yaml() and to_yaml() methods: import sys import ruamel.yaml class B: def __init__(self, a=None, b=None): self.a = a self.b = b self.c = a*b @classmethod def from_yaml(cls, constructor, node): data = dict(constructor.construct_pairs(node)) return cls(**data) @classmethod def to_yaml(cls, representer, node): data = {'a': node.a, 'b': node.b} return representer.represent_mapping( getattr(cls,...
Thank you for that really fast fix!! Because you mentioned, that attribute used, so I looked up its name. It is _yaml_anchor, isn't it (can this be mentioned in the documentation?). I guess, the changes are slim to use this attribute in your code, nevertheless I wonder, if it would not be better to start it with a double underscore?
Thanks for that really fast fix!! It works for my code which uses attr.s, but I noticed, that it is not working for this: import ruamel.yaml s = """ a: b: !BClass c: 1 """ class BClass: def __init__(self): self.c = c self.c2 = c*2 yaml = ruamel.yaml.YAML() yaml.register_class(BClass) yaml.load(s) y['a']['b'].c2 or any other class relying on __init__(). I also did not stumble upon this behaviour mentioned in the documentation. Have I missed it?
attrs and ruamel.yaml
Thanks. If you send me a patch/diff, I am happy to test it with my application.
Ouch! Not just a past error of the code, but also some write-up error. Sorry! Now let us add a tag: and the code block following should become: Now let us register the BClass: import sys import ruamel.yaml class BClass: def __init__(self): self.c = c s = """ a: b: &B !BClass c: 1 d: e: *B """ yaml = ruamel.yaml.YAML() yaml.register_class(BClass) y = yaml.load(s) yaml.dump(y, sys.stdout) This should give the output mentioned.
&anchor name lost, if !tag specified
I use 0.7.0 and have the same problem.