Pycharm:如何从字典内部重构代码

时间:2017-07-29 11:31:43

标签: python-3.x pycharm

我有一个字典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'])

过了一段时间,我想进行重构并将errorMessageMalformed重命名为error_MalformedMessage errors字典中的dicts.py字典及print中的action.py字典另一个文件中的语句(Caret should be positioned at a symbol to be refactored)。

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

我试过 Shift + F6 ,但它告诉我if [ -z "$1" ]; then read -p "supply x:" x else x="$1" fi

这甚至可能吗?

1 个答案:

答案 0 :(得分:2)

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

enter image description here

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

将显示一个弹出窗口。

enter image description here

点击Do Refactor

enter image description here