python 的 contextmanager 的用法

上下文管理器

上下文管理器是实现了上下文管理协议的对象,其特有的语法是“with …as”。主要用于保存和恢复各种全局状态,关闭文件等,并为try…except…finally提供了一个方便使用的封装。

上下文管理协议具体来说就是在类里面实现以下两个方法:

_enter_(): 从该方法进入运行时上下文,并返回当前对象或者与运行时上下文相关的其他对象。如果with语句有as关键词存在,返回值会绑定在as后的变量上。

_exit_(exc_type, exc_val, exc_tb): 退出运行时上下文,return True 如果 with 执行体有异常,则不会继续向上抛出异常;return Flase 如果 with 执行体有异常,则继续向上抛出异常。如果在执行with语句体时发生异常,那退出时参数会包括异常类型、异常值、异常追踪信息,否则,3个参数都是None。

class MyResource1:
    def __enter__(self):
        print('connect to resource')
        return self    # return可以是对象,然后会绑定到as后面的变量
 
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('close resource connection')
        # __exit__方法 
        # return True 的话,如果执行 query 有异常,则异常就不往上抛了
		# return False 的话,如果执行 query 有异常,则异常就继续往上抛了
 
    def query(self):
        print('query data')
 
 
with MyResource1() as r:
    r.query()
 
运行结果:
connect to resource
query data
close resource connection

contextlib 模块

对于上下文的管理,python也提供了内建的模块contextlib来实现相同的机制,而且这种通过生成器和装饰器实现的上下文管理器,看起来比with语句和手动实现上下文管理协议更优雅。

# 不再需要手动实现enter和exit方法
class MyResource2:
 
    def query(self):
        print('query data')
 
# contextmanager 简化上下文管理器复杂的定义
from contextlib import contextmanager
 
 
@contextmanager
def make_myresource():
    print('connect to resource')
    yield MyResource2()     # yield 相当于return 加 中断回执。yield后面的语句会最终回来继续执行
    print('close resource connection')
 
 
with make_myresource() as r:
    r.query()

一个实际应用例子:

from contextlib import contextmanager
# 一个应用场景,给一个书名前后加上书名号

   
@contextmanager
def book_mark():
    print('《', end='')
    yield   # yield 后面不一定要返回结果,纯粹起一个中断作用
    print('》', end='')
 
with book_mark():
    print("且将生活一饮而尽", end='')
 
# 衍生一种用法:
# 在要执行的代码的前面和后面各补充一段代码,特别是对于一些框架的源码等,我们不能直接在源码里面修改
# 其次是追求封装性和复用性
#
# 执行过程是:
# 从 contextlib 模块中引入 contextmanager,然后装饰我们所定义的 book_mark 函数
# 这就允许我们使用 Python 的 with语句来调用 book_mark 函数。
# 在函数中,我们打开文件,然后通过 yield,将其传递出去,最终主调函数可以使用它。
# 一旦with语句结束,控制就会返回给 book_mark 函数,它继续执行 yield 语句后面的代码。
# from: https://www.cnblogs.com/zhbzz2007/p/6158125.html

由此,@contextmanager可以衍生另一种用法,不仅仅是简化上下文的定义,而是在我们需要执行的代码的前面和后面补充代码执行。

扩展应用

背景是: 想创建一个临时文件名给 with … as … 使用,使用完成后立刻删除临时文件:

  1. 得到一个临时文件名
  2. 在 with … as … 的执行体中,使用文件:写入一些内容, 然后解析内容把
    想要的内容赋值给变量,在退出执行体时把临时文件删除

C:\Python27\Lib\contextlib.py 对 contextmanager 的用法介绍的特别好

def contextmanager(func):
    """@contextmanager decorator.

    Typical usage:

        @contextmanager
        def some_generator(<arguments>):
            <setup>
            try:
                yield <value>
            finally:
                <cleanup>

    This makes this:

        with some_generator(<arguments>) as <variable>:
            <body>

    equivalent to this:

        <setup>
        try:
            <variable> = <value>
            <body>
        finally:
            <cleanup>

    """
    @wraps(func)
    def helper(*args, **kwds):
        return GeneratorContextManager(func(*args, **kwds))
    return helper

def create_unique_file(filename):
    version = 0
    result_file = filename
    # if we have to try more than 1000 times, something is seriously wrong
    while os.path.exists(result_file) and version < 1000:
        result_file = filename + '.' + str(version)
        version += 1
    if version >= 1000:
        raise OSError('cannot create unique file %s.[0-1000]' % filename)
    return result_file


# 从 contextlib 模块中引入 contextmanager,然后装饰我们所定义的 temp_open 函数
# 这就允许我们使用 Python 的 with语句来调用 temp_open 函数。
# 在函数中,我们打开文件,然后通过 yield,将其传递出去,最终主调函数可以使用它。
# 一旦with语句结束,控制就会返回给 temp_open 函数,它继续执行 yield 语句后面的代码。
# 这个最终会执行finally语句--删除文件。
# 如果我们在打开文件时遇到了 OSError 错误,它就会被捕获,最终 finally 语句依然会关闭文件句柄。
# from: https://www.cnblogs.com/zhbzz2007/p/6158125.html
@contextmanager
def temp_open(filename, mode):
    f = open(filename, mode)
    try:
        yield f
    except OSError:
        print("We had an error!")
    finally:
        f.close()
        os.remove(filename)

def main():
    tmp_file = create_unique_file('%s.%s' % (self.conn_setting['host'], self.conn_setting['port']))
    with temp_open(tmp_file, "w") as f_tmp, self.connection as cursor:
        a. write_some
        b. value = parse the content  # 当退出 with 时,临时文件会被删除的
    return True     
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值