registry.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:deb-python-pint 作者: openstack 项目源码 文件源码
def with_context(self, name, **kw):
        """Decorator to wrap a function call in a Pint context.

        Use it to ensure that a certain context is active when
        calling a function::

            >>> @ureg.with_context('sp')
            ... def my_cool_fun(wavelenght):
            ...     print('This wavelength is equivalent to: %s', wavelength.to('terahertz'))


        :param names: name of the context.
        :param kwargs: keyword arguments for the contexts.
        :return: the wrapped function.
        """
        def decorator(func):
            assigned = tuple(attr for attr in functools.WRAPPER_ASSIGNMENTS if hasattr(func, attr))
            updated = tuple(attr for attr in functools.WRAPPER_UPDATES if hasattr(func, attr))

            @functools.wraps(func, assigned=assigned, updated=updated)
            def wrapper(*values, **kwargs):
                with self.context(name, **kw):
                    return func(*values, **kwargs)

            return wrapper

        return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号