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 model_log(files): start_time = time.time() results = yield aspectlib.Proceed(files) end_time = time.time() - start_time type_prediction = "single" if len(files) == 1 else "sequential" date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") message = "[%s] Request for %s prediction with results: %s Execution Time: %f\n" with open("log.txt", "a") as logfile: logfile.write(message % (date, type_prediction, str(results), end_time))
def encrypt(cut_point, *args, **kws): """ Encrypts the arguments of the given function. """ f = Fernet(crypto_key) # Encrypt all arguments encoded_args = list() for arg in args: if not isinstance(arg, bytes): arg = arg.encode() encoded_args.append(arg) encrypted_args = [f.encrypt(arg) for arg in encoded_args] # Call function with encrypted arguments yield aspectlib.Proceed(*encrypted_args)
def aspect(*args): history.append(args) args += ':)', yield aspectlib.Proceed(*args) yield aspectlib.Return('bar')
def aspect(_): assert 'first' == (yield aspectlib.Proceed) assert 'second' == (yield aspectlib.Proceed('second')) yield aspectlib.Return('stuff')
def intercept(func, *args): intercepted.append(args) yield aspectlib.Proceed(*args)
def foo(arg): yield aspectlib.Proceed(arg + 1)
def foo(arg): result = yield aspectlib.Proceed(arg + 1) yield aspectlib.Return(result)
def aspect(): yield aspectlib.Proceed('something')
def file_checker(*args): with aspectlib.weave('os.path.isfile', log(print_to=sys.stdout)): print(u"Checking if path given is a file: {}".format(args[1])) result = yield aspectlib.Proceed(os.path.isfile(args[1])) print("Result: {}".format(result))