4
1
Given a list of nested menu items,
items = [
'3D/Axis',
'3D/CameraTracker',
'Color/Invert',
'Color/Log2Lin',
'Color/Math/Add',
'Color/Math/Multiply',
'Other/Group',
'Other/NoOp',
'Views/JoinViews',
'Views/ShuffleViews',
'Views/Stereo/Anaglyph',
'Views/Stereo/ReConverge',
]
..and a dummy function to be triggered by each item:
def msg(path):
"""Dummy function used in menu
"""
return path.lower()
Programatically create a hierarchal collection of dictionaries, which is equivalent to manually typing this:
menu = {
'3D': {'Axis': lambda: msg('3D/Axis'),
'CameraTracker': lambda: msg("3D/CameraTracker")},
'Color': {'Invert': lambda: msg('Color/Invert'),
'Log2Lin': lambda: msg('Color/Log2Lin'),
'Math': {'Add': lambda: msg('Color/Add'),
'Multiply': lambda: msg('Color/Multiply')}},
'Other': {'Group': lambda: msg('Other/Group'),
'NoOp': lambda: msg('Other/NoOp')},
'Views': {'JoinViews': lambda: msg('Views/JoinViews'),
'ShuffleViews': lambda: msg('Views/ShuffleViews'),
'Stereo': {'Anaglyph': lambda: msg('Views/Stereo/Anaglyph'),
'ReConverge': lambda: msg('Views/Stereo/ReConverge')}}},
..which could be tested as follows:
assert menu['3D']['Axis']() == '3d/axis'
assert menu['Color']['Invert']() == 'color/invert'
assert menu['Color']['Math']['Add']() == 'color/math/add'
assert menu['Views']['Stereo']['Anaglyph']() == 'views/stereo/anaglyph'
2Code golf questions should go to [codegolf.se], it was decided that they don't belong here anymore (AFAIK). – None – 2012-01-01T07:51:42.773
@dbr If you'd like this migrated, go ahead and flag for it. – None – 2012-01-02T04:08:09.807
2...except we usually don't take gratuitously language-specific questions here :-/ – J B – 2012-01-02T13:03:33.693