• Project Info
  • Language
  • Python
  • License
  • MIT
  • Version Released
  • Jan. 20, 2021
  • Docs Built
  • May 30, 2021
  • Latest Version
  • master
  • Collaborators
  • admin
  • Tags
  • yaml

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)



Discussions (1)

bena 3 months ago 0

Is it possible to define a YAMLObject or a resolver for a YAMLObject that can handle lists of maps?

e.g.

class foo_data(yaml.YAMLObject):
    yaml_tag = "!foo_data"

    def __init__(self, some, data):
        .
        .
        .
foo:
    - some: ""
      data: ""

    - some: ""
      data: ""

I can do it if I include the yaml tag defined in the class, but I'd prefer not to add more syntax for users to remember and for me to document. e.g.

foo:
    -  !foo_data
      some: ""
      data: ""

    -  !foo_data
       some: ""
       data: ""



Add Discussion as Guest

Log in