首页 > 其他分享 >使用redbaron删除删除一个.py文件的所有注释,输出到一个新.py文件,文件名加上_nocmts后缀,用户可以自己决定是否保留空行

使用redbaron删除删除一个.py文件的所有注释,输出到一个新.py文件,文件名加上_nocmts后缀,用户可以自己决定是否保留空行

时间:2023-12-20 11:35:34浏览次数:28  
标签:comment 文件 删除 parent py next file endl

BUG:

  • 目前输出的文件中可能会出现缩进错误,待修改。
from redbaron import RedBaron

def remove_comments_with_redbaron(source_py_file, keep_blank_lines):
    with open(source_py_file, 'r', encoding='utf-8') as file:
        red = RedBaron(file.read())

    comments = red.find_all('comment')
    for comment in comments:
        # Before removing the comment, save the indentation of the parent node
        parent_indentation = comment.parent.absolute_bounding_box.top_left.column
        comment.parent.remove(comment)
        # After removing the comment, check if the next sibling is not indented
        next_sibling = comment.next
        if next_sibling and next_sibling.absolute_bounding_box.top_left.column < parent_indentation:
            # Indent the next_sibling to match the parent's indentation
            next_sibling.indent(parent_indentation // 4)  # Assuming 4 spaces per indentation level

    if not keep_blank_lines:
        while True:
            endl_nodes = red.find_all('endl')
            if not endl_nodes:
                break
            for endl in endl_nodes:
                if not endl.next_renderable:
                    endl.parent.remove(endl)

    return red.dumps()

def main():
    source_py_file = input("Enter the path to the .py file to clean up: ")
    keep_blank_lines = input("Do you want to keep blank lines? (y/n): ").lower() == 'y'
    new_content = remove_comments_with_redbaron(source_py_file, keep_blank_lines)
    new_file_name = source_py_file.replace('.py', '_nocmts.py')
    with open(new_file_name, 'w', encoding='utf-8') as new_file:
        new_file.write(new_content)
    print(f"Cleaned file has been saved as: {new_file_name}")

if __name__ == '__main__':
    main()

标签:comment,文件,删除,parent,py,next,file,endl
From: https://www.cnblogs.com/yhm138/p/17916146.html

相关文章

  • Could not build wheels for pillow, which is required to install pyproject.toml-b
     参考来源,致敬大佬。ERROR:CouldnotbuildwheelsforPillow,whichisrequiredtoinstallpyproject.toml-basedprojects-CSDN博客报错:Couldnotbuildwheelsforpillow,whichisrequiredtoinstallpyproject.toml-basedprojects的解决-CSDN博客 本人小白......
  • filebeat配置采集多个文件(多索引)推送ES
     Filebeat根据不同的日志设置不同的索引 配置如下:filebeat.inputs:-type:logpaths:-/tmp/log/ecologyencoding:GB2312fields:type:ecology-type:logpaths:-/tmp/log/stderr.logencoding:GB2312fields:type:strerr-ty......
  • MongoDB中如何优雅地删除大量数据
    删除大量数据,无论是在哪种数据库中,都是一个普遍性的需求。除了正常的业务需求,我们也需要通过这种方式来为数据库“瘦身”。为什么要“瘦身”呢?表的数据量到达一定量级后,数据量越大,表的查询性能相对也会越差。毕竟数据量越大,B+树的层级会越高,需要的IO也会越多。表的数据有......
  • 如何解决excel导入大文件报错
    开发过程中我们经常会遇到Excel导入大文件报错的问题,经过不断的摸索,我发现我们可以利用缓存区来实现大文件的上传,下面是我本人封装的一个实现工具,有兴趣的小伙伴可以看看,希望能对大家有所帮助!publicclassExcelImportBigDataUtil{/***每次放入缓冲区最大行数......
  • 手把手教你用python做一个年会抽奖系统
    引言马上就要举行年会抽奖了,我们都不知道是否有人能够中奖。我觉得无聊的时候可以尝试自己写一个抽奖系统,主要是为了娱乐。现在人工智能这么方便,写一个简单的代码不是一件困难的事情。今天我想和大家一起构建一个简易的抽奖系统,这样也能够巩固一下我自己对Python语法和框架的理解......
  • python xattr库
    因:ceph有一条设置文件/目录配额的命令ceph.quota.max_bytes,想在Python代码中调用它,最直接的方法是使用popen/subprocess等库直接执行这条命令,但如果频繁调用担心会影响系统性能,查阅资料发现xattr库也可以实现且更加方便。开始吧首先需要安装xattr库pip3installxattr......
  • .Net +Ajax大文件断点续传
    什么是断点续传大文件断点续传指的是在上传或下载大文件时,当传输中断或出现错误时,可以通过记录已经传输的数据和位置,下次从中断的位置继续传输,避免重新开始传输整个文件的过程,从而提高传输效率和稳定性。实现思路获取文件大小和已经传输的大小:在开始上传或下载文件之前,需要获取文......
  • 12.19---python
    seek()方法语法如下:file.seek(offset[,whece])offset--开始的偏移量,也就是代表需要移动偏移的字节数,如果是负数表示从倒数第几位开始。whence:可选,默认值为0。给offset定义一个参数,表示要从那个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾......
  • 【Python微信机器人】第六篇:优化使用方式,可pip安装
    优化内容这篇不聊技术点,说一下优化后的Python机器人代码怎么使用,优化内容如下:将hook库独立成一个库,发布到pypi,可使用pip安装将微信相关的代码发布成另一个库,也可以pip安装git仓库统一,以后都在这个仓库更新,不再一篇文章一个仓库开始建群,根据群里反馈增加功能和修复bug使用......
  • linux中部署python项目
    参考这篇博客:https://blog.csdn.net/smilehappiness/article/details/1173379431.首先查看python的版本:python-V2.安装python:3.安装虚拟环境报错1: 解决办法:export CURL_CA_BUNDLE="/etc/pki/tls/certs/ca-bundle.crt"https://3ms.huawei.com/km/blogs/details/14442367htt......