Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. foo = dict()
  2. foo['bar'] = 2
  3.  
  4. retrieve_name(foo)
  5.  
  6. # List of dictionaries for my DataFrame
  7. list_of_dicts = [n_jobs, users, queues, priorities]
  8.  
  9. import inspect
  10.  
  11. x,y,z = 1,2,3
  12.  
  13. def retrieve_name(var):
  14. callers_local_vars = inspect.currentframe().f_back.f_locals.items()
  15. return [var_name for var_name, var_val in callers_local_vars if var_val is var]
  16.  
  17. print retrieve_name(y)
  18.  
  19. >>> a = []
  20. >>> b = a
  21. >>> id(a)
  22. 140031712435664
  23. >>> id(b)
  24. 140031712435664
  25.  
  26. def name(**variables):
  27. return [x for x in variables]
  28.  
  29. name(variable=variable)
  30.  
  31. x = 'foo'
  32. y = 'bar'
  33. d = autodict(x, y)
  34. print d
  35.  
  36. {'x': 'foo', 'y': 'bar'}
  37.  
  38. def autodict(*args):
  39. get_rid_of = ['autodict(', ',', ')', 'n']
  40. calling_code = inspect.getouterframes(inspect.currentframe())[1][4][0]
  41. calling_code = calling_code[calling_code.index('autodict'):]
  42. for garbage in get_rid_of:
  43. calling_code = calling_code.replace(garbage, '')
  44. var_names, var_values = calling_code.split(), args
  45. dyn_dict = {var_name: var_value for var_name, var_value in
  46. zip(var_names, var_values)}
  47. return dyn_dict
  48.  
  49. import inspect
  50.  
  51.  
  52. def retrieve_name(var):
  53. """
  54. Gets the name of var. Does it from the out most frame inner-wards.
  55. :param var: variable to get name from.
  56. :return: string
  57. """
  58. for fi in reversed(inspect.stack()):
  59. names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
  60. if len(names) > 0:
  61. return names[0]
  62.  
  63. >>> locals()['foo']
  64. {}
  65. >>> globals()['foo']
  66. {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement