1

I am using the code from the answered question found here.

What would be the most pythonic way to store information about marriage? I would like it to be possible to ask for self.husband or self.wife from any married person stored in a class, and ask for self.children from these recursively.

class Person:
    ID = itertools.count()
    def __init__(self, name, parent=None, level=0):
        self.id = self.__class__.ID.next() # next(self.__class__.ID) in python 2.6+
        self.parent = parent
        self.name = name
        self.level = level
        self.children = []

def createTree(d, parent=None, level=0):
    if d:
        member = Person(d['parent'], parent, level)
        level = level + 1
        member.children = [createTree(child, member, level) for child in d['children']]
        return member

t = createTree(my_tree)  # my_tree is the name of thedictionary storing parents and children. Need 'parent' key to become 'parents' key which stores a list of two people.
Community
  • 1
  • 1
Samuel
  • 157
  • 6
  • 22

1 Answers1

0

Rather than trying to come out with a good model by yourself, what you're looking at here is a directed graph. I'd use networkx to create the graph. This will give you a bunch of tools for searching through the graph, displaying, getting stats out of it. So you can create a person as a node and then create edges to the parents and children. You can add restrictions to the edges creation if you want later on. https://networkx.github.io/documentation/latest/tutorial/tutorial.html

zom-pro
  • 1,571
  • 2
  • 16
  • 32