Skip to content Skip to sidebar Skip to footer

How To Replace Has_key In Python3?

I try to install Auto-SelfControl and got stuck when executing this command: sudo /usr/bin/python auto-selfcontrol.py It shows the error: AttributeError: 'dict' object has no att

Solution 1:

While the piecemeal solutions in the other answers will work, that approach is flawed simply because it's too easy to make a small mistake, miss a place that needs fixing, etc. The better solution is to just use the 2to3 converter. You could fix all of your files in one fell swoop with:

$ 2to3 -f has_key -w auto-selfcontrol.py

That only runs the has_key fixer which converts from dict.has_key(key) to key in dict. Sure you could make the fix yourself, but this is a case where simple programmatic fixers work just fine. You might just want to run it without -f has_key so it applies all the fixers at once, in case there are any other breaking changes that apply between Py2 and Py3.

2to3 handles just about everything automatically, with the exception of Py2 str type changes (where it's a code logic decision on whether a given str literal should be bytes or str in Py3) and integer division (where / might need to change to //, based on whether the computation is logically floating point division or floor division). But for has_key, it's pretty reliable.

Solution 2:

A formal source for the solution is here: https://portingguide.readthedocs.io/en/latest/dicts.html

The TL;DR is this:

some_dict.has_key('some key')

Is now:

'some key'in some_dict

So, in your code:

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

Becomes:

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

Similarly, lines like:

ifnotconfig.has_key("selfcontrol-path"):
    # do something

Become like:

if"selfcontrol-path"notinconfig:
    # do something

Note that you could also write if not "selfcontrol-path" in config: but the example given above is considered more Pythonic and should be preferred.

Solution 3:

Tested in python3.7.

   tmp = {'a':1, 'b':2, 'c':3}
   print(tmp.__contains__('a')) #True

Solution 4:

Replace as follows

1.

defcheck_if_running(username):
""" checks if self-control is already running. """
defaults = get_selfcontrol_settings(username)
return"BlockStartedDate"in defaults andnot NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])

2-4.

defcheck_config(config):
""" checks whether the config file is correct """ifnot"username"in config:
    exit_with_error("No username specified in config.")
if config["username"] notin 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"]))
ifnot"selfcontrol-path"in config:
    exit_with_error("The setting 'selfcontrol-path' is required and must point to the location of SelfControl.")
ifnot 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")
ifnot"block-schedules"in config:
    exit_with_error("The setting 'block-schedules' is required.")
iflen(config["block-schedules"]) == 0:
    exit_with_error("You need at least one schedule in 'block-schedules'.")
if config.get("host-blacklist", None) isNone:
    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)

Post a Comment for "How To Replace Has_key In Python3?"