且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PyCharm - 如何自动将项目中的所有标识符重命名为 snake_case?

更新时间:2023-01-22 21:22:35

你可以试试 camel-蛇pep8,来自官方描述:

You can try camel-snake-pep8, as from official description:

帮助将驼峰案例转换为蛇案例的重构工具在 Python 程序中相反,符合 PEP-8 风格指南.它使用/滥用 Python-Rope 来查找和执行更改.这程序以交互方式显示提议的更改并询问用户是否接受更改.

A refactoring tool to help convert camel case to snake case and vice versa in a Python program, in conformity with the PEP-8 style guide. It uses/abuses Python-Rope to find and perform the changes. The program interactively displays proposed changes and queries the user as to whether or not to accept the changes.

要安装运行这些命令:

$ <activate your virtual environment>
$ pip install rope colorama
$ wget https://raw.githubusercontent.com/abarker/camel-snake-pep8/master/src/camel_snake_pep8/camel_snake_pep8.py

然后转到项目的文件夹并运行以下命令:

After that go to the project's folder and run this command:

$ python2 camel_snake_pep8.py . *.py

该程序可用于重构 Python 2 或 Python 3 代码但它必须与 Python 2 一起运行.这是因为,截至 2017 年 3 月,Python-Rope 仅支持 Python 2(据说 Python 3 版本正在开发中).有时这不起作用,因为 python 3 的特定语法,例如:

The program can be used to refactor either Python 2 or Python 3 code but it must be run with Python 2. This is because, as of Mar. 2017, Python-Rope only supports Python 2 (a Python 3 version is said to be in progress). Sometimes this won't work, because of specific syntax for python 3, e.g.:

def pick(l: list, index: int) -> int:
    return l[index]

在这种情况下,您必须使此类表达式的语法与python2兼容.请投票/帮助绳索项目的issue支持python3.

In this case you have to make syntax of such expressions compatible with python2. Please, vote/help rope project's issue to support python3.