Dumping an object using a custom yaml tag in situations where it's hard or not possible to inherit from YAMLObject or override __repr__

import yaml

class MyDumper(yaml.Dumper): pass

class MyObjectWrapper:
    """
    Wrapper class for dumping any object with a custom tag via Dumper.represent_yaml_object.
    value must be an object that can be yaml dumped
    """

    def __init__(self, value):
        self.value = value

def my_custom_object_representer(dumper, data):
    return dumper.represent_yaml_object(
        "!MyCustomTag",
        MyObjectWrapper( transmogrify(data) ), # or data.transmogrify()
        MyObjectWrapper,
    )

MyDumper.add_representer(MyHardToRepresentClass, my_custom_object_representer)

# Result is:
#
#  !MyCustomTag:
#      value: my_transmogrified_value
#