Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 配置继承机制_Python_Inheritance_Recursion - Fatal编程技术网

Python 配置继承机制

Python 配置继承机制,python,inheritance,recursion,Python,Inheritance,Recursion,我的结构如下: config |-- groups |-- rootgroup |-- group1 (includes rootgroup) |-- group2 (includes group1) |-- group3 (includes rootgroup) |-- users |-- Fred (includes group3 and group2) 配置 |--团体 |--根群 |--组1(包括根组) |--组2(包括组1) |--组3(

我的结构如下:

config |-- groups |-- rootgroup |-- group1 (includes rootgroup) |-- group2 (includes group1) |-- group3 (includes rootgroup) |-- users |-- Fred (includes group3 and group2) 配置 |--团体 |--根群 |--组1(包括根组) |--组2(包括组1) |--组3(包括根组) |--使用者 |--Fred(包括第3组和第2组) 因此,Fred的继承树如下所示:

_Fred_ v v group2 group3 v v group1 v v / rootgroup _弗雷德_ v v 第2组第3组 v v 第1组v 五/ 根群 我需要一个算法来打印从树的左下角开始的线性配置读取顺序(例如,它将是rootgroup-group1-group2-group3;group1覆盖rootgroup,group2覆盖group1,等等),并查找递归链接(例如,如果rootgroup包括group2),而且它必须找到递归循环(…->group2->group1->rootgroup->group2->…)

首选的语言是python,但任何语言都可以


谢谢。

在阅读了有向无环图(DAG)之后,我提出了以下解决方案:

def getNodeDepsTree(self, node, back_path=None):
    """Return whole dependency tree for given node"""
    # List of current node dependencies
    node_deps = []

    if not back_path:
        back_path = []

    # Push current node into return path list
    back_path.append(node)

    # Get current node dependencies list
    deps = getNodeDeps(node)

    for dep in deps:
        # If dependency persist in list of visited nodes - it's a recursion
        if dep in back_path:
            pos = back_path.index(dep)
            self.log.error("Recursive link detected. '" + node
                    + "', contains a recursive link to '" + dep
                    + "'. Removing dependency. Recursive loop is:\n\t"
                    + '\n\t'.join(back_path[pos:]))
            continue
        # Recursively call self for child nodes
        node_deps.extend(self.getNodeDepsTree(dep, back_path))

    # Finally add current node as dependency
    node_deps.append(node)

    # Remove current node from list of visited nodes and return
    back_path.pop()

    return node_deps

您现在如何存储关系?它现在是一个文本,每行有obj,表格深度表示“子对象”关系,还是您考虑了一些基于类/对象的关系存储?您现在呈现源结构的方式(|--group1(包括rootgroup))不利于解析。很抱歉,目前它是一个文件结构。每个组都是一个目录,其中包含一个文件,当前组依赖项每行列出一个。