Class YAMLObject

Synopsis

class YAMLObject(metaclass=YAMLObjectMetaclass)

Description

An object that can dump itself to a YAML stream and load itself from a YAML stream.

Mentioned in

Inheritance

Ancestors: YAMLObjectMetaclass, metaclass

Methods

from_yamlConvert a representation node to a Python object.
to_yamlConvert a Python object to a representation node.

Source

Lines 399-428 in lib3/yaml/__init__.py.

class YAMLObject(metaclass=YAMLObjectMetaclass):
    """
    An object that can dump itself to a YAML stream
    and load itself from a YAML stream.
    """

    __slots__ = ()  # no direct instantiation, so allow immutable subclasses

    yaml_loader = [Loader, FullLoader, UnsafeLoader]
    yaml_dumper = Dumper

    yaml_tag = None
    yaml_flow_style = None

    @classmethod
    def from_yaml(cls, loader, node):
        """
        Convert a representation node to a Python object.
        """
        return loader.construct_yaml_object(node, cls)

    @classmethod
    def to_yaml(cls, dumper, data):
        """
        Convert a Python object to a representation node.
        """
        return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
                flow_style=cls.yaml_flow_style)






Add Discussion as Guest

Log in