I'm working on a simple example here, but the docs still leave me a bit confused.
Here is the Example code:
class A(yaml.YAMLObject):
yaml_tag = u'!A'
def __init__(self, val):
self.val = val
if __name__ == '__main__':
t = datetime.time()
a = A(t)
print yaml.dump(a)
print yaml.load(yaml.dump(a)).val == t
The output is
!A val: !!python/object/apply:datetime.time ["\0\0\0\0\0\0"]
True
So, it appears to be faithfully d-/serializing, but the default time object format leaves something to be desired. How can I make it prettier while preserving my mighty dump/load powers?
Thanks
Redefine time format for all times in a document
You can define your own serialization format for any
datetime.time
instances in your documents using PyYAML 'representers' and 'constructors'.Will output:
Redefine time format only for a particular class
You can also define how to a particular class serializes and deserializes. This way you can just change how time is represented for
A
only. Docs for YAMLObjectWill output: