我正在接近我的最終目標,即在模塊和其他導入的模塊之間生成一個很好的圖形。生成導入圖表
例如,如果從T中y和z,和y的進口X進口和v我想有:
x -> y, z
y -> t, v
現在,我已經有了我的進口吊鉤定義見下文,但在運行它簡單的文件我沒有得到我所期望的:
python study_imports.py CollectImports simple.py
('study_imports.py', 'study_imports')
其中simple.py實際上是從study_imports導入的。 問題是,我想看到「simple.py」,而不是「study_imports.py」,有沒有辦法讓文件的路徑實際導入其他模塊?
class CollectImports(object):
"""
Import hook, adds each import request to the loaded set and dumps
them to file
"""
def __init__(self, output_file):
self.loaded = set()
self.output_file = output_file
def __str__(self):
return str(self.loaded)
def cleanup(self):
"""Dump the loaded set to file
"""
dumped_str = '\n'.join(x for x in self.loaded)
open(self.output_file, 'w').write(dumped_str)
def find_module(self, module_name, package=None):
#TODO: try to find the name of the package which is actually
#importing something else, and how it's doing it
#use a defualtdict with empty sets as the storage for this job
entry = (__file__, module_name)
self.loaded.add(str(entry))
你知道的[Snakefood的'sfood-graph']( http://furius.ca/snakefood/doc/snakefood-doc.html#seconds-usage-instructions)? –
我現在非常感謝,這是非常好的,但我仍然想讓我的版本工作,只是爲了理解.. –