[英]Comment Python function calls in a string with source code
我正在嘗試注釋來自 Python 源代碼的函數調用。
我閱讀了一個字符串中的源代碼,然后我嘗試將一些正則表達式應用於注釋方法調用。
這是一個簡單的工作示例:
s = open(path, 'r').read()
# comment method calls
result = re.sub('(^[.\w]+\(.*\))', r'#\1', s, flags=re.MULTILINE)
如您所見,我在源代碼的第一個縮進級別中注釋函數( __name__ == '__main__'
之外的代碼、方法和類)
但是,如何使這個正則表達式適用於多行調用?
例如,如果我將以下代碼轉換為字符串:
Builder.load_string('''
type: 'example'
callback: my_callback()
''')
我如何評論這個電話的每一行?
這將為您提供您需要評論的行號:
mod = "test1"
mod = importlib.import_module(mod)
p = ast.parse(inspect.getsource(mod))
for n in p.body:
if isinstance(n, ast.Expr):
for node in ast.walk(n):
if isinstance(node, ast.Call):
print(node.lineno)
對於像這樣的文件:
import math
class Foo:
def __init__(self):
self.foo = 4
def bar(self):
print("hello world")
def foo(self):
return self.bar()
def bar():
return 123
f = Foo()
f.bar()
bar()
它會輸出16
和18
這兩個調用。
只需忽略這些行並編寫新源或對更新內容做任何您想做的事情:
import inspect
import importlib
import ast
def get_call_lines(mod):
mod = importlib.import_module(mod)
p = ast.parse(inspect.getsource(mod))
for n in p.body:
if isinstance(n, ast.Expr):
for node in ast.walk(n):
if isinstance(node, ast.Call):
yield(node.lineno)
from StringIO import StringIO
new = StringIO()
with open("test1.py") as f:
st = set(get_call_lines("test1"))
for ind, line in enumerate(f, 1):
if ind not in st:
new.write(line)
new.seek(0)
print(new.read())
新將包含:
import math
class Foo:
def __init__(self):
self.foo = 4
def bar(self):
print("hello world")
def foo(self):
return self.bar()
def bar():
return 123
f = Foo()
您可以在運行時使用ast.NodeTransformer
更改代碼,但刪除節點並不是一項微不足道的任務,您想要的最簡單的方法是簡單地忽略正文中的 Call 行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.