我得到了一个可能失败的函数列表,如果一个失败,我不希望脚本停止,而是继续下一个函数.
我用这样的东西执行它:
list_of_functions = [f_a,f_b,f_c]
for current_function in list_of_functions:
try:
current_function()
except Exception:
print(traceback.format_exc())
Run Code Online (Sandbox Code Playgroud)
它工作正常,但它不符合PEP8:
捕获异常时,请尽可能提及特定异常,而不是使用bare except子句.
例如,使用:
Run Code Online (Sandbox Code Playgroud)try: import platform_specific_module except ImportError: platform_specific_module = None一个裸的except:子句将捕获SystemExit和KeyboardInterrupt异常,使得用Control-C中断程序变得更加困难,并且可以掩盖其他问题.如果要捕获发出程序错误信号的所有异常,请使用除Exception之外的内容:(裸除了除了BaseException之外).
一个好的经验法则是将裸"除"子句的使用限制为两种情况:
如果异常处理程序将打印出来或记录回溯; 至少用户会意识到发生了错误.
如果代码需要做一些清理工作,但随后让异常向上传播并加注.尝试...终于可以更好地处理这种情况.
这个好方法怎么样?
我正在使用react / redux / react-redux来实现执行ajax请求的模式形式。
如果我是正确的,则react-redux使您能够:
我还使用redux-saga处理由redux触发的Ajax请求。
但是,我不知道从Redux触发特定事件时如何触发动作(例如:关闭模式形式)。
代码示例:
class MyFormDialog extends React.Component {
state = {
open: false,
};
handleOpen = () => {
this.setState({open: true});
};
handleClose = () => {
this.setState({open: false});
};
render() {
const actions = [
<FlatButton
label="Cancel"
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
onTouchTap={this.props.ajaxRequest}
/>,
];
return (
<div>
<MenuItem primaryText="Make a request" onTouchTap={this.handleOpen}/>
<Dialog
title="Make a request"
actions={actions}
modal={true}
open={this.state.open}
onRequestClose={this.handleClose} />
</div>
);
}
}
const mapStateToProps = (state, ownProps) => …Run Code Online (Sandbox Code Playgroud) 我有一个需要使用"或" |运算符的查询:
Mymodel.query.filter((Mymodel.a== 'b') | (Mymodel.b == 'c'))
Run Code Online (Sandbox Code Playgroud)
这很好.但是,我希望我的条件放在一个未知长度的数组中:
conds = [ Mymodel.a== 'b', Mymodel.b == 'c', Mymodel.c == 'd']
Mymodel.query.filter(???(conds))
Run Code Online (Sandbox Code Playgroud)
谢谢 !
我想让列表中的用户属于Facebook Graph API.根据文档,我试过这个:
GET /me/groups
Run Code Online (Sandbox Code Playgroud)
(https://developers.facebook.com/docs/graph-api/reference/user/groups/)
它需要user_managed_groups权限,但它只显示用户管理的组.我想要用户所属的所有群组.
可能吗 ?谢谢.
我想用facebook进行身份验证,使用Parse来存储用户的信息.我在我的应用程序类中得到以下代码:
public class App extends Application {
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Parse.initialize(this, "[XXX]", "[XXX]");
ParseFacebookUtils.initialize(this);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的登录活动中,以下内容:
public class LoginActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.login_auth_button).setOnClickListener(authClickListener);
}
@Override
protected void onStart() {
super.onStart();
currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
loadNext();
} else {
}
}
public void loadNext(){
[XXX]
}
View.OnClickListener authClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> permissions = Arrays.asList("public_profile","user_friends");
ParseFacebookUtils.logInWithReadPermissionsInBackground(LoginActivity.this, permissions, …Run Code Online (Sandbox Code Playgroud) facebook ×2
python ×2
android ×1
exception ×1
material-ui ×1
pep8 ×1
react-redux ×1
reactjs ×1
redux ×1
sqlalchemy ×1