def __init__(self, fonc): """initialisation du décorateur""" self.fonc = fonc self.comptappel = 0 # compteur d'appels self.tempsexec = 0 # pour stocker les temps d'exécution # pour que les fonctions décorées gardent nom et docstring: functools.wraps(fonc)(self)
def wrapper(*args, **kwargs): if sys_platform.system() in platform_name: return functools.wraps(func)(case_true_wraps)(*args, **kwargs) if case_true_wraps \ else func(*args, **kwargs) else: return functools.wraps(func)(case_false_wraps)(*args, **kwargs) if case_false_wraps \ else case_false_result
def argify(f): keys = inspect.getargspec(f).args functools.wraps(f) def wrapper(*args, **kwargs): args = _build_args(keys, args) return f(*args, **kwargs) return wrapper
def opt_func(func): functools.wraps(func) def closure(): return func return closure
def decorator(func): functools.wraps(func) def wrapper(*args, **kw): print('begin call, %s' % text) func(*args, **kw) print('end call') return wrapper
def wraps(self, args): if args: self.func = args[0] functools.wraps(self.func)(self) if 'help' not in self.docopts: self.docopts['help'] = True if 'doc' not in self.docopts: doc = getattr(self.func, '__doc__', None) else: doc = self.docopts.pop('doc') if doc is None: doc = 'Usage: %prog' name = self.func.__name__.replace('_', '-') doc = doc.replace('%prog', name).strip() if '%options' in doc: def options(match): l = match.groups()[-1] if l is not None: fmt = '{0:<%s}{1}\n' % l.strip('-s') else: fmt = '{0:<20}{1}\n' opts = '' for opt in self.options: opts += fmt.format(*opt) return opts doc = re.sub('(%options)(-[0-9]+s)*', options, doc) doc = doc.replace('\n ', '\n') self.doc = doc if 'name' not in self.logopts: self.logopts['name'] = name return self
def show_temp_imgs(fun): u""" 一个缓存图片处理中间结果的装饰器, 缓存结果后放在这该目录下tests\\data\\temp.png :param fun: :return: """ wraps(fun) def inner(self, *args, **kwargs): result = fun(self, *args, **kwargs) if not SAVE_TEMP_IMG: return result imgs = self.temp_imgs columns = [] for temps in imgs: column_stack = np.column_stack(temps) columns.append(column_stack) img = np.row_stack(columns) text_parameter = (cv2.FONT_HERSHEY_TRIPLEX, 5, (255, 0, 255), 5, False) times = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) cv2.putText(img, times, (100, 2000), *text_parameter) cv2.imwrite("tests\\data\\temp.png", img[::8, ::8]) sleep(1) return result return inner
def redirectView(request): MemberFormSet = formset_factory(wraps(reloadForm)(partial(reloadForm))) formTree = ProductSelectionForm(request.POST or None) if request.method == 'POST': #as I use formsets this is mandatory formset = MemberFormSet(request.POST) #make sure every form is validMemberFormSet = formset_factory(wraps(PostFormEFactor)(partial(PostFormEFactor))) if formset.is_valid(): #start retrieving formset data, which is all filter data except FancyTree for f in formset: cd = f.cleaned_data print("selection:") selection = cd.get('selection') print(selection) # #start getting some form skeletons now! ArticleFormSet = formset_factory(PostFormEFactor) formset = ArticleFormSet() formset = formset_factory(wraps(PostFormEFactor)(partial(PostFormEFactor)), extra=1) #updata dictionary for filter options: form skeletons selectionForm = formset_factory(wraps(reloadForm)(partial(reloadForm)), extra=1) dict = ({'selectionForm': selectionForm}) #now send some other forms to the filter interface dict.update({'formset':formset}) dict.update({'formTree': formTree}) #send signal for popup popup = 1 dict.update({'popup': popup}) return render(request,"ExioVisuals/distribution.html", dict)
def __getattr__(self, attr): if self._device is None: raise openhtf.plugs.InvalidPlugError( 'DeviceWrappingPlug instances must set the _device attribute.') if attr == 'as_base_types': return super(DeviceWrappingPlug, self).__getattr__(attr) attribute = getattr(self._device, attr) if not self.verbose or not callable(attribute): return attribute # Attribute callable; return a wrapper that logs calls with args and kwargs. functools.wraps(attribute, assigned=('__name__', '__doc__')) def logging_wrapper(*args, **kwargs): """Wraps a callable with a logging statement.""" args_strings = tuple(short_repr(arg) for arg in args) kwargs_strings = tuple( ('%s=%s' % (key, short_repr(val)) for key, val in six.iteritems(kwargs)) ) log_line = '%s calling "%s" on device.' % (type(self).__name__, attr) if args_strings or kwargs_strings: log_line += ' Args: \n %s' % (', '.join(args_strings + kwargs_strings)) self.logger.debug(log_line) return attribute(*args, **kwargs) return logging_wrapper
def wsgiapp(wrapped): """ Decorator to turn a WSGI application into a :app:`Pyramid` :term:`view callable`. This decorator differs from the :func:`pyramid.wsgi.wsgiapp2` decorator inasmuch as fixups of ``PATH_INFO`` and ``SCRIPT_NAME`` within the WSGI environment *are not* performed before the application is invoked. E.g., the following in a ``views.py`` module:: @wsgiapp def hello_world(environ, start_response): body = 'Hello world' start_response('200 OK', [ ('Content-Type', 'text/plain'), ('Content-Length', len(body)) ] ) return [body] Allows the following call to :meth:`pyramid.config.Configurator.add_view`:: from views import hello_world config.add_view(hello_world, name='hello_world.txt') The ``wsgiapp`` decorator will convert the result of the WSGI application to a :term:`Response` and return it to :app:`Pyramid` as if the WSGI app were a :app:`Pyramid` view. """ def decorator(context, request): return request.get_response(wrapped) # Support case where wrapped is a callable object instance if getattr(wrapped, '__name__', None): return wraps(wrapped)(decorator) return wraps(wrapped, ('__module__', '__doc__'))(decorator)
def set_up(func): wraps(func) def inner(self, route, params={}): route = route [1:] if route[0] == '/' else route full_route = '{}/{}'.format(self.base_url, route) return func(self, full_route, params) return inner
def wrapper(func): functools.wraps(func) def begin(): print('begin call %s'%time.clock()) func() print('end call %s'%time.clock()) return begin
def __init__( self, fonc ): self.fonc = fonc self.parent = None functools.wraps( fonc )( self )
def get_wrapper(func): """Return a wrapper around a Babel function to hook in our timezone and locale system. :param func: A function from babel.dates """ #: avoid failing doctest from the original docstring if func.func_name in _timezone_aware: wrapper = wraps(func, ("__module__", "__name__")) else: wrapper = wraps(func, ("__module__", "__name__", "__doc__")) @wrapper def _inner(*args, **kwargs): if "locale" not in kwargs or kwargs["locale"] is dates.LC_TIME: kwargs["locale"] = get_locale() # yet only a few methods can work with timezone information properly # we also check if the applied value is a basestring. If it is # we convert it to a proper pytz timezone value. If it's not we # assume to get a proper timezone value. tzinfo = kwargs.get("tzinfo", None) if isinstance(tzinfo, basestring) or tzinfo is None: tzinfo = get_timezone(tzinfo) if func.func_name in _timezone_aware: kwargs["tzinfo"] = tzinfo return func(*args, **kwargs) return _inner
def matcher(callable_or_names, names=None): """ Implicitly registers it with function's __name__, "be_a_dog": @matcher def be_a_dog(x): return type(x) == dog Explicitly registers it with names "be_a_dog" and "be_canine" @matcher("be_a_dog, be_canine") def whatever(x): return type(x) == dog matcher(f, "be_a_dog be_canine") """ if callable(callable_or_names): # called directly as a decorator if 'lambda' in callable_or_names.__name__ and names: callable_or_names.__name__ = words(names)[0] matchers.add(Matcher(callable_or_names), names) else: # called as a decorator factory decorator = functools.partial(matcher, names=callable_or_names) functools.wraps(matcher)(decorator) return decorator
def connect(attr): funcs = [] for base in reversed(model.__mro__): func = getattr(base, attr, None) if func and func not in funcs: funcs.append(func) if funcs: def save_signal(sender, **kwargs): self = kwargs['instance'] save = False for func in funcs: if func(self, created=kwargs['created'], save=save): save = True if save: self.save() def signal(sender, **kwargs): self = kwargs['instance'] for func in funcs: func(self) if attr == 'post_save': wrapper = wraps(funcs[0])(save_signal) else: wrapper = wraps(funcs[0])(signal) getattr(signals, attr).connect(wrapper, sender=model) setattr(model, '_' + attr, wrapper)
def __init__(self, func): wraps(func)(self) self.called = 0 self.runtimes = [] self.average_runtime = 0 self.last_run = 0 self.wrapped = func
def __call__(self, func): cache = self._cache expire = self._expire ft.wraps(func) def wrapper(*args, **kwargs): "Wrapper function to cache function result." key = (args, kwargs) try: result, expire_time, delta = cache.get( key, default=ENOVAL, expire_time=True, tag=True ) if result is ENOVAL: raise KeyError now = time.time() ttl = expire_time - now if (-delta * math.log(random.random())) < ttl: return result except KeyError: pass now = time.time() result = func(*args, **kwargs) delta = time.time() - now cache.set(key, result, expire=expire, tag=delta) return result return wrapper
def __init__(self, fn=nop, current_self=None, *a, **k): super(CallCounter, self).__init__(*a, **k) wraps(fn)(self) self.fn = fn self.count = 0 self.last_args = None self.current_self = current_self
def message(func): """ Returns: A telegram.Message instance representing the message posted. """ functools.wraps(func) def wrap(self, *args, **kwargs): url, data = func(self, *args, **kwargs) if kwargs.get('reply_to_message_id'): reply_to_message_id = kwargs.get('reply_to_message_id') data['reply_to_message_id'] = reply_to_message_id if kwargs.get('reply_markup'): reply_markup = kwargs.get('reply_markup') if isinstance(reply_markup, ReplyMarkup): data['reply_markup'] = reply_markup.to_json() else: data['reply_markup'] = reply_markup json_data = self._requestUrl(url, 'POST', data=data) data = self._parseAndCheckTelegram(json_data) if data is True: return data return Message.de_json(data) return wrap
def plot(func): try: import click except ImportError: click = None if click: doc_strings = [f.__doc__ for f in _plot_helper._functions] decorators = [click.command()] chain = itertools.chain(*(s.split("\n") for s in doc_strings)) lines1, lines2 = itertools.tee(chain) next(lines2, None) for line1, line2 in itertools.izip(lines1, lines2): if ':' in line1: opt, t = [s.strip() for s in line1.split(":")] decorators.append(click.option('--' + opt, type=pydoc.locate(t), help=line2.strip())) decorators.append(wraps(func)) else: decorators = [wraps(func)] @_decorate_all(decorators) def plotted_func(**kwargs): fig, ax = plt.subplots() name = func(fig, ax, **kwargs) for helper in _plot_helper._functions: helper(ax, **kwargs) fig.savefig(name) return plotted_func
def timeconsume(func = None, logger_func = sys.stdout.write): origin_func = sys.stdout.write def new_func(ifunc, *args, **kwds): start = datetime.datetime.now() value = ifunc(*args, **kwds) end = datetime.datetime.now() if logger_func == origin_func: print("time consume(%s): %s" % (ifunc.__name__, (end - start))) return value logger_func("time consume(%s): %s" % (ifunc.__name__, (end - start))) return value def decorator_func(ifunc): inner_func = functools.partial(new_func, ifunc) functools.wraps(ifunc)(inner_func) inner_func.__code__ = ifunc.__code__ #_funcwrapper(ifunc, inner_func) return inner_func if func is None: return decorator_func inner_func = functools.partial(new_func, func) functools.wraps(func)(inner_func) inner_func.__code__ = func.__code__ #_funcwrapper(func, inner_func) return inner_func return new_func
def cached(func = None, cached_dict = None, cache_length = DEFAULT_CACHE_LENGTH): if cached_dict is None: cached_dict = {} key_list = [] def new_func(ifunc, *arg, **kwds): key = str(arg) + str(kwds) if key in cached_dict: return cached_dict[key] result = ifunc(*arg, **kwds) if len(cached_dict) >= cache_length: front_key = key_list[0] key_list.remove(front_key) del cached_dict[front_key] cached_dict[key] = result key_list.append(key) return result def decorator_func(ifunc): inner_func = functools.partial(new_func, ifunc) functools.wraps(ifunc)(inner_func) inner_func.__code__ = ifunc.__code__ #_funcwrapper(ifunc, inner_func) return inner_func if func is None: return decorator_func inner_func = functools.partial(new_func, func) functools.wraps(func)(inner_func) inner_func.__code__ = func.__code__ #_funcwrapper(func, inner_func) return inner_func
def register_new(_func): print '########## register ##########' wraps(_func) def call(cls, *args, **kw): print self, args, kw self.actions[_func.__name__] = _func return call
def decorate_view(view, args, method): """Decorate a given view with cornice niceties. This function returns a function with the same signature than the one you give as :param view: :param view: the view to decorate :param args: the args to use for the decoration :param method: the HTTP method """ def wrapper(request): # if the args contain a klass argument then use it to resolve the view # location (if the view argument isn't a callable) ob = None view_ = view if 'klass' in args: ob = args['klass'](request) if is_string(view): view_ = getattr(ob, view.lower()) # do schema validation if 'schema' in args: validate_colander_schema(args['schema'], request) # the validators can either be a list of callables or contain some # non-callable values. In which case we want to resolve them using the # object if any validators = args.get('validators', ()) for validator in validators: if is_string(validator) and ob is not None: validator = getattr(ob, validator) validator(request) # only call the view if we don't have validation errors if len(request.errors) == 0: # if we have an object, the request had already been passed to it if ob: response = view_() else: response = view_(request) # check for errors and return them if any if len(request.errors) > 0: # We already checked for CORS, but since the response is created # again, we want to do that again before returning the response. request.info['cors_checked'] = False return args['error_handler'](request.errors) # We can't apply filters at this level, since "response" may not have # been rendered into a proper Response object yet. Instead, give the # request a reference to its api_kwargs so that a tween can apply them. # We also pass the object we created (if any) so we can use it to find # the filters that are in fact methods. request.cornice_args = (args, ob) return response # return the wrapper, not the function, keep the same signature functools.wraps(wrapper) return wrapper
def memoize(f): f.__memoize_cache__ = {} functools.wraps(f) def wrapper(*args): if args not in f.__memoize_cache__: f.__memoize_cache__[args] = f(*args) return f.__memoize_cache__[args] return wrapper
def trace(function): functools.wraps(function) def wrapper(*args, **kwargs): try: return function(*args, **kwargs) except: LOG_EXCEPTION('tea.trace - %s' % function.__name__) return wrapper
def argcatcher(func): functools.wraps(func) def decorator(*args, **kwargs): with settings(warn_only=True): result = env.safe_run("%s -c 'import %s'" % (_python_cmd(env), library)) if result.failed: return func(*args, **kwargs) return decorator
def memo(func): cache = {} wraps(func) def wrap(*args): if args not in cache: cache[args] =func(*args) # compute and cache the solution return cache[args] return wrap
def __init__( self, fonc ): self.fonc = fonc self.name = None self.order = None self.stack = None functools.wraps( fonc )( self )
def identity_func(x): """The identify (a.k.a. transparent) function that returns it's input as is.""" return x def mk_window_func(window_func, *args, **kwargs): window_wf = window_func(*args, **kwargs) def wf_preproc(wf): return window_wf * wf return wf_preproc mk_window_func.hanning = wraps(hanning)(partial(mk_window_func, hanning)) mk_window_func.kaiser = wraps(kaiser)(partial(mk_window_func, kaiser)) def mk_wf_to_spectr(preproc: callable = None, fft_func: callable = rfft, postproc: callable = abs): """Make a function that computes the spectrogram of a waveform By spectrum, we mean the output of the pipeline: `tile -> preproc -> fft -> postproc -> spectrum Because typically, we preprocess the input waveform (say, transform with a hanning function), and post process the fft (say take the norm of the complex vector). >>> import numpy as np
def create_script_module_impl(nn_module, concrete_type, cpp_module, stubs): """ Convert an nn.Module to a RecursiveScriptModule. Arguments: nn_module: The original Python nn.Module that we are creating a ScriptModule for. concrete_type: The fully initialized ConcreteType of the module. cpp_module: A newly-constructed C++ script::Module to copy stuff into. stubs: ScriptMethodStubs to compile as part of the conversion process. """ assert concrete_type.jit_type and concrete_type.jit_type == cpp_module._type( ) def init_fn(script_module): # Initialize the ScriptModule: # 1. Copy the attributes/parameters/buffers from the original `nn_module` to the new ScriptModule. for name, (attr_type, is_param) in concrete_type.get_attributes().items(): orig_value = getattr(nn_module, name) if is_param: cpp_module._register_parameter(name, orig_value, False) elif isinstance(orig_value, torch.jit.Attribute): cpp_module._register_attribute(name, attr_type, orig_value.value) else: cpp_module._register_attribute(name, attr_type, orig_value) # 2. Copy the submodules from the original `nn_module` to the new ScriptModule, # recursively scripting them. for name in concrete_type.get_module_names(): orig_value = getattr(nn_module, name) assert isinstance(orig_value, Module) scripted = recursive_script(orig_value) cpp_module._register_module(name, scripted._c) script_module._modules[name] = scripted # 3. Copy @ignored/@unused methods from the original `nn_module` to the new ScriptModule. # This ensures we can access these Python methods on the ScriptModule. for name in dir(nn_module): item = getattr(nn_module, name, None) if not inspect.ismethod(item): continue if _jit_internal.is_ignored_fn(item): setattr(script_module, name, item) # For convenience, attach the concrete type to the new ScriptModule script_module._concrete_type = concrete_type # Actually create the ScriptModule, initializing it with the function we just defined script_module = torch.jit.RecursiveScriptModule._construct( cpp_module, init_fn) # Compile methods if necessary if concrete_type not in concrete_type_store.methods_compiled: create_methods_from_stubs(concrete_type, stubs) torch._C._run_emit_module_hook(cpp_module) concrete_type_store.methods_compiled.add(concrete_type) # Make the compiled methods available to the Python ScriptModule class. for stub in stubs: if stub.original_method is None: # define()'d methods don't have an Python original_method, so we # don't need to do any Python re-wrapping stuff continue name = stub.original_method.__name__ if name != stub.def_.name().name: # TODO: Why skip this? Because @torch.jit._overload_method will # mangle the name of the function. continue script_method = cpp_module._get_method(name) # Wrap the original to propagate docstrings and such. # TODO: we don't currently do this functions that are recursively # compiled, we should. script_method = functools.wraps(stub.original_method)(script_method) # Add the methods to the script_module directly. This ensures they will # be found first when `name` is looked up (as opposed to the stubs or # nn.Module.forward) script_module.__dict__[name] = script_method return script_module
def wrapper(f): f = functools.wraps(fn)(f) f.__wrapped__ = getattr(fn, '__wrapped__', fn) return f
def finalize(wrapper, new_doc): wrapper = functools.wraps(func)(wrapper) wrapper.__doc__ = new_doc return wrapper
import django # NOQA except: self.ctx.die(681, "ERROR: Django not installed!") if django.VERSION < (1, 6) or django.VERSION >= (1, 9): self.ctx.err("ERROR: Django version %s is not " "supported!" % django.get_version()) try: import omeroweb.settings as settings kwargs['settings'] = settings except Exception, e: self.ctx.die(682, e) return func(self, *args, **kwargs) return wrapper return wraps(func)(import_django_settings(func)) def assert_config_argtype(func): """Decorator validating OMERO.web deployment dependencies""" def config_argtype(func): def wrapper(self, *args, **kwargs): argtype = args[0].type settings = kwargs['settings'] mismatch = False if args[0].system: self.ctx.die( 683, "ERROR: --system is no longer supported, " "see --help") if settings.APPLICATION_SERVER in ("development", ): mismatch = True
def __get__(self, obj, objtype): """ Called for instance methods """ return_func = functools.partial(self._cache_wrapper, obj) return_func.cache_clear = functools.partial(self.cache_clear, obj) # Return the wrapped function and wraps it to maintain the docstring and the name of the original function: return functools.wraps(self._input_func)(return_func)
def wrapper(f): f = functools.wraps(wrapped, assigned, updated)(f) f.__wrapped__ = wrapped return f
def typechecked(func=None, *, always=False): """ Perform runtime type checking on the arguments that are passed to the wrapped function. The return value is also checked against the return annotation if any. If the ``__debug__`` global variable is set to ``False``, no wrapping and therefore no type checking is done, unless ``always`` is ``True``. This can also be used as a class decorator. This will wrap all type annotated methods in the class with this decorator. :param func: the function or class to enable type checking for :param always: ``True`` to enable type checks even in optimized mode """ if not __debug__ and not always: # pragma: no cover return func if func is None: return partial(typechecked, always=always) if isclass(func): prefix = func.__qualname__ + '.' for key in dir(func): attr = getattr(func, key) if callable(attr) and attr.__qualname__.startswith(prefix): if getattr(attr, '__annotations__', None): setattr(func, key, typechecked(attr, always=always)) return func if not getattr(func, '__annotations__', None): warn('no type annotations present -- not typechecking {}'.format( function_name(func))) return func def wrapper(*args, **kwargs): memo = _CallMemo(func, args=args, kwargs=kwargs) check_argument_types(memo) retval = func(*args, **kwargs) check_return_type(retval, memo) if inspect.isgenerator(retval): return TypeCheckedGenerator(retval, memo) else: return retval async def async_wrapper(*args, **kwargs): memo = _CallMemo(func, args=args, kwargs=kwargs) check_argument_types(memo) retval = await func(*args, **kwargs) check_return_type(retval, memo) return retval def asyncgen_wrapper(*args, **kwargs): memo = _CallMemo(func, args=args, kwargs=kwargs) check_argument_types(memo) retval = func(*args, **kwargs) return TypeCheckedAsyncGenerator(retval, memo) if inspect.iscoroutinefunction(func): if func.__code__ is not async_wrapper.__code__: return wraps(func)(async_wrapper) elif isasyncgenfunction(func): if func.__code__ is not asyncgen_wrapper.__code__: return wraps(func)(asyncgen_wrapper) else: if func.__code__ is not wrapper.__code__: return wraps(func)(wrapper) # the target callable was already wrapped return func
def __init__(self, operation): self.operation = operation wraps(operation)(self)
def wrapper(f): f = functools.wraps(wrapped)(f) f.__wrapped__ = wrapped return f
def cache_decorator(function): return functools.wraps(function)(LRUCachedCallable( function, cache_size))
def mk_request_function(method_spec): """ Makes function that will make http requests for you, on your own terms. Specify what API you want to talk to, and how you want to talk to it (and it talk back to you), and get a function that does exactly that. Essentially, this factory function allows you to take an API specification, specify how you want it to relate to python objects (how to convert input arguments to API elements, and how to convert the response of the request, for instance), and get a method that is ready to be used. :param method_spec: Specification of how to convert arguments of the function that is being made to an http request. :return: A function. Note: I say "function", but the function is meant to be a method, so the function has a self as first argument. That argument is ignored. """ # defaults method_spec = method_spec.copy() method_spec['request_kwargs'] = method_spec.get('request_kwargs', {}) method_spec['request_kwargs']['method'] = method_spec[ 'request_kwargs'].get('method', 'GET') arg_order = _ensure_list(method_spec.get('args', [])) # TODO: inject a signature, and possibly a __doc__ in this function def request_func(self, *args, **kwargs): # print(args, arg_order) # absorb args in kwargs # if len(args) > len(arg_order): # raise ValueError( # f"The number ({len(args)}) of unnamed arguments was greater than " # f"the number ({len(arg_order)}) of specified arguments in arg_order") kwargs = dict( kwargs, **{argname: argval for argname, argval in zip(arg_order, args)}) # convert argument types TODO: Not efficient. Might could be revised. for arg_name, converter in method_spec.get('input_trans', {}).items(): if arg_name in kwargs: kwargs[arg_name] = converter(kwargs[arg_name]) json_data = {} for arg_name in method_spec.get('json_arg_names', []): if arg_name in kwargs: json_data[arg_name] = kwargs.pop(arg_name) # making the request_kwargs #################################################################################### request_kwargs = method_spec['request_kwargs'] if 'url_template' in method_spec: request_kwargs['url'] = method_spec['url_template'].format( **kwargs) elif 'url' in method_spec: request_kwargs['url'] = method_spec['url'] if json_data: request_kwargs['json'] = json_data if 'debug' in method_spec: debug = method_spec['debug'] if debug == 'print_request_kwargs': print(request_kwargs) elif debug == 'return_request_kwargs': return request_kwargs r = request(**request_kwargs) if 'output_trans' in method_spec: r = method_spec['output_trans'](r) return r if 'wraps' in method_spec: return wraps(method_spec['wraps'])(request_func) else: all_args = method_spec.get('args', []) + method_spec.get( 'json_arg_names', []) if all_args: set_signature_of_func(request_func, ['self'] + all_args) return request_func
def checkConnectionIsReplicatorConnection(func): def wrapper(self, conn, *args, **kw): if self.app.replicator.getCurrentConnection() is conn: return func(self, conn, *args, **kw) return wraps(func)(wrapper)
def _decorate_coroutine_callable(func, new_patching): if hasattr(func, 'patchings'): func.patchings.append(new_patching) return func # Python 3.5 returns True for is_generator_func(new_style_coroutine) if # there is an "await" statement in the function body, which is wrong. It is # fixed in 3.6, but I can't find which commit fixes this. # The only way to work correctly with 3.5 and 3.6 seems to use # inspect.iscoroutinefunction() is_generator_func = inspect.isgeneratorfunction(func) is_coroutine_func = asyncio.iscoroutinefunction(func) try: is_native_coroutine_func = inspect.iscoroutinefunction(func) except AttributeError: is_native_coroutine_func = False if not (is_generator_func or is_coroutine_func): return None patchings = [new_patching] def patched_factory(*args, **kwargs): extra_args = [] patchers_to_exit = [] patch_dict_with_limited_scope = [] exc_info = tuple() try: for patching in patchings: arg = patching.__enter__() if patching.scope == LIMITED: patchers_to_exit.append(patching) if isinstance(patching, _patch_dict): if patching.scope == GLOBAL: for limited_patching in patch_dict_with_limited_scope: if limited_patching.in_dict is patching.in_dict: limited_patching._keep_global_patch(patching) else: patch_dict_with_limited_scope.append(patching) else: if patching.attribute_name is not None: kwargs.update(arg) if patching.new is DEFAULT: patching.new = arg[patching.attribute_name] elif patching.new is DEFAULT: patching.mock_to_reuse = arg extra_args.append(arg) args += tuple(extra_args) gen = func(*args, **kwargs) return _PatchedGenerator(gen, patchings, asyncio.iscoroutinefunction(func)) except BaseException: if patching not in patchers_to_exit and _is_started(patching): # the patcher may have been started, but an exception # raised whilst entering one of its additional_patchers patchers_to_exit.append(patching) # Pass the exception to __exit__ exc_info = sys.exc_info() # re-raise the exception raise finally: for patching in reversed(patchers_to_exit): patching.__exit__(*exc_info) # wrap the factory in a native coroutine or a generator to respect # introspection. if is_native_coroutine_func: # inspect.iscoroutinefunction() returns True patched = _awaitable.make_native_coroutine(patched_factory) elif is_generator_func: # inspect.isgeneratorfunction() returns True def patched_generator(*args, **kwargs): return (yield from patched_factory(*args, **kwargs)) patched = patched_generator if is_coroutine_func: # asyncio.iscoroutinefunction() returns True patched = asyncio.coroutine(patched) else: patched = patched_factory patched.patchings = patchings return functools.wraps(func)(patched)
def lru_cache(maxsize=255, timeout=None): """lru_cache(maxsize = 255, timeout = None) --> returns a decorator which returns an instance (a descriptor). Purpose - This decorator factory will wrap a function / instance method and will supply a caching mechanism to the function. For every given input params it will store the result in a queue of maxsize size, and will return a cached ret_val if the same parameters are passed. Params - maxsize - int, the cache size limit, anything added above that will delete the first values enterred (FIFO). This size is per instance, thus 1000 instances with maxsize of 255, will contain at max 255K elements. - timeout - int / float / None, every n seconds the cache is deleted, regardless of usage. If None - cache will never be refreshed. Notes - If an instance method is wrapped, each instance will have it's own cache and it's own timeout. - The wrapped function will have a cache_clear variable inserted into it and may be called to clear it's specific cache. - The wrapped function will maintain the original function's docstring and name (wraps) - The type of the wrapped function will no longer be that of a function but either an instance of _LRU_Cache_class or a functool.partial type. On Error - No error handling is done, in case an exception is raised - it will permeate up. """ class _LRU_Cache_class(object): def __init__(self, input_func, max_size, timeout): self._input_func = input_func self._max_size = max_size self._timeout = timeout # This will store the cache for this function, format - {caller1 : [OrderedDict1, last_refresh_time1], caller2 : [OrderedDict2, last_refresh_time2]}. # In case of an instance method - the caller is the instance, in case called from a regular function - the caller is None. self._caches_dict = {} def cache_clear(self, caller=None): # Remove the cache for the caller, only if exists: if caller in self._caches_dict: del self._caches_dict[caller] self._caches_dict[caller] = [ collections.OrderedDict(), time.time() ] def __get__(self, obj, objtype): """ Called for instance methods """ return_func = functools.partial(self._cache_wrapper, obj) return_func.cache_clear = functools.partial(self.cache_clear, obj) # Return the wrapped function and wraps it to maintain the docstring and the name of the original function: return functools.wraps(self._input_func)(return_func) def __call__(self, *args, **kwargs): """ Called for regular functions """ return self._cache_wrapper(None, *args, **kwargs) # Set the cache_clear function in the __call__ operator: __call__.cache_clear = cache_clear def _cache_wrapper(self, caller, *args, **kwargs): # Create a unique key including the types (in order to differentiate between 1 and '1'): kwargs_key = "".join( map( lambda x: str(x) + str(type(kwargs[x])) + str(kwargs[x]), sorted(kwargs), )) key = "".join(map(lambda x: str(type(x)) + str(x), args)) + kwargs_key # Check if caller exists, if not create one: if caller not in self._caches_dict: self._caches_dict[caller] = [ collections.OrderedDict(), time.time() ] else: # Validate in case the refresh time has passed: if self._timeout != None: if time.time( ) - self._caches_dict[caller][1] > self._timeout: self.cache_clear(caller) # Check if the key exists, if so - return it: cur_caller_cache_dict = self._caches_dict[caller][0] if key in cur_caller_cache_dict: return cur_caller_cache_dict[key] # Validate we didn't exceed the max_size: if len(cur_caller_cache_dict) >= self._max_size: # Delete the first item in the dict: cur_caller_cache_dict.popitem(False) # Call the function and store the data in the cache (call it with the caller in case it's an instance function - Ternary condition): cur_caller_cache_dict[key] = (self._input_func( caller, *args, **kwargs) if caller != None else self._input_func(*args, **kwargs)) return cur_caller_cache_dict[key] # Return the decorator wrapping the class (also wraps the instance to maintain the docstring and the name of the original function): return lambda input_func: functools.wraps(input_func)(_LRU_Cache_class( input_func, maxsize, timeout))
def dec(func: Callable) -> Callable: for decorator in reversed(list(decorators) + [wraps(func)]): func = decorator(func) return func
def __init__(self, func): wraps(func, updated=())(self) self.func = func self.tls = local()
def decorator(func): def inner_decorator(request, *args, **kwargs): if check_group_admin(groups, request): return func(request, *args, **kwargs) return wraps(func)(inner_decorator)
def __init__(self, func, name=None): wraps(func)(self) self._func = func if name: self.__name__ = name
def wrapper(f): return wraps(f)(FunctionWithTag(f, nargs=nargs, nouts=nouts, ndefs=ndefs))
def __get__(self, component, _component_type): return wraps(self.function)(partial(self._call_function, *filter(None, [component])))
def wrapper(f): return wraps(f)(MultiDispatchFunction(f, nargs=nargs, nouts=nouts))
def wrapper(f): return wraps(f)(SingleDispatchFunction(f, nargs=nargs, nouts=nouts, ndefs=ndefs))
def wraps(self, func): return wraps(func)
def wrapper(checkfunc): return wraps(checkfunc)(FontBakeryCheck(checkfunc, *args, **kwds))
def __call__(self, f: Callable): # We use deferred wrapping because when decorator called # We did not have self._instance: we did not call init_app yet wrapper = functools.wraps(f)(DeferredWrapper(f)) self._deferred_wrappers.append(wrapper) return wrapper
from functools import wraps,update_wrapper def check(fn): @wraps(fn) def wrapper(*args, **kwargs): ret = fn(*args, **kwargs) return ret return wrapper #@check def add(x, y): return x+y add(4,5) = wrappper(4,5) = partial(update_wrapper,wrappper,元组,字典)=update_wrapper(fn) wraps(wrapper, assigned = WRAPPER_ASSIGNMENTS, updated = WRAPPER_UPDATES) partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated) add(4,5)
# -*- coding: utf-8 -*- import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dmoj.settings') import gevent.monkey gevent.monkey.patch_all() try: import MySQLdb except ImportError: import pymysql pymysql.install_as_MySQLdb() else: from functools import wraps, partial import gevent.hub def gevent_waiter(fd, hub=gevent.hub.get_hub()): hub.wait(hub.loop.io(fd, 1)) MySQLdb.connect = MySQLdb.Connection = MySQLdb.Connect = wraps( MySQLdb.connect)(partial(MySQLdb.connect, waiter=gevent_waiter)) from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
def wrapper(func): return wraps(func)(FontBakeryCondition(func, *args, **kwds))
def _inject_tracing_into_class(_cls): """Given a class that will be made into an actor, inject tracing into all of the methods.""" def span_wrapper(method: Callable[..., Any]) -> Any: def _resume_span( self: Any, *_args: Any, _ray_trace_ctx: Optional[Dict[str, Any]] = None, **_kwargs: Any, ) -> Any: """ Wrap the user's function with a function that will extract the trace context """ # If tracing feature flag is not on, perform a no-op if not _is_tracing_enabled() or _ray_trace_ctx is None: return method(self, *_args, **_kwargs) tracer: _opentelemetry.trace.Tracer = _opentelemetry.trace.get_tracer( __name__ ) # Retrieves the context from the _ray_trace_ctx dictionary we # injected. with _use_context( _DictPropagator.extract(_ray_trace_ctx) ), tracer.start_as_current_span( _actor_span_consumer_name(self.__class__.__name__, method), kind=_opentelemetry.trace.SpanKind.CONSUMER, attributes=_actor_hydrate_span_args(self.__class__.__name__, method), ): return method(self, *_args, **_kwargs) return _resume_span def async_span_wrapper(method: Callable[..., Any]) -> Any: async def _resume_span( self: Any, *_args: Any, _ray_trace_ctx: Optional[Dict[str, Any]] = None, **_kwargs: Any, ) -> Any: """ Wrap the user's function with a function that will extract the trace context """ # If tracing feature flag is not on, perform a no-op if not _is_tracing_enabled() or _ray_trace_ctx is None: return await method(self, *_args, **_kwargs) tracer = _opentelemetry.trace.get_tracer(__name__) # Retrieves the context from the _ray_trace_ctx dictionary we # injected, or starts a new context with _use_context( _DictPropagator.extract(_ray_trace_ctx) ), tracer.start_as_current_span( _actor_span_consumer_name(self.__class__.__name__, method.__name__), kind=_opentelemetry.trace.SpanKind.CONSUMER, attributes=_actor_hydrate_span_args( self.__class__.__name__, method.__name__ ), ): return await method(self, *_args, **_kwargs) return _resume_span methods = inspect.getmembers(_cls, is_function_or_method) for name, method in methods: # Skip tracing for staticmethod or classmethod, because these method # might not be called directly by remote calls. Additionally, they are # tricky to get wrapped and unwrapped. if is_static_method(_cls, name) or is_class_method(method): continue # Add _ray_trace_ctx to method signature setattr( method, "__signature__", _add_param_to_signature( method, inspect.Parameter( "_ray_trace_ctx", inspect.Parameter.KEYWORD_ONLY, default=None ), ), ) if inspect.iscoroutinefunction(method): # If the method was async, swap out sync wrapper into async wrapped_method = wraps(method)(async_span_wrapper(method)) else: wrapped_method = wraps(method)(span_wrapper(method)) setattr(_cls, name, wrapped_method) return _cls