Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NamedDict(dict[str, Any]):
- class _Get:
- __slots__ = ('instance',)
- instance: 'NamedDict'
- def __call__(self, key: str, default=None):
- return super(NamedDict, self.instance).get(key, default)
- def __get__(self, instance, owner=None):
- self.instance = instance
- return self
- def __getattr__(self, name: str):
- return super(NamedDict, self.instance).get(name)
- class _SetDefault:
- __slots__ = ('instance',)
- instance: 'NamedDict'
- def __call__(self, key: str, default=None):
- return super(NamedDict, self.instance).setdefault(key, default)
- def __get__(self, instance, owner=None):
- self.instance = instance
- return self
- def __getattr__(self, name: str):
- def setdefault(default=None):
- return super(NamedDict, self.instance).setdefault(name, default)
- return setdefault
- get = _Get()
- setdefault = _SetDefault()
- def __getattr__(self, name: str):
- return self[name]
- def __setattr__(self, name: str, value):
- if name in vars(self):
- super().__setattr__(name, value)
- else:
- self[name] = value
- def __delattr__(self, name: str):
- if name in vars(self):
- super().__delattr__(name)
- else:
- del self[name]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement