Extract a set of variables by name from another frame. Parameters ---------- *names : str One or more variable names which will be extracted from the caller's frame. depth : integer, optional How many frames in the stack to walk when looking for your variabl
(*names,**kw)
| 21 | #----------------------------------------------------------------------------- |
| 22 | |
| 23 | def extract_vars(*names,**kw): |
| 24 | """Extract a set of variables by name from another frame. |
| 25 | |
| 26 | Parameters |
| 27 | ---------- |
| 28 | *names : str |
| 29 | One or more variable names which will be extracted from the caller's |
| 30 | frame. |
| 31 | |
| 32 | depth : integer, optional |
| 33 | How many frames in the stack to walk when looking for your variables. |
| 34 | The default is 0, which will use the frame where the call was made. |
| 35 | |
| 36 | |
| 37 | Examples |
| 38 | -------- |
| 39 | :: |
| 40 | |
| 41 | In [2]: def func(x): |
| 42 | ...: y = 1 |
| 43 | ...: print(sorted(extract_vars('x','y').items())) |
| 44 | ...: |
| 45 | |
| 46 | In [3]: func('hello') |
| 47 | [('x', 'hello'), ('y', 1)] |
| 48 | """ |
| 49 | |
| 50 | depth = kw.get('depth',0) |
| 51 | |
| 52 | callerNS = sys._getframe(depth+1).f_locals |
| 53 | return dict((k,callerNS[k]) for k in names) |
| 54 | |
| 55 | |
| 56 | def extract_vars_above(*names): |
nothing calls this directly
no outgoing calls
no test coverage detected