我尝试安装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)
答案 0 :(得分:2)
尽管其他答案中的零散解决方案都行得通,但是该方法存在缺陷,原因仅在于它容易犯小错误,错过需要修复的地方等。更好的解决方案是仅使用the 2to3
converter 。您可以使用以下方法一次解决所有文件:
$ 2to3 -f has_key -w auto-selfcontrol.py
仅运行the has_key
fixer,它会从dict.has_key(key)
转换为key in dict
。当然,您可以自己进行修复,但是在这种情况下,简单的程序化修复程序就可以正常工作。您可能只想在不使用-f has_key
的情况下运行它,以便在Py2和Py3之间应用任何其他重大更改的情况下,一次应用所有修复程序。
2to3
几乎可以自动处理所有内容,但Py2 str
类型更改除外(这是由代码逻辑决定的,是否应将给定的str
文字设为bytes
或str
(在Py3中)和整数除法(其中,/
可能需要更改为//
,具体取决于计算是逻辑浮点除法还是底数除法)。但是对于has_key
,它是非常可靠的。
答案 1 :(得分:0)
替换如下
1。
def check_if_running(username):
""" checks if self-control is already running. """
defaults = get_selfcontrol_settings(username)
return "BlockStartedDate" in defaults and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])
2-4。
def check_config(config):
""" checks whether the config file is correct """
if not "username" in config:
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 "selfcontrol-path" in config:
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 "block-schedules" in config:
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)
答案 2 :(得分:0)
此解决方案的正式资源位于:https://portingguide.readthedocs.io/en/latest/dicts.html
TL; DR是这样的:
SELECT
u.agent_number,
u.reporting_week
FROM table1 e
JOIN (
SELECT
b.agent_number,
b.reporting_week
FROM table2 b
GROUP BY b.agent_number
) u ON u.agent_number = e.agent_number
where DATE_FORMAT(date(u.reporting_week),'%Y-%m-%d %r') BETWEEN DATE_FORMAT(date('2019-01-01 00:00:00'),'%Y-%m-%d %r') AND DATE_FORMAT(date('2019-01-07 00:00:00'),'%Y-%m-%d %r');
现在是:
import React,{Component} from 'react';
import {Text, View, StyleSheet} from 'react-native';
rotateByStyle = (percent, base_degrees, clockwise) => {
let rotateBy = base_degrees;
if(clockwise) {
rotateBy = base_degrees + (percent * 3.6);
} else {
//anti clockwise progress
rotateBy = base_degrees - (percent * 3.6);
}
return {
transform:[{rotateZ: `${rotateBy}deg`}]
};
}
renderThirdLayer = (percent, commonStyles, ringColorStyle, ringBgColorStyle, clockwise, bgRingWidth, progressRingWidth, innerRingStyle, startDegrees) => {
let rotation = 45 + startDegrees;
let offsetLayerRotation = -135 + startDegrees;
if(!clockwise) {
rotation += 180;
offsetLayerRotation += 180;
}
if(percent > 50) {
return <View style= {[styles.secondProgressLayer,this.rotateByStyle((percent - 50), rotation, clockwise),
commonStyles, ringColorStyle, {borderWidth: progressRingWidth} ]}></View>
} else {
return <View
style={[styles.offsetLayer, innerRingStyle, ringBgColorStyle, {transform:[{rotateZ: `${offsetLayerRotation}deg`}], borderWidth: bgRingWidth}]}>
</View>
}
}
const CircularProgress = ({percent, radius, bgRingWidth, progressRingWidth, ringColor, ringBgColor, textFontSize, textFontWeight, clockwise, bgColor, startDegrees}) => {
const commonStyles = {
width: radius * 2,
height: radius * 2,
borderRadius: radius
};
}
let firstProgressLayerStyle;
let displayThickOffsetLayer = false;
if(percent > 50){
firstProgressLayerStyle = this.rotateByStyle(50, rotation, clockwise);
} else {
firstProgressLayerStyle = this.rotateByStyle(percent, rotation, clockwise);
if( progressRingWidth > bgRingWidth ) {
displayThickOffsetLayer = true;
}
}
let offsetLayerRotation = -135 + startDegrees;
if(!clockwise) {
offsetLayerRotation += 180;
}
export default CircularProgress;
因此,在您的代码中:
some_dict.has_key('some key')
成为:
'some key' in some_dict
类似地,像这样的行:
return defaults.has_key("BlockStartedDate") and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])
变得像:
return "BlockStartedDate" in defaults and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])
请注意,您也可以编写if not config.has_key("selfcontrol-path"):
# do something
,但是上面给出的示例被认为是更Python风格的,应该是首选。
答案 3 :(得分:0)
在python3.7中测试。
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>