如何在Jython中迭代递归类?

2024-10-01 19:33:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Jython中创建了一个类,它在顶级接受一个对象,然后递归地遍历所有子对象(来自外部系统),从而有效地为我提供一个嵌入的对象树。由此,我可以使用类似的技术列出子级的相关属性,只需使用递归。你知道吗

然而,我现在需要一个从调用程序中遍历这棵树的方法,在这里我可以使用一个for循环来递归遍历所有的子对象,而我却无法理解这个问题!你知道吗

有什么建议吗?你知道吗

代码:

class DepotGroup():

    def __init__(self, jli, groupName, groupFQPath, groupID, parentID=0):
        self.logger = clog.CoBaLogging("Class:DepotGroup")
        self._jli = jli
        self.name = groupName
        self.fullyQualifiedPath = groupFQPath
        self.ID = groupID
        self.parentID = parentID

        # Create and populate the children[] list
        self.children = []
        self._FindChildren()

        # Now do the same for Depot Objects
        self.depotObjects = []
        self._FindDepotObjects()

        return


    def AddChild(self, childGroupName, childGroupFQPath, childGroupID):
        child = DepotGroup(self._jli, childGroupName, childGroupFQPath, childGroupID, self.ID)
        self.children.append(child)

        return

    def _FindChildren(self):
        (ok, childList) = self._jli.cliCall("Group", "findAllByParentGroup", DEPOT_GROUP, self.ID)
        if not ok:
            raise EnvironmentError("BLCLI call 'Group.findAllByParentGroup()' failed")

        for child in childList:
            (ok, groupPath) = self._jli.cliCall("Group", "getStringPathToGroup", child.groupId)
            if not ok:
                raise EnvironmentError("BLCLI call 'Group.getStringPathToGroup()' failed")
            self.AddChild(child.name, groupPath[5:], child.groupId)

        return

    def _FindDepotObjects(self):
        (ok, objList) = self._jli.cliCall("DepotObject", "findAllByGroup", self.ID, False)
        if not ok:
            raise EnvironmentError("BLCLI call 'DepotObject.findAllByGroup()' failed")

        for depotObj in objList:
            self.depotObjects.append(DepotObject(depotObj.name, depotObj.objectId, depotObj.DBKey, depotObj.objectTypeId))

        return

    def __list(self, indent=""):
        if indent=="":
            print "%s" % self.fullyQualifiedPath
        else:
            print "%s/%s" % (indent, self.name)

        childIndent = "%s    " % indent
        for child in self.children:
            child.__list(childIndent)

        for depotObj in self.depotObjects:
            flag = ""
            if depotObj.IsBLPackage():
                flag = "*"

            print "%s  - %s %s" % (indent, depotObj.name, flag)

        if indent=="":
            print "\n\n[Note: '*' indicates a BLPackages]\n"

        return

    def list(self):
        self.__list()
        return

    def __repr__(self):
        thisNode = {'Name': self.name, 'FQPath': self.fullyQualifiedPath, 'GroupID': self.ID, 'Children': []}
        for child in self.children:
            thisNode['Children'].append(child)

        return repr(thisNode)

    def __str__(self):
        retVal = ""
        retVal += "Name       : %s" % self.name
        retVal += "\nFQPath     : %s" % self.fullyQualifiedPath
        retVal += "\nGroup ID   : %s" % str(self.ID)
        retVal += "\n# Children : %d" % len(self.children)

        return retVal

    def __len__(self):
        totalLen = len(self.children)

        for child in self.children:
            totalLen += len(child)

        return totalLen




class DepotObject():

    def __init__(self, objName, objID, objDBKey, objType):
        self.name = objName
        self.ID = objID
        self.DBKey = objDBKey
        self.type = objType

    def IsBLPackage(self):
        return (self.type == BLPACKAGE)

Tags: nameinselfidchildforreturnif

热门问题