在python中构建依赖图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14242295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:50:57  来源:igfitidea点击:

Build a dependency graph in python

pythongraphdependencies

提问by Johny19

I was wondering if python has some built-in library (or any library on the net..) That will create for for me a graph of dependencies ? I have a file formatted like that

我想知道 python 是否有一些内置库(或网络上的任何库..)这将为我创建一个依赖关系图?我有一个这样格式的文件

A::Requires         = ""
B::Requires     = A
C::Requires     = B
H::Requires     = A

AA::Requires         = ""
BB::Requires         = AA
C::Requires     = B

CC::Requires    = BB

Ideally I would like to have something like a tree like that:

理想情况下,我想要像这样的树:

A
 +-B
   +-C
 +-H

AA
 +-BB
   +-CC

So basically A lib where I will provide a tuple (A,B) or (A,H) and it will build the tree for me? If such a lib doesn't exist, what would be the easier way to accomplish something like that?

所以基本上是一个库,我将在其中提供一个元组 (A,B) 或 (A,H),它会为我构建树?如果这样的库不存在,那么完成这样的事情的更简单方法是什么?

Thank you

谢谢

采纳答案by Belphegor

Try with one of the several ones:

尝试使用以下几种方法之一:

graph-toolis very difficult to install (it needs a lot of memory for compilation, I think it was around 5GB of RAM and around 12 hours of compilation).

graph-tool很难安装(它需要大量内存来编译,我认为它大约有 5GB 的 RAM 和大约 12 小时的编译时间)。

networkxis pretty decent.

networkx相当不错。

igraphquote from their page: igraph is a free software package for creating and manipulating undirected and directed graphs. It includes implementations for classic graph theory problems like minimum spanning trees and network flow, and also implements algorithms for some recent network analysis methods, like community structure search.

igraph引用自他们的页面:igraph 是一个免费软件包,用于创建和操作无向图和有向图。它包括经典图论问题的实现,如最小生成树和网络流,还实现了一些最近的网络分析方法的算法,如社区结构搜索。

I've been using all of them. It really depends on what exactly do you need. If you need them for something as simple as dependencies, then it really is not important which one you are going to use, though, I would recomend you to avoud graph-tool if you need it for something shorter and lighter.

我一直在使用它们。这真的取决于你到底需要什么。如果你需要它们来处理像依赖这样简单的事情,那么你要使用哪个并不重要,但是,如果你需要更短更轻的东西,我会建议你避免使用图形工具。

回答by Theodros Zelleke

Assuming your input from above is given as a string in raw:

假设您从上面的输入以字符串形式给出raw

import networkx as nx
import re

regex = re.compile(r'^([A-Z]+)::Requires\s+=\s([A-Z"]+)$')

G = nx.DiGraph()
roots = set()
for l in raw.splitlines():
    if len(l):
        target, prereq = regex.match(l).groups()
        if prereq == '""':
            roots.add(target)
        else:
            G.add_edge(prereq, target)

Now print the tree(s):

现在打印树:

for s in roots:
    print s
    spacer = {s: 0}
    for prereq, target in nx.dfs_edges(G, s):
        spacer[target] = spacer[prereq] + 2
        print '{spacer}+-{t}'.format(
                                     spacer=' ' * spacer[prereq],
                                     t=target)
    print ''

this prints:

这打印:

A
+-H
+-B
  +-C

AA
+-BB
  +-CC

This requires that allroots are presented through root::Requires = ""in order for them to be identified as such.

这要求所有的词根都被呈现出来root::Requires = "",以便它们被识别出来。

回答by nathan-cloudright.co.uk

Graphvizis great for building documentation of dependencies in an automated manner.

Graphviz非常适合以自动化方式构建依赖关系文档。

There's a useful Python library too called pygraphviz

有一个有用的 Python 库也称为pygraphviz

I use this to build up a dependency map then output in both text form and as a visual that automatically exports to PDF.

我使用它来构建依赖关系图,然后以文本形式和自动导出为 PDF 的视觉效果输出。