Browse Source

added PersonCollection.get_coparents_recursively

Fabian Peter Hammerle 4 years ago
parent
commit
fe01ce2145
2 changed files with 30 additions and 0 deletions
  1. 2 0
      CHANGELOG.md
  2. 28 0
      yamily/__init__.py

+ 2 - 0
CHANGELOG.md

@@ -5,5 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## Unreleased
+### Added
+- `PersonCollection.get_coparents_recursively(person)`
 
 ## 0.1.0 - 2020-01-02

+ 28 - 0
yamily/__init__.py

@@ -280,6 +280,34 @@ class PersonCollection:
                     coparents.add(coparent)
         return coparents
 
+    def get_coparents_recursively(
+        self, parent: typing.Union[Person, str]
+    ) -> typing.Set[Person]:
+        """
+        >>> c = PersonCollection()
+        >>> child_a = Person("child-a")
+        >>> child_a.father = Person("father")
+        >>> child_a.mother = Person("mother-a")
+        >>> c.add_person(child_a)
+        Person(child-a)
+        >>> child_b = Person("child-b")
+        >>> child_b.father = Person("father")
+        >>> child_b.mother = Person("mother-b")
+        >>> c.add_person(child_b)
+        Person(child-b)
+        >>> sorted(c.get_coparents_recursively("mother-a"), key=lambda p: p.identifier)
+        [Person(father), Person(mother-a), Person(mother-b)]
+        >>> c.get_coparents_recursively("child-a")
+        set()
+        """
+        coparents = self.get_coparents(parent)
+        last_len = None
+        while len(coparents) != last_len:
+            for coparent in set(coparents):
+                coparents.update(self.get_coparents(coparent))
+            last_len = len(coparents)
+        return coparents
+
     def __iter__(self) -> "PersonCollection":
         """
         >>> c = PersonCollection()