Update:
following the KICS (keep it complicated) principle, I was playing around with attributes that work like dictionary entries. This reminds me of a prototyped language like JavaScript. My motivation is that I wanted a quicker syntax to create, access and save arbitrary config options during development.
This is a first cut that works in CPython3:
Code: Select all
class SettingDict(object):
def __init__(self, d = {}):
object.__setattr__(self, 'dict_', {})
for k, v in d.items():
self.do_setting(k, v)
def __getattr__(self, k):
if k is 'dict_':
return dict_
try:
return self.dict_[k]
except KeyError:
self.do_setting(k, {})
return self.dict_[k]
def do_setting(self, k, v):
if isinstance(v, dict):
d = SettingDict(v)
self.dict_[k] = d
else:
self.dict_[k] = v
def __setattr__(self, k, v):
self.do_setting(k, v)
def items(self):
return self.dict_.items()
def __str__(self, ):
return 'SD'
def toDict(self):
rec = set([])
return self.toDict1(rec)
def toDict1(self, rec):
d = {}
for k, v in self.items():
if set([v]) & rec:
raise Exception("circular ref detected")
rec.update([v])
if isinstance(v, SettingDict):
d[k] = v.toDict1(rec)
else:
d[k] = v
rec.discard(v)
return d
It works like this:
[code]
>>> from settings import SettingDict, pp
>>> sd0 = SettingDict({"aaa": {"BBB": {"sss": {"w": 11232334}, "x": 11232334, "z": 888888888}}})
>>> pp(sd0)
aaa:
BBB:
sss:
w=11232334
x=11232334
z=888888888
>>>
>>> sd0.aaa.BBB.newThing = {'a':1, 'b':2, 'c':3}
>>>
>>> pp(sd0)
aaa:
BBB:
sss:
w=11232334
x=11232334
z=888888888
newThing:
a=1
b=2
c=3
>>> sd0.aaa.BBB.a.b.c.d = 999
>>> pp(sd0)
aaa:
BBB:
sss:
w=11232334
x=11232334
z=888888888
newThing:
a=1
b=2
c=3
a:
b:
c:
d=999
>>> sd0.toDict()
{'aaa': {'BBB': {'sss': {'w': 11232334}, 'x': 11232334, 'z': 888888888, 'newThing': {'a': 1, 'b': 2, 'c': 3}, 'a': {'b': {'c': {'d': 999}}}}}}
>>>
>>> import json
>>> json.dumps(sd0.toDict())
'{"aaa": {"BBB": {"sss": {"w": 11232334}, "x": 11232334, "z": 888888888, "newThing": {"a": 1, "b": 2, "c": 3}, "a": {"b": {"c": {"d": 999}}}}}}'
>>>
It won't work in MP due to the absence of object.__setattr__() and it's hard to find a way to compose an object without it.
The issues around this are traversed
here and
here
A side issue is my use of recursion, which may be solvable using a tail call. I may also need to guard against some other attribute names...
My question is.. does anybody have any ideas on how the initial creation of the 'dict_' attribute could be accomplished using MP? In the meantime, I'm moving on with a conventional dictionary!
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)