对于那些使用 apt-get 的人来说,您知道每次安装/卸载某些东西时,您都会收到通知,说明您需要/不再需要某些依赖项。
我正在尝试理解其背后的理论,并可能实现我自己的版本。我做了一些谷歌搜索,想出了大部分耦合的东西。据我了解,耦合是两个相互依赖的类/模块。这不正是我正在寻找的。我正在寻找的更像是依赖关系树生成,我可以在其中找到最不依赖的模块(我已经采用了递归方式来执行此操作),并且(这是我尚未完成的部分)找到什么删除节点后不再需要。
另外,学习图论会有帮助吗?有没有关于最好使用 Python 作为语言的教程?
This可能有些兴趣:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)