Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Last active October 25, 2016 11:54
Show Gist options
  • Save jdiez17/c7279edae5a78728b1c7eb74198d82b7 to your computer and use it in GitHub Desktop.
Save jdiez17/c7279edae5a78728b1c7eb74198d82b7 to your computer and use it in GitHub Desktop.
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