def __setitem__(self, key, val):
"""Allow using [] syntax to save a keyvalue.
- It is case-insensitive, so it will overwrite a key which only
differs by case.
"""
if isinstance(val, bool):
val = '1' if val else '0'
key_fold = key.casefold()
for k in self.keys:
if k.casefold() == key_fold:
# Check case-insensitively for this key first
orig_val = self.keys.get(k)
self.keys[k] = str(val)
break
else:
orig_val = self.keys.get(key)
self.keys[key] = str(val)
# Update the by_class/target dicts with our new value
if key_fold == 'classname':
with suppress(KeyError):
self.map.by_class[orig_val].remove(self)
self.map.by_class[val].add(self)
elif key_fold == 'targetname':
with suppress(KeyError):
self.map.by_target[orig_val].remove(self)
self.map.by_target[val].add(self)
评论列表
文章目录