return '\n'.join(lines)
def select_config(self, setting_name, attribute_value=None,
- attribute_name='name'):
+ get_attribute=lambda value : value['name']):
"""Select a `Config` instance from `ConfigListSetting` values
If your don't want to select `ConfigListSetting` items by
>>> child['name']
'Jack'
- `attribute_value` defaults to `name`, because I expect that to
- be the most common case.
+ `get_attribute` defaults to returning `value['name']`, because
+ I expect that to be the most common case, but you can use
+ another function if neccessary, for example to drill down more
+ than one level. Here we select by grandchild name:
+
+ >>> for name in ['Jack', 'Jill']:
+ ... grandchild = TestConfig()
+ ... grandchild['name'] = name + ' Junior'
+ ... child = c.select_config('children', name)
+ ... child['children'] = [grandchild]
+ >>> get_grandchild = lambda value : value['children'][0]['name']
+ >>> get_grandchild(child)
+ 'Jill Junior'
+ >>> child = c.select_config('children', 'Jack Junior',
+ ... get_attribute=get_grandchild)
+ >>> child # doctest: +ELLIPSIS
+ <TestConfig ...>
+ >>> child['name']
+ 'Jack'
"""
- setting_value = self[setting_name]
+ setting_value = self.get(setting_name, None)
if not setting_value:
- raise KeyError(setting_value)
+ raise KeyError(setting_name)
for item in setting_value:
- if item[attribute_name] == attribute_value:
+ if get_attribute(item) == attribute_value:
return item
raise KeyError(attribute_value)