Python Language
추상 구문 트리
수색…
파이썬 스크립트로 함수 분석하기
이것은 파이썬 스크립트를 분석하고 정의 된 각 함수에 대해 함수가 시작된 행 번호, 서명이 끝나는 위치, 문서 문자열이 끝나는 위치 및 함수 정의가 끝나는 위치를보고합니다.
#!/usr/local/bin/python3
import ast
import sys
""" The data we collect. Each key is a function name; each value is a dict
with keys: firstline, sigend, docend, and lastline and values of line numbers
where that happens. """
functions = {}
def process(functions):
""" Handle the function data stored in functions. """
for funcname,data in functions.items():
print("function:",funcname)
print("\tstarts at line:",data['firstline'])
print("\tsignature ends at line:",data['sigend'])
if ( data['sigend'] < data['docend'] ):
print("\tdocstring ends at line:",data['docend'])
else:
print("\tno docstring")
print("\tfunction ends at line:",data['lastline'])
print()
class FuncLister(ast.NodeVisitor):
def visit_FunctionDef(self, node):
""" Recursively visit all functions, determining where each function
starts, where its signature ends, where the docstring ends, and where
the function ends. """
functions[node.name] = {'firstline':node.lineno}
sigend = max(node.lineno,lastline(node.args))
functions[node.name]['sigend'] = sigend
docstring = ast.get_docstring(node)
docstringlength = len(docstring.split('\n')) if docstring else -1
functions[node.name]['docend'] = sigend+docstringlength
functions[node.name]['lastline'] = lastline(node)
self.generic_visit(node)
def lastline(node):
""" Recursively find the last line of a node """
return max( [ node.lineno if hasattr(node,'lineno') else -1 , ]
+[lastline(child) for child in ast.iter_child_nodes(node)] )
def readin(pythonfilename):
""" Read the file name and store the function data into functions. """
with open(pythonfilename) as f:
code = f.read()
FuncLister().visit(ast.parse(code))
def analyze(file,process):
""" Read the file and process the function data. """
readin(file)
process(functions)
if __name__ == '__main__':
if len(sys.argv)>1:
for file in sys.argv[1:]:
analyze(file,process)
else:
analyze(sys.argv[0],process)
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow