PyCharm - 如何自动将项目中的所有标识符重命名为 snake_case? [英] PyCharm - how to rename all identifiers in project to snake_case automatically?

查看:51
本文介绍了PyCharm - 如何自动将项目中的所有标识符重命名为 snake_case?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 Python 项目(仅 3000 多行),不幸的是,语法不符合现在需要的 PEP8.我正在寻找将所有函数从 camelCase 样式重命名为 snake_case 样式的方法.

I have a pretty large project in Python (little over 3000 lines) and unfortunately, the syntax doesn't respect PEP8, which is needed now. I'm looking especially for the way of renaming all the functions from camelCase style to snake_case style.

到目前为止,我只找到了 这个答案,它拒绝了在 PyCharm 中自动重命名所有变量的可能性,并建议手动进行,我不想这样做,因为代码中有数千个变量.

So far I've only found this answer, which rejects the possibility of renaming all variable automatically in PyCharm and suggests doing it manually, which I don't want to because there are thousands of variables in the code.

那么,有没有办法,如何根据上面的规则自动重命名所有变量?

So, is there a way, how to rename all the variables automatically according to the rule above?

我对只针对一个字符串执行的函数/正则表达式不感兴趣,我真的只对集体"解决方案感兴趣.

I'm not interested in functions/regexes, which do it for one string, I'm really only interested in the "collective" solution.

推荐答案

你可以试试 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.

这篇关于PyCharm - 如何自动将项目中的所有标识符重命名为 snake_case?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆