rewrote over.types.ndict to use composition instead of inheritance due to obscue issues on raspbian
This commit is contained in:
parent
0d1794d4b6
commit
853e05a275
1 changed files with 28 additions and 7 deletions
|
@ -5,9 +5,9 @@ from collections import OrderedDict
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
|
|
||||||
class ndict(OrderedDict):
|
class ndict:
|
||||||
"""
|
"""
|
||||||
An OrderedDict subclass whose keys are exposed as attributes.
|
An OrderedDict wrapper whose keys are exposed as attributes.
|
||||||
|
|
||||||
>>> d = ndict()
|
>>> d = ndict()
|
||||||
>>> d.alpha = 1
|
>>> d.alpha = 1
|
||||||
|
@ -20,17 +20,38 @@ class ndict(OrderedDict):
|
||||||
{"alpha": 1, "beta": 42}
|
{"alpha": 1, "beta": 42}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
__methods__ = ["values", "items"]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
object.__setattr__(self, "d", OrderedDict(*args, **kwargs))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "|" + repr(self.d)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self.d.__iter__()
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
"""
|
"""
|
||||||
@while looking up an attribute
|
@while looking up an attribute
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if name in self:
|
safe_name = name.replace("_", "-")
|
||||||
return self[name]
|
|
||||||
elif name.replace("_", "-") in self:
|
if name in self.__methods__:
|
||||||
return self[name.replace("_", "-")]
|
return getattr(self.d, name)
|
||||||
|
if name in self.d:
|
||||||
|
return self.d[name]
|
||||||
|
elif safe_name in self.d:
|
||||||
|
return self.d[safe_name]
|
||||||
else:
|
else:
|
||||||
raise AttributeError('"ndict" object has no attribute "%s"' %(name))
|
raise AttributeError('"ndict" object has no attribute "%s"' %(name))
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
self[name] = value
|
self.d[name] = value
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self.d[key]
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self.d[key] = value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue