关于ContextManager

2017-04-11  本文已影响350人  blueshadow

原来markdown还支持写导语啊

Generator

生成器是个什么鬼?

>>> def gen():
...     yield 1
>>> type(a)
<type 'generator'>

简单地说,函数里包含yield就成为了一个generator(这当然是编译器处理的,语法来的)。
这个函数被调用时返回一个generator对象。这个对象有着迭代器的接口: next()

>>> a.next()
1
>>> a.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

yield和return都会返回一个值,不同点在于迭代器返回一个值后函数并不会立即结束,而是挂起并保存本地变量的状态,这些信息在函数恢复执行时再度有效。
但,如果你的迭代器函数如果无法提供更多给next调用,就会抛出StopIteration异常。这里就迭代器a就是个例子。

>>> def fab(a,b,c):
...     while a < c:
...             yield a
...             a, b = b, a+b
...
>>> aList = list(fab(1,1,200))
>>> aList
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

With 语法

with后面的表达式必须返回一个实现了ContextManager协议的对象。
啥叫实现了这个协议的对象?只要定义了函数__enter____exit__就可以啦:
with语法做了啥事?

class Context(object):

    def __init__(self):
        print '__init__()'

    def __enter__(self):
        print '__enter__()'
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        print '__exit__()'
        
with Context():
    print 'Doing work in the context'

>>> Output:
__init__()
__enter__()
Doing work in the context
__exit__()

实践场景

一个很有用的场景便是一段逻辑需要一个临时目录,这段逻辑完事后得把这个临时目录删了。不管这段逻辑会不会出错,这样实现后都能保证这个创建的临时目录最后得到删除。

class TempDir(object):
    def __init__(self):
        self._path = mkdtemp()

    def __enter__(self):
        return self._path

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.remove()

    def remove(self):
        if not self._path or not os.path.exists(self._path):
            return
        shutil.rmtree(self._path)

with TempDir() as d:
    print d
    print os.path.isdir(d)
print os.path.isdir(d)

>>> Output:
/var/folders/f4/jjr6p3w51kb77j2_v_5n0mq80000gn/T/tmpL59Tz6
True
False

一定要写一个类么

有时候可能故意写个类来实现ContextManager显得很浪费精力,这时候我们还有另外一个选择:用@contextmanager装饰器来将一个迭代器generator转换成一个context manager

import contextlib

@contextlib.contextmanager
def make_context():
    print '  entering'
    try:
        yield {}
    except RuntimeError, err:
        print '  ERROR:', err
    finally:
        print '  exiting'

print 'Normal:'
with make_context() as value:
    print '  inside with statement:', value

print
print 'Handled error:'
with make_context() as value:
    raise RuntimeError('showing example of handling an error')

print
print 'Unhandled error:'
with make_context() as value:
    raise ValueError('this exception is not handled')

>>> Output
Normal:
  entering
  inside with statement: {}
  exiting

Handled error:
  entering
  ERROR: showing example of handling an error
  exiting

Unhandled error:
  entering
  exiting
Traceback (most recent call last):
  File "contextlib_contextmanager.py", line 34, in <module>
    raise ValueError('this exception is not handled')
ValueError: this exception is not handled

装饰器方式原理分析

看源代码:

class GeneratorContextManager(object):
    def __init__(self, gen):
        self.gen = gen

    def __enter__(self):
        try:
            return self.gen.next()
        except StopIteration:
            raise RuntimeError("generator didn't yield")

    def __exit__(self, type, value, traceback):
        if type is None:
            try:
                self.gen.next()
            except StopIteration:
                return
            else:
                raise RuntimeError("generator didn't stop")
        else:
            if value is None:
                value = type()
            try:
                self.gen.throw(type, value, traceback)
                raise RuntimeError("generator didn't stop after throw()")
            except StopIteration, exc:
                return exc is not value
            except:
                if sys.exc_info()[1] is not value:
                    raise

def wraps(wrapped, assigned = WRAPPER_ASSIGNMENTS, updated = WRAPPER_UPDATES):
    return partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)

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

最佳实践

ContextManager不应该去捕捉block执行时的异常,而应该只careblock执行时如果发生了异常能够保证某些动作一定得到执行。

@contextmanager
def pushd(new_dir):
    origin_dir = os.getcwd()
    os.chdir(new_dir)
    try:
        yield
    # except OSError, err:
    #     print 'ERROR: ', err
    # except RuntimeError, err:
    #     print 'ERROR: ', err
    finally:
        print 'changing back'
        os.chdir(origin_dir)


with pushd('/tmp'):
    print os.getcwd()
    raise RuntimeError
print os.getcwd()
上一篇下一篇

猜你喜欢

热点阅读