分析并輸出Python代碼依賴的庫的實現(xiàn)代碼
用法:
分析一個腳本的依賴: 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實例分析
這篇文章主要介紹了Python進(jìn)程的通信Queue、Pipe,結(jié)合實例形式分析了Python進(jìn)程通信Queue、Pipe基本概念、用法及操作注意事項,需要的朋友可以參考下2020-03-03python 實現(xiàn)檢驗33品種數(shù)據(jù)是否是正態(tài)分布
今天小編就為大家分享一篇python 實現(xiàn)檢驗33品種數(shù)據(jù)是否是正態(tài)分布,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python應(yīng)用自動化部署工具Fabric原理及使用解析
這篇文章主要介紹了Python應(yīng)用自動化部署工具Fabric原理及使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11python矩陣轉(zhuǎn)換為一維數(shù)組的實例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python2.x和3.x下maketrans與translate函數(shù)使用上的不同
這篇文章主要介紹了Python2.x和3.x下maketrans與translate函數(shù)使用上的不同,這兩個函數(shù)建立映射來替換內(nèi)容是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識,需要的朋友可以參考下2015-04-04python pandas實現(xiàn)excel轉(zhuǎn)為html格式的方法
今天小編就為大家分享一篇python pandas實現(xiàn)excel轉(zhuǎn)為html格式的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python創(chuàng)建多線程的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Python中創(chuàng)建多線程的兩種常用方法,文中的示例代碼簡潔易懂,對我們掌握Python有一定的幫助,需要的可以收藏一下2023-05-05