Python获取嵌套类的最终类

时间:2019-05-19 17:52:40

标签: python oop

我正在为我的世界构建Web应用程序创建家谱功能,并且正在使用从列表中获取的数据,我使用的示例是这样的:

            # PERSON                # SPOUSE             # MOTHER          # FATHER             # SEX       # HOUSE
listChars = [['Ned Stark',          'Catelyn Tully',    'Lyarra Stark',     'Rickard Stark',    'Male',     'Stark'],
             ['Catelyn Tully',      'Ned Stark',        'Minisa Whent',     'Hoster Tully',     'Female',   'Stark'],
             ['Robb Stark',         'Talisa Maegyr',    'Catelyn Tully',    'Ned Stark',        'Male',     'Stark'],
             ['Bran Stark',         'None',             'Catelyn Tully',    'Ned Stark',        'Male',     'Stark'],
             ['Sansa Stark',        'Tyrion Lannister', 'Catelyn Tully',    'Ned Stark',        'Female',   'Stark'],
             ['Tywin Lannister',    'Joanna Lannister', 'Jeyne Marbrand',   'Tytos Lannister',  'Male',     'Lannister'],
             ['Jaime Lannister',    'Cersei Lannister', 'Joanna Lannister', 'Tywin Lannister',  'Male',     'Lannister'],
             ['Cersei Lannister',   'Jaime Lannister',  'Joanna Lannister', 'Tywin Lannister',  'Female',   'Lannister'],
             ['Tyrion Lannister',   'Sansa Stark',      'Joanna Lannister', 'Tywin Lannister',  'Male',     'Lannister'],
             ['Joffrey Baratheon',  'None',             'Cersei Lannister', 'Jaime Lannister',  'Male',     'Lannister'],
             ['Myrcella Baratheon', 'None',             'Cersei Lannister', 'Jaime Lannister',  'Female',   'Lannister'],
             ['Tommen Baratheon',   'None',             'Cersei Lannister', 'Jaime Lannister',  'Male',     'Lannister'],
             ['Rickon Stark',       'None',             'Talisa Maegyr',    'Robb Stark',       'Male',     'Stark'],
             ['Lyanna Lannister',   'None',             'Sansa Stark',      'Tyrion Lannister', 'Male',     'Lannister']]

到目前为止,我已经创建了两个类:HouseholdFamilyHousehold类包含房屋名称,没有祖先/父母的房屋的顶级父母,以及顶级父母的子女。它还有一个families列表,其中包含顶级父母子女的家庭(Family类实例的列表)。 Family类具有其父母,房屋,孩子和家庭列表(适用于顶级父母的孩子的孩子)。这是我的两个类的代码:

class Family:
    def __init__(self,parents,house):
        self.parents=parents
        self.house = house
        self.children=self.getChildren()
        self.families=self.createFamilies()

    def getChildren(self):
        childs = [row[0] for row in listChars if (row[2]==self.parents[1] and row[3]==self.parents[0]) or (row[2]==self.parents[0] and row[3]==self.parents[1])]
        return childs

    def createFamilies(self):
        family_list = []                
        for child in self.children:
            childRow = [row for row in listChars if row[0]==child][0]
            # if child has no spouse, no family
            if childRow[1] != 'None':
                if family_list !=[]:
                    for f in family_list:
                        if childRow[0] not in family_list and childRow[0] not in f.parents: # deals with a Jaime/Cersei scenario
                            family_list.append(Family((childRow[0],childRow[1]),self.house))
                else:
                    family_list.append(Family((childRow[0],childRow[1]),self.house))
        return family_list

    def __repr__(self):
        return '<Family parents:%s, children:%s>' % (self.parents,self.children)

class Household:
    def __init__(self,house,parents):
        self.house=house
        self.parents=parents
        self.children = self.getChildren()
        self.families=self.createFamilies()

    def getChildren(self):
        childs = [row[0] for row in listChars if row[2]==self.parents[0] or row[3]==self.parents[1]]
        return childs

    def createFamilies(self):
        fams = []
        for child in self.children:
            childRow = [row for row in listChars if row[0]==child][0]
            if childRow[1] != 'None':
                fams.append(Family((childRow[0],childRow[1]),self.house))
        return fams

    def __repr__(self):
        return '<Household house:%s, top-parents:%s, children:%s>' % (self.house,self.parents,self.children)

然后我创建Household实例并将其放入household_list

parents = list(set([(r[2],r[3]) for r in listChars])) # list of parents, no duplicates
parentsTop = {}.fromkeys(parents)

names = [r[0] for r in listChars]

# TRUE if parents TOP of household, FALSE if not
parentsTop = {p:([True] if p[0] not in names and p[1] not in names else [False]) for p in parentsTop}

# for now, get House as fathers last name
for p in parentsTop:
    if parentsTop[p]==[True]:
        parentsTop[p].append(p[1].split(' ')[-1])

household_list=[Household(parentsTop[ps][1],ps) for ps in parentsTop if parentsTop[ps][0]==True]

此操作的目标是以一种可以将其读取到.yml文件的格式进行格式化,然后使用kingraph创建一个svg数据。

当前,我想知道如何访问家庭中的所有家庭。因此,例如,现在我知道,对于“兰尼斯特”家庭来说,它会下降为(正好是最后一个男性):“泰托斯”->“泰温”->“海梅”->乔佛里,所以有两个层次家庭实例的数量。考虑到我不知道有多少个级别,我将如何遍历所有Family实例?我感谢任何建议和正确方向的任何观点。谢谢!

0 个答案:

没有答案