Last active
October 25, 2016 11:54
-
-
Save jdiez17/c7279edae5a78728b1c7eb74198d82b7 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 | |
import ast | |
def d(*values): | |
f = inspect.currentframe() | |
f_name = f.f_code.co_name | |
caller_ast = ast.parse(inspect.getsource(f.f_back)) | |
def find_names(root): | |
for child in ast.iter_child_nodes(root): | |
if (isinstance(root, ast.Call) | |
and isinstance(child, ast.Name) | |
and child.id == f_name): | |
names = list(map(lambda arg: arg.id, root.args)) | |
return names | |
res = find_names(child) | |
if res: | |
return res | |
names = find_names(caller_ast) | |
return dict(zip(names, values)) | |
def main(): | |
foo = 1234 | |
bar = "meow" | |
print(d(foo, bar)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment