Advertisement
Guest User

Untitled

a guest
May 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import inspect
  2. import json
  3. from os.path import isfile as exists
  4. from pprint import pprint
  5.  
  6. class TestClass(object):
  7. config_file = dict()
  8. def get_my_name(self):
  9. ans = []
  10. frame = inspect.currentframe().f_back
  11. tmp = dict(list(frame.f_globals.items()) + list(frame.f_locals.items()))
  12. for k, var in tmp.items():
  13. if isinstance(var, self.__class__):
  14. if hash(self) == hash(var) and k is not 'self':
  15. ans.append(k)
  16. return ans[0]
  17. def load(self):
  18. file_name = self.get_my_name()+'.json'
  19. if exists(file_name):
  20. with open(file_name) as f:
  21. self.config_file = json.load(f)
  22. def save(save):
  23. file_name = self.get_my_name()+'.json'
  24. if self.config_file:
  25. with open(file_name) as f:
  26. json.dump(self.config_file, f)
  27. def print_it(self):
  28. if self.config_file:
  29. pprint(self.config_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement