Python龙卷风停止执行完成()

时间:2017-02-18 06:25:52

标签: python runtime-error tornado

我对龙卷风比较新,我试图在条件通过时停止执行方法,否则它应该继续下一行代码(类似于返回)

这是我被困的代码部分,

Form hi = new Form("Table", new BorderLayout());
TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3"}, new Object[][] {
    {"Row 1", "Row A", "Row X"},
    {"Row 2", "Row B can now stretch", null},
    {"Row 3", "Row C", "Row Z"},
    {"Row 4", "Row D", "Row K"},
    }) {
        public boolean isCellEditable(int row, int col) {
            return col != 0;
        }
    };
Table table = new Table(model) {
    private int selectedRow = -1;
    @Override
    protected Component createCell(Object value, int row, int column, boolean editable) { 
        Component cell;
        if(row < 0) {
            cell = super.createCell(value, row, column, editable);
        } else {
            cell = new Button(value.toString());
            cell.setUIID("TableCell");
            ((Button)cell).addActionListener(e -> {
                selectedRow = row;
                setModel(getModel());
            });
        }
        if(selectedRow > -1 && selectedRow == row) { 
            cell.getAllStyles().setBgColor(0xff0000);
            cell.getAllStyles().setBgTransparency(100);
        } 
        return cell;
    }

    @Override
    protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
        TableLayout.Constraint con =  super.createCellConstraint(value, row, column);
        if(row == 1 && column == 1) {
            con.setHorizontalSpan(2);
        }
        con.setWidthPercentage(33);
        return con;
    }
};
hi.add(BorderLayout.CENTER, table);
hi.show();

BaseRequestHandler是:

class PostMatchHandler(BaseRequestHandler):
    result1 = "some sql query".execute()
    if not result1:
        response.update({'info': 'Levels Completed', 'status': settings.STATUS_200})
        self.write(response)
        self.finish()

    else:
        result1 = result1[0]
        do_something()
    self.write(response)

所以,如果result1为空,那么它应该完成API的执行并返回。但我收到此错误class BaseRequestHandler(tornado.web.RequestHandler): """ Base Class for all the future classes Activities like logging should be done in this class. Allow cors request in this class """ def set_default_headers(self): print "setting headers!!!" def write_error(self, status_code, **kwargs): response = {} handle_error_messages.. self.write(response) @staticmethod def extract_psycopg2_integrity_error(error): return error.message.split("Key")[1].replace("(", "").replace(")", "").split(".")[0].replace("=", " ") def prepare(self): logging.debug( "[info] Class {} via {} with body {}".format(self.__class__.__name__, self.request.uri, self.request.body))

如何停止执行。

2 个答案:

答案 0 :(得分:5)

你不需要&#34; self.finish&#34;它完成了Tornado写的响应,但它并没有停止执行你的功能。要停止执行函数,请添加return语句:

def get(self)
    result1 = "some sql query".execute()
    if not result1:
        response.update({'info': 'Levels Completed', 'status': settings.STATUS_200})
        self.write(response)
        return  # <-- here
    else:
        result1 = result1[0]
        do_something()
    self.write(response)

另一个选项是缩进最后一行,使其保持在&#34; else&#34;子句:

def get(self):
    result1 = "some sql query".execute()
    if not result1:
        response.update({'info': 'Levels Completed', 'status': settings.STATUS_200})
        self.write(response)    
    else:
        result1 = result1[0]
        do_something()
        self.write(response)  # indented 4 more spaces

答案 1 :(得分:1)

另一种方法,当您想强制从代码中的任何位置结束请求时非常有用,即使return(例如来自辅助函数)很难,也要提高{ {3}}例外。