Created
May 7, 2019 01:33
-
-
Save averykhoo/e2aef868e2af0c0e118a282500f895fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def verbose(func): | |
"""Decorator to print function call details - parameters names and effective values""" | |
@wraps(func) | |
def wrapper(*func_args, **func_kwargs): | |
print('func_code.co_varnames =', func.func_code.co_varnames) | |
print('func_code.co_argcount =', func.func_code.co_argcount) | |
print('func_args =', func_args) | |
print('func_kwargs =', func_kwargs) | |
params = [] | |
for argNo in range(func.func_code.co_argcount): | |
argName = func.func_code.co_varnames[argNo] | |
argValue = func_args[argNo] if argNo < len(func_args) else func.func_defaults[ | |
argNo - func.func_code.co_argcount] | |
params.append((argName, argValue)) | |
for argName, argValue in func_kwargs.items(): | |
params.append((argName, argValue)) | |
params = [argName + ' = ' + repr(argValue) for argName, argValue in params] | |
print((func.__name__ + ' ( ' + ', '.join(params) + ' )')) | |
return func(*func_args, **func_kwargs) | |
return wrapper | |
def memo(f): | |
""" | |
memoization decorator using dict subclass | |
only for single-argument functions | |
""" | |
@wraps(f) | |
class Memo(dict): | |
def __missing__(self, key): | |
result = self[key] = f(key) | |
return result | |
return Memo().__getitem__ | |
class memo2(dict): | |
""" | |
memoization decorator via dict subclass | |
takes multiple arguments but not keyword arguments | |
""" | |
def __init__(self, func): | |
self.func = func | |
def __call__(self, *args): | |
return self[args] | |
def __missing__(self, key): | |
result = self[key] = self.func(*key) | |
return result | |
def memo3(func): | |
cache = func.cache = {} | |
@wraps(func) | |
def memoizer(*args, **kwargs): | |
key = str(args) + str(kwargs) | |
if key not in cache: | |
cache[key] = func(*args, **kwargs) | |
return cache[key] | |
return memoizer | |
def synchronized(lock): | |
""" | |
synchronization decorator | |
example use: | |
from threading import Lock | |
my_lock = Lock() | |
@synchronized(my_lock) | |
def foo(*args): | |
pass | |
""" | |
def wrap(f): | |
@wraps(f) | |
def new_function(*args, **kw): | |
lock.acquire() | |
try: | |
return f(*args, **kw) | |
finally: | |
lock.release() | |
return new_function | |
return wrap | |
def map_if_list(func): | |
""" | |
automatically maps if many inputs are given in a list | |
runs the function on each input, returns results (if any) in a list | |
""" | |
@wraps(func) | |
def mapped_func(*args): | |
if all(type(arg) is list for arg in args): | |
return [func(*z_args) for z_args in zip(*args)] | |
return func(*args) | |
return mapped_func | |
@map_if_list | |
def square(x): | |
print(x * x) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment