Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- class DictJ(object):
- """Dictionary with getters and setters"""
- def __init__(self, data):
- self._dict = data
- def __getattr__(self, key):
- val = self._dict.get(key, None)
- if isinstance(val, dict):
- return DictJ(val)
- return val
- def update(self, data):
- self._d.update(data)
- def __setattr__(self, key, value):
- if key == '_dict':
- self.__dict__[key] = value
- else:
- self.__dict__['_dict'][key] = value
- def __contains__(self, item):
- return item in self._dict
- def keys(self):
- return self._dict.keys()
- @property
- def dict(self):
- return self._dict
- def __repr__(self):
- return str(self._dict)
- aaa = {"test1":1, "test2":False, "test3":False, "test4": {"test5": False}}
- a = DictJ(aaa)
- a.aaa = "bbb"
- print(a.dict)
- a.test4.test6 = "aaa"
- print("test5" in a.test4)
- print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement