|
| 1 | +# ------------------------------------------------------------------------------------------- |
| 2 | +def time_this(func): |
| 3 | + import time |
| 4 | + |
| 5 | + def wrapper(*args, **kwargs): |
| 6 | + start = time.time() |
| 7 | + r_value = func(*args, **kwargs) |
| 8 | + stop = time.time() |
| 9 | + runtime = stop - start |
| 10 | + print(f'{func.__name__} took {runtime} seconds to exeucte.') |
| 11 | + return r_value |
| 12 | + |
| 13 | + wrapper.__name__ = func.__name__ + '_timed' |
| 14 | + return wrapper |
| 15 | + |
| 16 | + |
| 17 | +@ time_this |
| 18 | +def sum_to_1(stop): |
| 19 | + total = 0 |
| 20 | + for n in range(1, stop + 1): |
| 21 | + total += n |
| 22 | + |
| 23 | + return total |
| 24 | + |
| 25 | +# decorator @time_this is the same as |
| 26 | +sum_to_1 = time_this(sum_to_1) |
| 27 | + |
| 28 | +result_1 = sum_to_1(30) |
| 29 | + |
| 30 | +print(result_1) |
| 31 | + |
| 32 | + |
| 33 | +# ------------------------------------------------------------------------------------------- |
| 34 | +def make_time_this(allowed=10): |
| 35 | + |
| 36 | + def time_this(func): |
| 37 | + import time |
| 38 | + |
| 39 | + def wrapper(*args, **kwargs): |
| 40 | + start = time.time() |
| 41 | + r_value = func(*args, **kwargs) |
| 42 | + stop = time.time() |
| 43 | + runtime = stop - start |
| 44 | + if runtime > allowed: |
| 45 | + print(f'{func.__name__} took {runtime} seconds to exeucte. You might want to optimize it.') |
| 46 | + |
| 47 | + return r_value |
| 48 | + |
| 49 | + wrapper.__name__ = func.__name__ + '_timed' |
| 50 | + return wrapper |
| 51 | + |
| 52 | + return time_this |
| 53 | + |
| 54 | +@ make_time_this(1) |
| 55 | +def sum_to_2(stop): |
| 56 | + total = 0 |
| 57 | + for n in range(1, stop + 1): |
| 58 | + total += n |
| 59 | + |
| 60 | + return total |
| 61 | + |
| 62 | +# decorator @make_time_this(1) is the same as |
| 63 | +sum_to_2 = make_time_this(1)(sum_to_2) |
| 64 | + |
| 65 | +# probably won't tell the time it took to execute (depends on your computer speed) |
| 66 | +result_2 = sum_to_2(30) |
| 67 | + |
| 68 | +print(result_2) |
| 69 | + |
| 70 | +# probably will tell how long it took to exeucte (depends on your computer speed) |
| 71 | +print(sum_to_2(20_000_000)) |
| 72 | + |
| 73 | + |
| 74 | +# Some simpler examples that don't make sense but is allowed. |
| 75 | +# ------------------------------------------------------------------------------------------- |
| 76 | +# use regular factory function ( this is normal ) |
| 77 | +def make_greet_1(): |
| 78 | + |
| 79 | + def greet(name): |
| 80 | + print('hello there', name) |
| 81 | + |
| 82 | + return greet |
| 83 | + |
| 84 | + |
| 85 | +test_1 = make_greet_1() |
| 86 | +test_1('jeremy') |
| 87 | + |
| 88 | +# ------------------------------------------------------------------------------------------- |
| 89 | +# use decorator which inputs the current function, despite not using the inputed function ( not normal ) |
| 90 | +def make_greet_2(function): |
| 91 | + |
| 92 | + def greet(name): |
| 93 | + print('hello there', name) |
| 94 | + |
| 95 | + return greet |
| 96 | + |
| 97 | +@ make_greet_2 |
| 98 | +def test_2(): |
| 99 | + pass |
| 100 | + |
| 101 | +test_2('jeremy') |
| 102 | + |
| 103 | +# ------------------------------------------------------------------------------------------- |
| 104 | +# another case of using a decorator which inputs the current function, despite not using it |
| 105 | +def change_func_to_int(function): |
| 106 | + return 5 |
| 107 | + |
| 108 | +@ change_func_to_int |
| 109 | +def test_3(): |
| 110 | + pass |
| 111 | + |
| 112 | +# no longer a function. just prints out 5 |
| 113 | +print(test_3) |
0 commit comments