I am using a function from a python library which returns an object with a specific data type. I would like to serialize that object to a yaml file and I would like to use ruamel.yaml. The problem is that ruamel.yaml
does not know how to serialize the specific data type that the function returns and throws an exception:
RepresenterError: cannot represent an object: <...>
The question is how to "declare" the data type to ruamel.yaml
so that it knows how to handle it.
Note: I can't / I don't want to make changes to the library or anything of that sort. I am only the consumer of an API.
To make this more concrete, let's use the following example that uses socket.AF_INET
which happens to be an IntEnum
but the specific data type should not be important.
import sys
import socket
import ruamel.yaml
def third_party_lib():
""" Return a dict with our data """
return {"AF_INET": socket.AF_INET}
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
yaml.dump(third_party_lib(), sys.stdout)
which gives this error:
ruamel.yaml.YAML.dump(self, data, stream, **kw)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/main.py", line 439, in dump
return self.dump_all([data], stream, _kw, transform=transform)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/main.py", line 453, in dump_all
self._context_manager.dump(data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/main.py", line 801, in dump
self._yaml.representer.represent(data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 84, in represent
node = self.represent_data(data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 111, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 359, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 222, in represent_mapping
node_value = self.represent_data(item_value)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 121, in represent_data
node = self.yaml_representers[None](self, data)
File "/home/feanor/Prog/git/vps-bench/.direnv/python-venv-3.7.2/lib/python3.7/site-packages/ruamel/yaml/representer.py", line 392, in represent_undefined
raise RepresenterError('cannot represent an object: %s' % data)
ruamel.yaml.representer.RepresenterError: cannot represent an object: AddressFamily.AF_INET
syaml.dump(third_party_lib(), sys.stdout)
instead of what you were doing, as documented that is slow and memory-inefficient. Here it just distracts from the real issue: that there is no representer registered ("declared") forAddressFamily
– Install