Python decorator.decorate() Examples
The following are 14
code examples of decorator.decorate().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
decorator
, or try the search function
.
Example #1
Source File: debug.py From omniduct with MIT License | 6 votes |
def logging_scope(name, *wargs, **wkwargs): """ A decorator to add the decorated function as a new logging scope, with name `name`. All additional arguments are passed to `StatusLogger._scope_enter`. Current supported keyword arguments are "timed", in which case when the scope closes, the duration of the call is shown. """ def logging_scope(func, *args, **kwargs): logger._scope_enter(name, *wargs, **wkwargs) success = True try: f = func(*args, **kwargs) return f except Exception as e: success = False raise_with_traceback(e) finally: logger._scope_exit(success) return lambda func: decorate(func, logging_scope)
Example #2
Source File: helpers.py From SMV with Apache License 2.0 | 6 votes |
def _helpCls(receiverCls, helperCls): iscallable = lambda f: hasattr(f, "__call__") for name, oldMethod in inspect.getmembers(helperCls, predicate=iscallable): # We will use decorator.decorate to ensure that attributes of oldMethod, like # docstring and signature, are inherited by newMethod. decorator.decorate # won't accept an unbound method, so for Python 2 we extract oldMethod's # implementing function __func__. In Python 3, inspect.getmembers will return # the implementing functions insead of unbound method - this is due to # Python 3's data model. try: impl = oldMethod.__func__ except: impl = oldMethod if not name.startswith("_"): newMethod = _getUnboundMethod(helperCls, impl) setattr(receiverCls, name, newMethod)
Example #3
Source File: helpers.py From bionic with Apache License 2.0 | 6 votes |
def count_calls(counter): """ A decorator which counts the number of times the decorated function is called using the counter argument. """ def call_counts_inner(func): def wrapper(func, *args, **kwargs): counter.mark() return func(*args, **kwargs) wrapped = decorate(func, wrapper) counter.reset() return wrapped return call_counts_inner
Example #4
Source File: base.py From incubator-tvm with Apache License 2.0 | 6 votes |
def decorate(func, fwrapped): """A wrapper call of decorator package, differs to call time Parameters ---------- func : function The original function fwrapped : function The wrapped function """ import decorator return decorator.decorate(func, fwrapped) #----------------------------------------- # Base code for structured error handling. #----------------------------------------- # Maps error type to its constructor
Example #5
Source File: base.py From dgl with Apache License 2.0 | 5 votes |
def decorate(func, fwrapped): """A wrapper call of decorator package, differs to call time Parameters ---------- func : function The original function fwrapped : function The wrapped function """ import decorator return decorator.decorate(func, fwrapped)
Example #6
Source File: base.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def decorate(func, fwrapped): """A wrapper call of decorator package, differs to call time Parameters ---------- func : function The original function fwrapped : function The wrapped function """ import decorator return decorator.decorate(func, fwrapped)
Example #7
Source File: helpers.py From SMV with Apache License 2.0 | 5 votes |
def _getUnboundMethod(helperCls, oldMethod): def newMethod(_oldMethod, self, *args, **kwargs): return _oldMethod(helperCls(self), *args, **kwargs) return decorator.decorate(oldMethod, newMethod)
Example #8
Source File: Mastodon.py From Mastodon.py with MIT License | 5 votes |
def api_version(created_ver, last_changed_ver, return_value_ver): """Version check decorator. Currently only checks Bigger Than.""" def api_min_version_decorator(function): def wrapper(function, self, *args, **kwargs): if not self.version_check_mode == "none": if self.version_check_mode == "created": version = created_ver else: version = bigger_version(last_changed_ver, return_value_ver) major, minor, patch = parse_version_string(version) if major > self.mastodon_major: raise MastodonVersionError("Version check failed (Need version " + version + ")") elif major == self.mastodon_major and minor > self.mastodon_minor: print(self.mastodon_minor) raise MastodonVersionError("Version check failed (Need version " + version + ")") elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: raise MastodonVersionError("Version check failed (Need version " + version + ", patch is " + str(self.mastodon_patch) + ")") return function(self, *args, **kwargs) function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" return decorate(function, wrapper) return api_min_version_decorator ### # Dict helper class. # Defined at top level so it can be pickled. ###
Example #9
Source File: WebApp.py From RENAT with Apache License 2.0 | 5 votes |
def session_check(f): return decorate(f, _session_check) # reconnect methods currenlty focus on Samurai only # need to enhance this
Example #10
Source File: WebApp.py From RENAT with Apache License 2.0 | 5 votes |
def with_reconnect(f): return decorate(f, _with_reconnect) ###
Example #11
Source File: VChannel.py From RENAT with Apache License 2.0 | 5 votes |
def with_reconnect(f): return decorate(f, _with_reconnect) ###
Example #12
Source File: trace.py From terminal-leetcode with MIT License | 5 votes |
def trace(f): return decorate(f, _trace)
Example #13
Source File: pickle_memoize.py From training_results_v0.6 with Apache License 2.0 | 4 votes |
def memoize(key): """Memoize the result of function and reuse multiple times. Parameters ---------- key: str The unique key to the file Returns ------- fmemoize : function The decorator function to perform memoization. """ def _register(f): """Registration function""" allow_types = (string_types, int, float) fkey = key + "." + f.__name__ + ".pkl" if fkey not in Cache.cache_by_key: Cache.cache_by_key[fkey] = Cache(fkey) cache = Cache.cache_by_key[fkey] cargs = tuple(x.cell_contents for x in f.__closure__) cargs = (len(cargs),) + cargs def _memoized_f(func, *args, **kwargs): assert not kwargs, "Only allow positional call" key = cargs + args for arg in key: if isinstance(arg, tuple): for x in arg: assert isinstance(x, allow_types) else: assert isinstance(arg, allow_types) if key in cache.cache: return cache.cache[key] res = func(*args) cache.cache[key] = res cache.dirty = True return res return decorate(f, _memoized_f) return _register
Example #14
Source File: pickle_memoize.py From incubator-tvm with Apache License 2.0 | 4 votes |
def memoize(key, save_at_exit=False): """Memoize the result of function and reuse multiple times. Parameters ---------- key: str The unique key to the file save_at_exit: bool Whether save the cache to file when the program exits Returns ------- fmemoize : function The decorator function to perform memoization. """ def _register(f): """Registration function""" allow_types = (string_types, int, float, tuple) fkey = key + "." + f.__name__ + ".pkl" if fkey not in Cache.cache_by_key: Cache.cache_by_key[fkey] = Cache(fkey, save_at_exit) cache = Cache.cache_by_key[fkey] cargs = tuple(x.cell_contents for x in f.__closure__) if f.__closure__ else () cargs = (len(cargs),) + cargs def _memoized_f(func, *args, **kwargs): assert not kwargs, "Only allow positional call" key = cargs + args for arg in key: if isinstance(arg, tuple): for x in arg: assert isinstance(x, allow_types) else: assert isinstance(arg, allow_types) if key in cache.cache: return cache.cache[key] res = func(*args) cache.cache[key] = res cache.dirty = True return res return decorate(f, _memoized_f) return _register