gpt4 book ai didi

python-3.x - Pycharm:如何从字典内部重构代码

转载 作者:行者123 更新时间:2023-12-03 23:56:39 25 4
gpt4 key购买 nike

我有一本字典,它在文件 dicts.py 中:

#!/usr/bin/env python3

errors = {
'errorMessageNotSent' : 'Your message was not sent',
'errorConnectionLost' : 'The connection to the server was lost\nTry again later.'
'errorMessageMalformed' : 'Your message seems to have unwilling characters, Check for them and try again'
}

我在我的代码中以这种方式在文件 action.py 中使用它:
#!/usr/bin/env python3
from dicts import errors

def doAction():
if error_happened_and_message_was_not_sent:
print(errors['errorMessageNotSent'])
else:
print(errors['errorMessageMalformed'])

过了一会儿我想做重构和重命名 errorMessageMalformederror_MalformedMessage都在 errors字典在 dicts.py文件及其在 print 中的用法另一个文件中的语句 ( action.py )。

如何使用 pycharm 实现这一目标?

我试过 Shift + F6 但它告诉我 Caret should be positioned at a symbol to be refactored .

这甚至可能吗?

最佳答案

您可以创建一个具有相同名称的虚拟变量。
现在我创建 errorMessageMalformed通过添加 errorMessageMalformed='' .

enter image description here

然后重命名新变量 errorMessageMalformed ,记得选择Search in comments and strings .

将显示一个弹出窗口。

enter image description here

点击Do Refactor :

enter image description here

关于python-3.x - Pycharm:如何从字典内部重构代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com