Ian Bicking: the old part of his blog

Re: wsgikit.config (nested keys)

Ian wrote:

Better syntax for nested options than config[section][key]

I ran into the same issue. In my config format, I found that I never needed a depth greater than 2, so the following works pretty well (as a method of the Application object):

def config(self, branch=None, leaf=None, default={}):
    """config([branch], [leaf], default={}) -> tree, branch, or leaf.
    
    If branch is not specified, the entire config dict is returned.
    If branch is specified, but leaf is not, the requested branch
        is returned (or default if it does not exist).
    If branch and leaf are specified, the leaf value is returned (or
        default if either the branch or leaf does not exist).
    
    In the case where either branch or leaf are not found, the config
    tree is not modified--you simply get back empty or default values.
    """
    if branch is None:
        return self._config
    if leaf is None:
        return self._config.get(branch, default)
    return self._config.get(branch, {}).get(leaf, default)

I don't think it would be too hard to convert to n-depth.

Comment on wsgikit.config
by Robert Brewer

Comments:

I was thinking of allowing either a sequence of keys, like config[section, key] or config.get((section, key)), or maybe somehow allow dotted names, like config.get('section.key') (since usually these are constants anyway). But sometimes I do want keys that contain dots, so it's not a complete plan.
# Ian Bicking