我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用functools.WRAPPER_UPDATES。
def test_update_wraper(): print(functools.WRAPPER_ASSIGNMENTS) print(functools.WRAPPER_UPDATES) hello() print(hello.__name__) print(hello.__doc__) print hello2() print(hello2.__name__) print(hello2.__doc__) # 3. wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES) # ??partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)???
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): def wrapper(f): f = functools.wraps(wrapped, assigned, updated)(f) f.__wrapped__ = wrapped return f return wrapper
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
def check(ureg, *args): """Decorator to for quantity type checking for function inputs. Use it to ensure that the decorated function input parameters match the expected type of pint quantity. Use None to skip argument checking. :param ureg: a UnitRegistry instance. :param args: iterable of input units. :return: the wrapped function. :raises: :class:`DimensionalityError` if the parameters don't match dimensions """ dimensions = [ureg.get_dimensionality(dim) if dim is not None else None for dim in args] 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): for dim, value in zip_longest(dimensions, values): if dim is None: continue val_dim = ureg.get_dimensionality(value) if val_dim != dim: raise DimensionalityError(value, 'a quantity of', val_dim, dim) return func(*values, **kwargs) return wrapper return decorator
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): def wrapper(f): f = functools.wraps(wrapped)(f) f.__wrapped__ = wrapped return f return wrapper