| 1 |
import functools |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
def compose(*funcs): |
| 6 |
def compose_two(f1, f2): |
| 7 |
return lambda *args, **kwargs: f1(f2(*args, **kwargs)) |
| 8 |
|
| 9 |
return functools.reduce(compose_two, funcs) |
| 10 |
|
| 11 |
|
| 12 |
def apply(transform): |
| 13 |
def wrap(func): |
| 14 |
return functools.wraps(func)(compose(transform, func)) |
| 15 |
|
| 16 |
return wrap |
| 17 |
|
| 18 |
|
| 19 |
def result_invoke(action): |
| 20 |
def wrap(func): |
| 21 |
@functools.wraps(func) |
| 22 |
def wrapper(*args, **kwargs): |
| 23 |
result = func(*args, **kwargs) |
| 24 |
action(result) |
| 25 |
return result |
| 26 |
|
| 27 |
return wrapper |
| 28 |
|
| 29 |
return wrap |
| 30 |
|
| 31 |
|
| 32 |
def identity(x): |
| 33 |
return x |
| 34 |
|
| 35 |
|
| 36 |
def bypass_when(check, *, _op=identity): |
| 37 |
def decorate(func): |
| 38 |
@functools.wraps(func) |
| 39 |
def wrapper(param, /): |
| 40 |
return param if _op(check) else func(param) |
| 41 |
|
| 42 |
return wrapper |
| 43 |
|
| 44 |
return decorate |