Use OrderedDict for ValueList
This commit is contained in:
parent
c67f0c6482
commit
f679a97efc
@ -84,6 +84,7 @@ class ValueListSection:
|
|||||||
default: An OrderedDict with the default configuration as strings.
|
default: An OrderedDict with the default configuration as strings.
|
||||||
After __init__, the strings become key/value types.
|
After __init__, the strings become key/value types.
|
||||||
types: A tuple for (keytype, valuetype)
|
types: A tuple for (keytype, valuetype)
|
||||||
|
valdict: The "true value" dict.
|
||||||
descriptions: A dict with the description strings for the keys.
|
descriptions: A dict with the description strings for the keys.
|
||||||
Currently a global empty dict to be compatible with
|
Currently a global empty dict to be compatible with
|
||||||
KeyValue section.
|
KeyValue section.
|
||||||
@ -100,8 +101,15 @@ class ValueListSection:
|
|||||||
self.values = OrderedDict()
|
self.values = OrderedDict()
|
||||||
keytype = self.types[0]()
|
keytype = self.types[0]()
|
||||||
valtype = self.types[1]()
|
valtype = self.types[1]()
|
||||||
self.default = {keytype.transform(key): valtype.transform(value)
|
self.default = OrderedDict(
|
||||||
for key, value in self.default.items()}
|
[(keytype.transform(key), valtype.transform(value))
|
||||||
|
for key, value in self.default.items()])
|
||||||
|
|
||||||
|
def update_valdict(self):
|
||||||
|
self.valdict = OrderedDict()
|
||||||
|
self.valdict.update(self.default)
|
||||||
|
if self.values is not None:
|
||||||
|
self.valdict.update(self.values)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
"""Get the value for key.
|
"""Get the value for key.
|
||||||
@ -121,22 +129,15 @@ class ValueListSection:
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"""Iterate over all set values."""
|
"""Iterate over all set values."""
|
||||||
# FIXME using a custon iterator this could be done more efficiently
|
# FIXME using a custon iterator this could be done more efficiently
|
||||||
valdict = self.default
|
self.update_valdict()
|
||||||
if self.values is not None:
|
return self.valdict.__iter__()
|
||||||
valdict.update(self.values)
|
|
||||||
return valdict.__iter__()
|
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
"""Get boolean state of section."""
|
"""Get boolean state of section."""
|
||||||
# FIXME we really should cache valdict
|
self.update_valdict()
|
||||||
valdict = self.default
|
return bool(self.valdict)
|
||||||
if self.values is not None:
|
|
||||||
valdict.update(self.values)
|
|
||||||
return bool(valdict)
|
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
"""Get dict items."""
|
"""Get dict items."""
|
||||||
valdict = self.default
|
self.update_valdict()
|
||||||
if self.values is not None:
|
return self.valdict.items()
|
||||||
valdict.update(self.values)
|
|
||||||
return valdict.items()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user