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