def insert_before_parent_list_fixed(node_list, s):
"""
insert a string before a certain ast node if the node's parent has a field which is a list that contains the node
"""
for i in range(len(node_list)):
parent = node_list[i].parent
for field, value in ast.iter_fields(parent):
if isinstance(value, list):
try:
index = value.index(node_list[i])
value[index:index] = ast.parse(s).body
except:
continue
return
评论列表
文章目录