依赖树实现
对于那些使用 apt-get 的人来说,您知道每次安装/卸载某些东西时,您都会收到通知说您需要/不再需要某些依赖项。
我正在尝试理解其背后的理论,并可能实现我自己的版本。我做了一些谷歌搜索,想出了大部分耦合的东西。据我了解,耦合是两个相互依赖的类/模块。这不正是我正在寻找的。我正在寻找的更像是依赖关系树生成,我可以在其中找到最不依赖的模块(我已经采用了递归方式来执行此操作),并且(这是我尚未完成的部分)找到什么删除节点后不再需要。
另外,学习图论会有帮助吗?有没有关于最好使用 Python 作为语言的教程?
For those who used apt-get, you know that everytime you install / uninstall something, you get the notificatons saying you need / no longer need certain dependencies.
I'm trying to understand the theory behinds this and potentially implement my own version of this. I've done some googling, came up with mostly coupling stuff. From what I understand, coupling is 2 classes/modules that depends on each other. This is not exactly what I'm looking for. What I'm looking for is more like a dependencies tree generation, where I can find the least dependent module (I've already made a recursive way of doing this), and (this being the part i haven't done) finding what's no longer needed after removing a node.
Also, would learning about graph theory help? And is there any tutorials on that preferably using Python as a language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可能会引起一些兴趣:
This might be of some interest:
图论是一条出路。
图只是一堆地方(顶点),它们之间有道路(边),特别是我们谈论的是有向图,这意味着单向道路。基本上,找出依赖关系意味着找出沿着单向道路可以到达特定城镇的所有地点。
所以现在,你已经有了一堆模块,它们成为你的顶点。假设我们有 A 和 B,并且我们知道 B 依赖于 A,因此存在从 A 到 B 的有向边——一条“单向路”。
如果 C 依赖于 B,则有 A→B→C 。
形式上,图只是顶点和(有序)顶点对(称为边)的集合。您想要一种称为“拓扑排序”的图形算法,现在您需要阅读一些内容。
Graph theory is the way to go.
A graph is just a bunch of places (vertices) with roads (edges) between them, and in particular we're talking about a directed graph, which means one-way roads. Figuring out dependencies means, basically, finding out all the places that can reach a particular town along the one-way roads.
So now, you've got you bunch of modules, which become your vertices. Let's say we have A and B, and we know B depends on A, so there's a directed edge -- a "one way road" -- from A to B.
If C depends on B, then you have A→B→C.
Formally, a graph is just a collection of vertices and (ordered) pairs of vertices, called the edges. You want an graph algorithm called "topological sort", and now you've got some stuff to read.
我确实编写了一个工具,用于查找和绘制 PyPi 上的 Python 包之间的依赖关系。这是 贪吃
我确实用来分析我正在使用的一些库的依赖关系。以下是一些图表:
我不确定这是否是您想要的。如果是,您可以在此处阅读源代码,它是一个开源项目。更多依赖关系图可以查看图库
讲一下我是如何实现的为了查找包依赖项,我使用 pip 作为后端。为了绘制图表,我使用 Networkx。
I did wrote a tool for finding and drawing the dependencies between Python packages on PyPi. It's gluttony
And I did use to analysis the dependencies of some library I'm using. Here are some of diagrams:
I'm not sure is this what you want. If it is, you can read the source code here, it is an open source project. For more dependencies diagrams, you can view the gallery
Talking about how I implement it, for finding package dependencies, I use pip as backend. For drawing diagrams, I use Networkx.
这是我的工具,可以满足您的要求:
您可以使用文件和基本数据结构的 .ast 来创建任何您喜欢的树:
请注意,Python 中的基本只是一个字典。尽管如此,据我所知,没有树 DS 实际上实现了对自循环的检查。那东西叫做Floyd-Warshall
Here's the tool of mine that does what you asked for:
You can any sort of tree you like using .ast of file and basic data structures:
Notice the basic in Python is just a dict. Redardless of that no tree DS I know of Actually implements checks on self looping. That thing is called Floyd-Warshall
集合
。set
。结果就是你的答案。或者:
前者实现起来比较简单,但效率较低。后者虽然高效,但实施起来比较困难。
set
of all of the elements in it.set
from the former. The result is your answer.Alternatively:
The former is simpler to implement, but less efficient. The latter is efficient, but harder to implement.
我认为您正在寻找的步骤是区分已明确安装的软件包和依赖项。完成此操作后,您可以构建所有请求软件包的依赖关系树,并将这些软件包集与已安装软件包集进行比较。假设所有请求的包都已安装,对这些进行异或,应该会给出不再依赖的所有包的集合。
I think the step you're looking for is to differentiate between packages you has explicitly installed, and those that are dependencies. Once you have done this you can build dependency trees of all requested packages, and compare the set of these packages with the set of installed packages. XORing these, assuming all requested packages are installed, should give you the set of all packages which are no longer depended on.