如何在python3中替换has_key?

我尝试安装Auto-SelfControl并在执行此命令时卡住:


sudo /usr/bin/python auto-selfcontrol.py

它显示错误:


AttributeError: 'dict' 对象没有属性 'has_key'


我正在寻找解决方案并最终将has_key替换为in运算符,但由于我只了解 Python 的基础知识,因此代码对我来说非常复杂。


有3个地方使用了has_key,你能帮我改一下,这样我就可以用python3运行了吗?


1.


def check_if_running(username):

""" checks if self-control is already running. """

defaults = get_selfcontrol_settings(username)

return defaults.has_key("BlockStartedDate") and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])

2-4.


def check_config(config):

""" checks whether the config file is correct """

if not config.has_key("username"):

    exit_with_error("No username specified in config.")

if config["username"] not in get_osx_usernames():

    exit_with_error(

            "Username '{username}' unknown.\nPlease use your OSX username instead.\n" \

            "If you have trouble finding it, just enter the command 'whoami'\n" \

            "in your terminal.".format(

                    username=config["username"]))

if not config.has_key("selfcontrol-path"):

    exit_with_error("The setting 'selfcontrol-path' is required and must point to the location of SelfControl.")

if not os.path.exists(config["selfcontrol-path"]):

    exit_with_error(

            "The setting 'selfcontrol-path' does not point to the correct location of SelfControl. " \

            "Please make sure to use an absolute path and include the '.app' extension, " \

            "e.g. /Applications/SelfControl.app")

if not config.has_key("block-schedules"):

    exit_with_error("The setting 'block-schedules' is required.")

if len(config["block-schedules"]) == 0:

    exit_with_error("You need at least one schedule in 'block-schedules'.")

if config.get("host-blacklist", None) is None:

    print("WARNING:")

    msg = "It is not recommended to directly use SelfControl's blacklist. Please use the 'host-blacklist' " \

          "setting instead."

    print(msg)

    syslog.syslog(syslog.LOG_WARNING, msg)


森栏
浏览 183回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python