Decorators
utilz.decorators
Custom guards for defensive data analysis compatible with bulwark.
Intended usage is as Python decorators:
from utilz.guards import log_df
@log_df
def myfunc(df):
do some stuff...
expensive(force=False)
A decorator that wraps joblib.Memory
for caching the results of a function to disk.
This is useful for expensive functions that take a while to compute, as rerunning
them will simply load the last results from disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force |
bool
|
clear the cache before rerunning; Default False |
False
|
Source code in utilz/decorators.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
log(func)
Log the type and shape/size/len of the output from a function
Source code in utilz/decorators.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
maybe(function)
A decorator that wraps a function which should take a kwarg called out_file
. If
out_file
exists then it's loaded from disk, otherwise the wrapped function is
called. If the wrapped function takes a kwarg overwrite = True
then it always runs. You can also pass loader_func = callable
to use a custom loading function
@maybe
def expensive_func(data, **kwargs'):
b = something_expensive(data)
np.save(b, kwargs.get('out_file')
return b
# First run will execute the function
expensive_func(data, out_file='result.npy')
# Second run will load from 'result.npy'
expensive_func(data, out_file='result.npy')
Source code in utilz/decorators.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
show(func)
Print result of function call in addition to returning it
Source code in utilz/decorators.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
timeit(func)
Log the run time of a function
Source code in utilz/decorators.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|