当前位置: 首页 > article >正文

python删除一个函数 ast

python删除一个函数 ast

import ast
import sys


class FunctionRemover(ast.NodeTransformer):
    def __init__(self, func_name):
        self.func_name = func_name

    def visit_FunctionDef(self, node):
        # 如果是我们要删除的函数,添加占位标志并返回None来从树中移除它
        if node.name == self.func_name:
            # print("node",node)
            # print("node.name ",node.name )
            # 创建一个占位标志的注释节点
            placeholder = ast.Expr(value=ast.Constant(value="# Function removed: " + node.name))
            # return [placeholder, None]
            # return [placeholder, placeholder]
            return placeholder
        return node

    def visit_Call(self, node):
        # 检查函数调用是否是我们想要删除的函数
        if isinstance(node.func, ast.Name) and node.func.id == self.func_name:
            # 创建一个占位标志的注释节点
            placeholder = ast.Expr(value=ast.Constant(value="# Function call removed: " + self.func_name))
            return placeholder
        return node



# def remove_function_from_code(code, func_name):
#     tree = ast.parse(code)
#     # AttributeError: 'Assign' object has no attribute 'value'
#     remover = FunctionRemover(func_name)
#     new_tree = remover.visit(tree)
#     new_code = ast.unparse(new_tree)
#     return new_code

def remove_function_from_code(code, func_name):
    tree = ast.parse(code)
    remover = FunctionRemover(func_name)
    new_tree = remover.visit(tree)
    ast.fix_missing_locations(new_tree)  # 修复AST树的位置信息
    new_code = ast.unparse(new_tree)
    return new_code



import ast

class FunctionNameFinder(ast.NodeVisitor):
    def __init__(self):
        self.function_names = []

    def visit_FunctionDef(self, node):
        # 将函数名添加到列表中
        self.function_names.append(node.name)
        # 继续遍历AST
        self.generic_visit(node)

# def list_function_names(file_path):
#     with open(file_path, "r") as file:
#         code = file.read()
#     tree = ast.parse(code)
#     visitor = FunctionNameFinder()
#     visitor.visit(tree)
#     return visitor.function_names

def list_function_names(code):
    # with open(file_path, "r") as file:
    #     code = file.read()
    tree = ast.parse(code)
    visitor = FunctionNameFinder()
    visitor.visit(tree)
    return visitor.function_names


from top.starp.util import file_util

import random


# 使用示例
# file_path = "example.py"  # 将这里的路径替换为你的Python文件路径
# function_names = list_function_names(file_path)
# print("函数名列表:", function_names)
# file_path=rf"D:\proj\python\data_enhancement\parquet_language_percent.py"



def code_random_rm_func(code):
    function_names = list_function_names(code)
    # 使用random.choice随机选取一个元素
    func_name = random.choice(function_names)

    new_code = remove_function_from_code(code, func_name)
    # print(new_code)

    # print(new_code)
    # 补充这里的代码  # Function removed:

    return new_code



def code_path_random_rm_func(file_path=rf"D:\proj\python\data_enhancement\gen_sentence.py"):
    # file_path=rf"D:\proj\python\data_enhancement\parquet_code_desp.py"

    code=file_util.read_file(file_path)
    return code_random_rm_func(code)

http://www.kler.cn/news/283944.html

相关文章:

  • 如何将FMEA整合到组织的质量管理体系中?
  • 百度:未来or现在 顾此失彼?
  • 如何利用智能文档处理技术,搭建供应链金融智能审单系统?
  • 《深入浅出WPF》读书笔记.9Command系统
  • 人工智能的可解释性(XAI) | 使用LIME
  • NeRF笔记
  • 发布app到ios
  • 【网络】NAT、代理服务、内网穿透
  • Substance 3D Stager for Mac/Win:高效三维场景设计利器
  • AI大模型编写多线程并发框架(六十三):监听器优化·下
  • Goolge earth studio 进阶3——路径创建与编辑
  • 15行为型设计模式——责任链模式
  • NCNN入门之编译与安装
  • Redis的内存淘汰策略- volatile-lru
  • Linux命令行更换yum源repo为阿里源-centos7
  • 【分布式注册中心】NACOS_2.3.0部署与实战
  • DPR:一种用于开放与问答任务的检索方法
  • 暴搜、深搜、回溯算法题集
  • AI如何改变科学与数学领域:陶哲轩演讲解析
  • SpringBoot+redis+aop处理黑白名单
  • PHP伪协议总结
  • QT实战项目之音乐播放器
  • ES6基础----iterator接口的使用
  • 【Android】UI拓展之滑动菜单、悬浮按钮、CoordinatorLayout布局等
  • 分布式计算架构详解以及任务调度方式
  • python用波形显示udp数据实现一个模拟示波器
  • Leetcode3240. 最少翻转次数使二进制矩阵回文 II
  • ES常用多条件查询
  • 西方法律思想史考试复习材料
  • 每天一个数据分析题(五百一十一)- 神经网络