implement config.setdefault

This commit is contained in:
Mike Fährmann
2015-10-07 00:58:43 +02:00
parent c8e0efe1ff
commit bea33ae9cb
2 changed files with 18 additions and 0 deletions

View File

@@ -68,6 +68,18 @@ def set(keys, value):
conf = temp
conf[keys[-1]] = value
def setdefault(keys, value):
"""Set the value of property 'key' if it doesn't exist"""
conf = _config
for k in keys[:-1]:
try:
conf = conf[k]
except KeyError:
temp = {}
conf[k] = temp
conf = temp
return conf.setdefault(keys[-1], value)
# --------------------------------------------------------------------
# internals