分析并輸出Python代碼依賴的庫(kù)的實(shí)現(xiàn)代碼
用法:
分析一個(gè)腳本的依賴: analysis_dependency.py script1.py
遞歸分析依賴: analysis_dependency.py script1.py -r
#!/usr/bin/env python # encoding: utf-8 # source: https://github.com/MrLYC/ycyc/blob/dev/tools/analysis_dependency.py import ast import importlib import inspect class Analysis(ast.NodeTransformer): def __init__(self, paths, recursion): self.modules = list() self.paths = list(paths) self.recursion = recursion def add_module(self, module): if module and module not in self.modules: self.modules.append(module) if self.recursion: try: path = inspect.getsourcefile(importlib.import_module(module)) if path: self.paths.append(path) except: pass def visit_Import(self, node): for i in node.names: self.add_module(i.name) def visit_ImportFrom(self, node): self.add_module(node.module) def analysis(self): for p in self.paths: try: with open(p,"rt") as fp: self.visit(ast.parse(fp.read(), p)) except: pass return tuple(self.modules) if __name__ =="__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument("paths", nargs="+") parser.add_argument("-r","--recursion", action="store_true", default=False) args = parser.parse_args() analysisor = Analysis(args.paths, args.recursion) for m in analysisor.analysis(): print m
相關(guān)文章
Python進(jìn)程的通信Queue、Pipe實(shí)例分析
這篇文章主要介紹了Python進(jìn)程的通信Queue、Pipe,結(jié)合實(shí)例形式分析了Python進(jìn)程通信Queue、Pipe基本概念、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03python 實(shí)現(xiàn)檢驗(yàn)33品種數(shù)據(jù)是否是正態(tài)分布
今天小編就為大家分享一篇python 實(shí)現(xiàn)檢驗(yàn)33品種數(shù)據(jù)是否是正態(tài)分布,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python應(yīng)用自動(dòng)化部署工具Fabric原理及使用解析
這篇文章主要介紹了Python應(yīng)用自動(dòng)化部署工具Fabric原理及使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python矩陣轉(zhuǎn)換為一維數(shù)組的實(shí)例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python2.x和3.x下maketrans與translate函數(shù)使用上的不同
這篇文章主要介紹了Python2.x和3.x下maketrans與translate函數(shù)使用上的不同,這兩個(gè)函數(shù)建立映射來(lái)替換內(nèi)容是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-04-04vscode調(diào)試django項(xiàng)目的方法
這篇文章主要介紹了vscode調(diào)試django項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08python pandas實(shí)現(xiàn)excel轉(zhuǎn)為html格式的方法
今天小編就為大家分享一篇python pandas實(shí)現(xiàn)excel轉(zhuǎn)為html格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python創(chuàng)建多線程的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Python中創(chuàng)建多線程的兩種常用方法,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們掌握Python有一定的幫助,需要的可以收藏一下2023-05-05