def dep(arg):
'''
Dependency resolver
"arg" is a dependency dictionary in which
the values are the dependencies of their respective keys.
'''
d=dict((k, set(arg[k])) for k in arg)
r=[]
while d:
# values not in keys (items without dep)
t=set(i for v in d.values() for i in v)-set(d.keys())
# and keys without value (items without dep)
t.update(k for k, v in d.items() if not v)
# can be done right away
r.append(t)
# and cleaned up
d=dict(((k, v-t) for k, v in d.items() if v))
return r
if __name__=='__main__':
d=dict(
a=('b','c'),
b=('c','d'),
e=(),
f=('c','e'),
g=('h','f'),
i=('f',)
)
print dep(d)
我确实编写了一个工具来查找和绘制PyPi上Python包之间的依赖关系。是gluttony
我也曾经分析过我正在使用的库的依赖关系。以下是一些图表:
我不确定这是你想要的。如果是,可以读取源代码here,这是一个开源项目。对于更多依赖关系图,可以查看gallery
谈到如何实现它,为了查找包依赖项,我使用pip作为后端。对于绘制图表,我使用Networkx。
This可能会引起一些兴趣:
图论是一条路。
图只是一堆中间有路(边)的地方(顶点),特别是我们所说的有向图,意思是单向路。找出依赖关系,基本上意味着,找出所有的地方,可以到达一个特定的城镇沿着单行道。
现在,你得到了一堆模块,它们成为你的顶点。假设我们有A和B,我们知道B依赖于A,所以有一条有方向的边——一条“单行道”——从A到B
如果C依赖于B,那么你有A→B→C
在形式上,图只是顶点和(有序的)顶点对的集合,称为边。你想要一个叫做“拓扑排序”的图算法,现在你有一些东西要读。
相关问题 更多 >
编程相关推荐