Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class DictJ(object):
  4. """Dictionary with getters and setters"""
  5. def __init__(self, data):
  6. self._dict = data
  7.  
  8. def __getattr__(self, key):
  9. val = self._dict.get(key, None)
  10. if isinstance(val, dict):
  11. return DictJ(val)
  12. return val
  13.  
  14. def update(self, data):
  15. self._d.update(data)
  16.  
  17. def __setattr__(self, key, value):
  18. if key == '_dict':
  19. self.__dict__[key] = value
  20. else:
  21. self.__dict__['_dict'][key] = value
  22.  
  23. def __contains__(self, item):
  24. return item in self._dict
  25.  
  26. def keys(self):
  27. return self._dict.keys()
  28.  
  29. @property
  30. def dict(self):
  31. return self._dict
  32.  
  33. def __repr__(self):
  34. return str(self._dict)
  35.  
  36.  
  37. aaa = {"test1":1, "test2":False, "test3":False, "test4": {"test5": False}}
  38.  
  39. a = DictJ(aaa)
  40. a.aaa = "bbb"
  41. print(a.dict)
  42. a.test4.test6 = "aaa"
  43. print("test5" in a.test4)
  44. print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement