def model_cache(name): if not cache or cache["name"] != name: models = yield aspectlib.Proceed(name) cache["name"] = name cache["value"] = models yield aspectlib.Return(models) else: yield aspectlib.Return(cache["value"])
def ensure_connection(instance): """Aspect that tries to ensure a DB connection by retrying. Catches name resolution errors, by filtering on OperationalError exceptions that contain name resolution error messages Useful in case the DNS resolution is shaky, as in the case of the Heroku environment """ max_tries = 3 for trial in range(0, max_tries): try: result = yield aspectlib.Proceed yield aspectlib.Return(result) except OperationalError as error: message = str(error) if "could not translate host name" not in message: raise if trial == max_tries - 1: raise sleep(2**trial) LOGGER.warning( "Database connection lost, retrying trial %d: %s", trial, message, )
def aspect(): try: yield aspectlib.Proceed except ZeroDivisionError: pass else: raise AssertionError("didn't raise") yield aspectlib.Return('stuff')
def exception_logger(cutpoint, *args): try: value = yield aspectlib.Proceed except Exception as e: print( u"Raised exception {} for function {} called with arguments: {}" .format(cutpoint.__name__, e, args)) raise else: print(u"Returned {} for {}".format(value, args)) yield aspectlib.Return(value)
def aspect(): try: hist.append('before') hist.append((yield aspectlib.Proceed)) hist.append('after') except Exception: hist.append('error') finally: hist.append('finally') try: hist.append((yield aspectlib.Return('squelched'))) except GeneratorExit: hist.append('closed') raise else: hist.append('consumed')
def recursive_weave(cutpoint: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: ret = yield aspectlib.Proceed if isinstance(ret, GithubObject) or isinstance(ret, PaginatedListBase): checked_weave(ret, handle_rate_limits, methods=aspectlib.ALL_METHODS) if dry_run: checked_weave( ret, enforce_dryrun, methods=re.compile(r"(^edit|^remove|^create|^replace)")) checked_weave(ret, retry_on_server_failure, methods=aspectlib.ALL_METHODS) checked_weave(ret, create_recursive_weave_aspect(dry_run), methods=aspectlib.ALL_METHODS) yield aspectlib.Return(ret)
def aspect(_): assert 'first' == (yield aspectlib.Proceed) assert 'second' == (yield aspectlib.Proceed('second')) yield aspectlib.Return('stuff')
def aspect(): yield aspectlib.Return('stuff')
def foo(arg): result = yield aspectlib.Proceed(arg + 1) yield aspectlib.Return(result)
def aspect(): yield aspectlib.Proceed yield aspectlib.Return('result')
def path_exists(*args): with aspectlib.weave('os.path.exists', log(print_to=sys.stdout)): print(u"Checking if path exists: {}".format( args[1])) # args[0] e instanta clasei yield aspectlib.Return(os.path.exists(args[1]))
def path_exists(*args, **kwargs): with aspectlib.weave('os.path.exists', log(print_to=sys.stdout)): print(u"Checking if path exists: {}".format(args[2])) yield aspectlib.Return(os.path.exists(args[2]))
def check_type_of(*args, **kwargs): print(u"Is right typeof: {}".format(args[0])) yield aspectlib.Return(isinstance(args[0], keras.utils.Sequence))
def _trace_call(cutpoint, *args, **kw): gazerbeam.stamp(cutpoint, args, kw, None) retval = yield aspectlib.Proceed gazerbeam.stamp(cutpoint, args, kw, retval) yield aspectlib.Return(retval)
def wrapped(*args, **kws): result = yield aspectlib.Proceed # Get function result. hash_object = hashlib.sha256(result) # Hash result. hex_dig = hash_object.hexdigest() # Get hex digest. return aspectlib.Return(hex_dig) # Return hex digest.
def aspect(*args): result = yield history.append(args + (result,)) yield aspectlib.Return('bar-' + result)
def mock_stuff(self, value): if value == 'cnp': yield aspectlib.Return('mocked-hash-result') else: yield aspectlib.Proceed
def aspect(*args): history.append(args) args += ':)', yield aspectlib.Proceed(*args) yield aspectlib.Return('bar')
def result_logger(*args): result = yield aspectlib.Proceed print(u"[LOGGER] The return result is: {}".format(result, )) yield aspectlib.Return(result)