Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foo = dict()
- foo['bar'] = 2
- retrieve_name(foo)
- # List of dictionaries for my DataFrame
- list_of_dicts = [n_jobs, users, queues, priorities]
- import inspect
- x,y,z = 1,2,3
- def retrieve_name(var):
- callers_local_vars = inspect.currentframe().f_back.f_locals.items()
- return [var_name for var_name, var_val in callers_local_vars if var_val is var]
- print retrieve_name(y)
- >>> a = []
- >>> b = a
- >>> id(a)
- 140031712435664
- >>> id(b)
- 140031712435664
- def name(**variables):
- return [x for x in variables]
- name(variable=variable)
- x = 'foo'
- y = 'bar'
- d = autodict(x, y)
- print d
- {'x': 'foo', 'y': 'bar'}
- def autodict(*args):
- get_rid_of = ['autodict(', ',', ')', 'n']
- calling_code = inspect.getouterframes(inspect.currentframe())[1][4][0]
- calling_code = calling_code[calling_code.index('autodict'):]
- for garbage in get_rid_of:
- calling_code = calling_code.replace(garbage, '')
- var_names, var_values = calling_code.split(), args
- dyn_dict = {var_name: var_value for var_name, var_value in
- zip(var_names, var_values)}
- return dyn_dict
- import inspect
- def retrieve_name(var):
- """
- Gets the name of var. Does it from the out most frame inner-wards.
- :param var: variable to get name from.
- :return: string
- """
- for fi in reversed(inspect.stack()):
- names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
- if len(names) > 0:
- return names[0]
- >>> locals()['foo']
- {}
- >>> globals()['foo']
- {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement