|
@@ -11,6 +11,43 @@ class _Loader(yaml.SafeLoader):
|
|
|
|
|
|
|
|
|
|
|
|
+ """
|
|
|
+ >>> alice_yaml = '''
|
|
|
+ ... !person
|
|
|
+ ... identifier: alice
|
|
|
+ ... name: Alice Test
|
|
|
+ ... birth_date: 1976-02-01
|
|
|
+ ... mother: mum
|
|
|
+ ... father: dad
|
|
|
+ ... '''
|
|
|
+ >>> alice = yaml.load(alice_yaml, Loader=_Loader)
|
|
|
+ >>> alice
|
|
|
+ Person(alice, Alice Test, *1976-02-01)
|
|
|
+ >>> alice.mother
|
|
|
+ Person(mum)
|
|
|
+ >>> alice.father
|
|
|
+ Person(dad)
|
|
|
+
|
|
|
+ >>> alice_yaml = '''
|
|
|
+ ... !person
|
|
|
+ ... identifier: alice
|
|
|
+ ... name: Alice Test
|
|
|
+ ... birth_date: 1976-02-01
|
|
|
+ ... mother: !person
|
|
|
+ ... identifier: mum
|
|
|
+ ... father: !person
|
|
|
+ ... identifier: dad
|
|
|
+ ... name: Dad Test
|
|
|
+ ... '''
|
|
|
+ >>> alice = yaml.load(alice_yaml, Loader=_Loader)
|
|
|
+ >>> alice
|
|
|
+ Person(alice, Alice Test, *1976-02-01)
|
|
|
+ >>> alice.mother
|
|
|
+ Person(mum)
|
|
|
+ >>> alice.father
|
|
|
+ Person(dad, Dad Test)
|
|
|
+ """
|
|
|
+
|
|
|
def __init__(self, stream):
|
|
|
super().__init__(stream)
|
|
|
self.add_constructor("!person", self._construct_person)
|
|
@@ -24,9 +61,15 @@ class _Loader(yaml.SafeLoader):
|
|
|
if "birth_date" in person_attrs:
|
|
|
person.birth_date = person_attrs["birth_date"]
|
|
|
if "mother" in person_attrs:
|
|
|
- person.mother = Person(person_attrs["mother"])
|
|
|
+ if isinstance(person_attrs["mother"], Person):
|
|
|
+ person.mother = person_attrs["mother"]
|
|
|
+ else:
|
|
|
+ person.mother = Person(person_attrs["mother"])
|
|
|
if "father" in person_attrs:
|
|
|
- person.father = Person(person_attrs["father"])
|
|
|
+ if isinstance(person_attrs["father"], Person):
|
|
|
+ person.father = person_attrs["father"]
|
|
|
+ else:
|
|
|
+ person.father = Person(person_attrs["father"])
|
|
|
return person
|
|
|
|
|
|
|