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
评论列表
文章目录