如何在function.py中捕获字符串,并将def step1()
及其后续函数create()
和login()
跟踪为字典格式?(我想要实现的格式如下所示)
function.py
#!C:\Python\Python39\python.exe
# print ('Content-type: text/html\n\n')
def step1():
create()
login()
def step2():
authenticate()
def step3():
send()
预期产出
thisdict = {
'def step1()': ['create(),login()'],
'def step2():':['authenticate()'],
'def step3():': ['send()']
}
您可以读取文件
function.py
,将其拆分以分离不同的函数,然后对每个函数再次拆分,以获得签名作为键,命令作为值:这将返回:
你可以这样做:
这显然不是最好的代码,但很容易理解和实现:)
您可以使用一个正则表达式来查找每个方法和内容
(def \w+\(.*\):)((?:\n[ \t]+.+)+)
(def \w+\(.*\):)
用于方法定义\n[ \t]+.+
用于每个方法行(与前面的\n
)相关问题 更多 >
编程相关推荐