如何从声明中返回?

时间:2016-04-26 07:04:36

标签: python contextmanager

我有一个函数可以尝试一些params列表连接到ftp并连接到它可能的第一个服务器。

def connect(params):
    for user, passw, host in params:
        try:
            import pdb;pdb.set_trace()
            with FTPHost(host, user, passw) as h:
                return h
        except FTPError as e:
            logger.debug("Can't connect to ftp error is {}".format(e))
    else:
        raise Exception(
            "Can't connect to ftp server with none of the {}".format(params)
        )

此外,在代码I中尝试类似的事情

host = connect(*args)
host.walk()

但是在返回后立即关闭连接。这可能是它应该如何工作的,我想,虽然我希望它不会。 但现在我真的不知道如何正确地封装来自应用程序逻辑的连接试验。

我的意思是我当然可以将其改为延续传递方式(这是正确的名称,对吗?)

def connect(params, some_application_function):
    for user, passw, host in params:
        try:
            import pdb;pdb.set_trace()
            with FTPHost(host, user, passw) as host:
                some_application_function(host)
        except FTPError as e:
            logger.debug("Can't connect to ftp error is {}".format(e))
    else:
        raise Exception(
            "Can't connect to ftp server with none of the {}".format(params)
        )

但这似乎不太可读。还有其他选择吗?

2 个答案:

答案 0 :(得分:1)

也许将with语句移到connect函数之外?

def connect(params):
    for user, passw, host in params:
        try:
            import pdb;pdb.set_trace()
            return FTPHost(host, user, passw)
        except FTPError as e:
            logger.debug("Can't connect to ftp error is {}".format(e))
    else:
        raise Exception(
            "Can't connect to ftp server with none of the {}".format(params)
        )

def main():
    with connect(params) as h:
        do_something(h)

答案 1 :(得分:0)

我可能会将你的[7x4]函数转换为一个上下文管理器本身,它在内部调用你的FTPHost上下文管理器的魔术方法:

connect

现在您可以在主函数中运行它:

class FTPConnector:
    def __init__(self, params):
        self.params = params
    def __enter__(self):
        for user, passw, host in params:
            try:
                # import pdb;pdb.set_trace()  # not sure what you want to do with that...
                self.h_context = FTPHost(host, user, passw)
                return self.h_context.__enter__()
            except FTPError as e:
                logger.debug("Can't connect to ftp error is {}".format(e))
        else:
            raise Exception(
                "Can't connect to ftp server with none of the {}".format(params)
            )
    def __exit__(self, exc_type, exc_value, traceback):
        return self.h_context.__exit(exc_type, exc_value, traceback)