Ideally, I aim to achieve a class that extends (or is very similar) to dictin Python with additional features:
- Dot-Notation, capable of setting and receiving values
- Key-Value features like
dict(i.e. setitem, getitem) - Can cling operations with spot recording.
The goal is that if I have something like example = DotDict()that, I could do the following against him example.configuration.first= 'first', and he will create instances of the corresponding DotDict in examplewith a very painful warning that if the operation is not assigned, it just needs to raise KeyError, like a dictmake
That's what I naively put together
class DotDict(dict):
def __getattr__(self, key):
""" Make attempts to lookup by nonexistent attributes also attempt key lookups. """
import traceback
import re
s= ''.join(traceback.format_stack(sys._getframe(1),1))
if re.match(r' File.*\n.*[a-zA-Z]+\w*\.[a-zA-Z]+[a-zA-Z0-9_. ]*\s*=\s*[a-zA-Z0-9_.\'"]+',s):
self[key] = DotDict()
return self[key]
return self[key]
def __setattr__(self, key, value):
if isinstance(value,dict):
self[key] = DotDict(value)
self[key] = value
, , , , . , .
, Python , , a.b.c = 3, - getattr(a,b), setattr, .
, , - , , setattr.
Edit:
, user1320237.
class DotDict(dict):
def __getattr__(self, key):
""" Make attempts to lookup by nonexistent attributes also attempt key lookups. """
if self.has_key(key):
return self[key]
import sys
import dis
frame = sys._getframe(1)
if '\x00%c' % dis.opmap['STORE_ATTR'] in frame.f_code.co_code:
self[key] = DotDict()
return self[key]
raise AttributeError('Problem here')
def __setattr__(self, key, value):
if isinstance(value,dict):
self[key] = DotDict(value)
self[key] = value
, . , STORE_ATTR, , a.b.this.doesnt.exist.yet = 'something' . , CPython.