I want to have a custom function that serializes arbitrary python objects, like how the json.dump function has an optional arg called 'default', that should be a function that the json dumper will call if the object is not json serializable.
I simply want to do the equivalent of this from the json package.
json.dump(tests_dump, file('somefile', 'w+'), default = lambda x: x.__dict__)
It looks like I need to write yaml.add_representer, from the PyYAML docs, but it really isn't clear how to do this.
Here is a sample for add_representer. Not sure if this is exactly what you want. Nevertheless ...
Of course there is code duplication and the code is not optimized. You can make these two functions part of your Class, and also implement
__repr__
to get a printable representation that you can use to populateserializedData
inMyClass_representer