For anyone looking for something like this today I have a get_variable_name() function in my argel1200.utilties that does this. You can do a pip install argel1200 to get it.
Documentation:
Called by dumps()
Pulls the variable names from the function that called this function
This function traces back through the call stack, so we have to subtract -1
for every intermediate function, including this function.
Subtract -1 for every intermediate step in the call stack.
So we have: -1 for this function -1 for whoever called it = -2, which is the default.
If there are more functions in the middle then subtract -1 for each of them. For example:
-1 for this function -1 for dumps(), and -1 for whoever called dumps = -3.
:param stack_back: How far back we need to go in the stack (see above description)
:return: Returns the variable name(s)
And here's the function:
import re
import traceback
def get_variable_name(stack_back=-2):
stack = traceback.extract_stack()
caller_name = stack[-2].name
caller_len = len(caller_name)
line = stack[stack_back].line
# Example line: print('fu', 'bar/', argel1200.utilities.dumps(header), '/foobar')
my_line = re.sub(r'(\s|\u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF)+', '', line) # Remove all whitespace
caller_start = my_line.find(caller_name + '(') # Find where the caller string is (e.g. where "dumps(" starts)
caller_end = caller_start + caller_len # And where it ends (the index of the '(' in "dumps(" )
my_line_substr = my_line[caller_end:] # Get a substr of everything past the caller (e.g. "dumps").
# Now let's find all the variable names passed in
vars_passed_in = []
parens = 0
str_start = None
for idx, char in enumerate(my_line_substr):
if char == '(':
parens += 1
str_start = idx + 1
elif char == ',' or char == ')':
vars_passed_in.append(my_line_substr[str_start:idx])
str_start = idx + 1
if char == ')':
parens -= 1
if parens == 0:
break
return vars_passed_in
You can see the code on GitHub: https://github.com/argel1200/argel1200-python/blob/master/argel1200/utilities.py
The function dumps() just below it calls it, so you can see how that works.