'Pyyaml: Default dump behavior for inherited classes
I have a class that inherits from one of the built-ins:
from yaml import YAMLObject, dump
class D(dict, YAMLObject):
yaml_tag = u'!!map'
...
Is there a way to tell pyyaml that the dump of any instance of D should be treated like the parent (in this case dict)? I.e., I would like the following output:
d = {'a':1}
print( dump(d) )
>>> a: 1
Instead of
print( dump(D(d)) )
>>> !%21map {}
As you can see I already tried assigning the class a default tag, but that did not quite work out. Defining a custom dumper is unfortunately not an option.
Solution 1:[1]
Short addition: Converting the data back to a dict first before dumping it is another elegant solution I completely missed:
d = D({'a':1})
print( dump(dict(d)) )
>>> a: 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Titor |
