You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Line 830 of representer.py hints that it's possible to add comments to serialisation of python object instances.
Consider the following code. How would I modify it so that the resulting yaml is commented to look like this:
!C# Documentation for class Cdata: Ccontents: !B# Documentation for class Bdata: Bcontents: !A# Documentation for class Adata: Acontents:
Is that already possible or would that be a new feature?
importruamel.yamlfromruamel.yamlimportyaml_objectfromioimportStringIOimportcontextlibimportsys@contextlib.contextmanagerdefuncloseable(fd):
""" Context manager which turns the fd's close operation to no-op for the duration of the context. """close=fd.closefd.close=lambda: Noneyieldfdfd.close=closedefget_obj_as_yaml(yaml_inst, object):
# Use StringIO to capture the yaml outputs=StringIO()
withuncloseable(s):
yaml_inst.dump(object, s)
yaml_text=s.getvalue()
s.close()
returnyaml_textyaml=ruamel.yaml.YAML()
classSomeData:
def__init__(self, data):
self.data=dataself.contents=None@yaml_object(yaml)classA(SomeData):
"""This is an A object"""def__init__(self):
super().__init__("A")
@yaml_object(yaml)classB(SomeData):
"""This is a B object"""def__init__(self):
super().__init__("B")
self.contents=A()
@yaml_object(yaml)classC(SomeData):
"""This is a C object"""def__init__(self):
super().__init__("C")
self.contents=B()
# Create an instance of the top level object# which has other object instances insideinstance=C()
# Serialise the objects tree as yamltext=get_obj_as_yaml(yaml, instance)
sys.stdout.write(text)
The text was updated successfully, but these errors were encountered:
Line 830 of representer.py hints that it's possible to add comments to serialisation of python object instances.
Consider the following code. How would I modify it so that the resulting yaml is commented to look like this:
Is that already possible or would that be a new feature?
The text was updated successfully, but these errors were encountered: