1

Exception如果发生某些事情,我想重试我的方法,如下所示,也许使用装饰器更好?

class Processor(object):
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.user_info = UserInfo.objects.get(username=username)

    def get_inbox_page(self):
        try:
            # TODO process inbox page
            pass
        except NotLoginException:
            self.login()
            self.get_inbox_page()

    def get_downloaded_page(self):
            try:
            # TODO process downloaded page
            pass
        except NotLoginException:
            self.login()
            self.get_downloaded_page()

    def get_accept_msg_page(self):
        try:
            # TODO process accept message page
            pass
        except NotLoginException:
            self.login()
            self.get_accept_msg_page()

    # ... some other similar methods

    def login(self):
        session = self.login_proxy(self.username, self.password)
        self.user_info.session = session
        self.user_info.save()
4

0 回答 0