Created
November 14, 2018 17:08
-
-
Save gerryjenkinslb/9113f2dfa04e649dd698c2e980e43ede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
""" var_dump('vars') module to dump variables (python 3.6+: uses f strings)""" | |
def var_dump(var_list_str): | |
""" print variables in callers scope indicated in space delimited string param""" | |
# retrieve information needed on callers frame | |
prev_frame = inspect.currentframe().f_back | |
frame_info = inspect.getframeinfo(prev_frame) | |
module_name = inspect.getmodulename(frame_info.filename) | |
output = f"{module_name} line: {frame_info.lineno} in {frame_info.function}()\n " | |
names = prev_frame.f_locals # local namespace from caller as dict | |
var_list = var_list_str.split() | |
output += ', '.join([f'{n}: {repr(names[n])}' for n in var_list]) | |
print(output) | |
def main(): # test it | |
count = 101 | |
greeting = 'hello' | |
amount = 11.11 | |
a_list = [1, 2, 3] | |
var_dump('greeting a_list a_list') | |
var_dump('amount count') | |
if __name__ == '__main__': | |
main() | |
# references: | |
# ref https://docs.python.org/3/library/inspect.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Debug print function to dump callers specified variables using the Python inspect module.