Browse Source

added PersonCollection.get_coparents

Fabian Peter Hammerle 4 years ago
parent
commit
7eda3f1e0b
1 changed files with 30 additions and 0 deletions
  1. 30 0
      yamily/__init__.py

+ 30 - 0
yamily/__init__.py

@@ -235,6 +235,36 @@ class PersonCollection:
             return self.__children[parent]
         return self.get_children(parent.identifier)
 
+    def get_coparents(
+        self, parent: typing.Union[Person, str]
+    ) -> typing.Iterator[Person]:
+        """
+        >>> c = PersonCollection()
+
+        >>> alice = Person('alice')
+        >>> alice.father = Person('bob')
+        >>> c.add_person(alice)
+        Person(alice)
+        >>> list(c.get_coparents('bob'))
+        []
+
+        >>> alice.mother = Person('carol')
+        >>> c.add_person(alice)
+        Person(alice)
+        >>> list(c.get_coparents('bob'))
+        [Person(carol)]
+        >>> list(c.get_coparents(c['carol']))
+        [Person(bob)]
+        """
+        if isinstance(parent, Person):
+            for coparent in self.get_coparents(parent.identifier):
+                yield coparent
+            return
+        for child in self.get_children(parent):
+            for coparent in child.parents:
+                if coparent.identifier != parent:
+                    yield coparent
+
     def __iter__(self) -> "PersonCollection":
         """
         >>> c = PersonCollection()