为了账号安全,请及时绑定邮箱和手机立即绑定

如何在python3中替换has_key?

如何在python3中替换has_key?

森栏 2021-10-19 09:47:03
我尝试安装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)
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信