If I'm understanding correctly, I'd recommend a collections.defaultdict.
This isn't really what I'd call a "switch" statement, but I think the end result is close to what you're looking for.
I can best explain with full code, data, and application.
Obviously, the key line is the defualtdict line.
>>> import collections
>>>
>>> descriptions = {
... 'player_joined_clan' : '%(player)s joined clan %(clan)s',
... 'player_left' : '%(player)s left',
... 'player_hit_player' : '%(player)s (of %(clan)s) hit %(player2)s (of %(clan2)s)',
... }
>>>
>>> data = [
... {'player': 'PlayerA'},
... {'player': 'PlayerB', 'clan' : 'ClanB'},
... {'clan' : 'ClanC'},
... {'clan' : 'ClanDA', 'player2': 'PlayerDB'},
... ]
>>>
>>> for item in data:
... print item
... item = collections.defaultdict(lambda : '"<unknown>"', **item)
... for key in descriptions:
... print ' %s: %s' % (key, descriptions[key] % item)
... print
...
{'player': 'PlayerA'}
player_joined_clan: PlayerA joined clan "<unknown>"
player_left: PlayerA left
player_hit_player: PlayerA (of "<unknown>") hit "<unknown>" (of "<unknown>")
{'clan': 'ClanB', 'player': 'PlayerB'}
player_joined_clan: PlayerB joined clan ClanB
player_left: PlayerB left
player_hit_player: PlayerB (of ClanB) hit "<unknown>" (of "<unknown>")
{'clan': 'ClanC'}
player_joined_clan: "<unknown>" joined clan ClanC
player_left: "<unknown>" left
player_hit_player: "<unknown>" (of ClanC) hit "<unknown>" (of "<unknown>")
{'clan': 'ClanDA', 'player2': 'PlayerDB'}
player_joined_clan: "<unknown>" joined clan ClanDA
player_left: "<unknown>" left
player_hit_player: "<unknown>" (of ClanDA) hit PlayerDB (of "<unknown>")
Or, if you want it more customizable than simply a lambda with one string, you can define your own defaultdict class, such as:
class my_defaultdict(collections.defaultdict):
def __missing__(self, key):
return '<unknown %s>' % key
change the line to use your class instead of the default one:
#item = collections.defaultdict(lambda : '"<unknown>"', **item)
item = my_defaultdict(**item)
and, voila, the output:
{'player': 'PlayerA'}
player_joined_clan: PlayerA joined clan <unknown clan>
player_left: PlayerA left
player_hit_player: PlayerA (of <unknown clan>) hit <unknown player2> (of <unknown clan2>)
{'clan': 'ClanB', 'player': 'PlayerB'}
player_joined_clan: PlayerB joined clan ClanB
player_left: PlayerB left
player_hit_player: PlayerB (of ClanB) hit <unknown player2> (of <unknown clan2>)
{'clan': 'ClanC'}
player_joined_clan: <unknown player> joined clan ClanC
player_left: <unknown player> left
player_hit_player: <unknown player> (of ClanC) hit <unknown player2> (of <unknown clan2>)
{'clan': 'ClanDA', 'player2': 'PlayerDB'}
player_joined_clan: <unknown player> joined clan ClanDA
player_left: <unknown player> left
player_hit_player: <unknown player> (of ClanDA) hit PlayerDB (of <unknown clan2>)
See the documentation for collections.defaultdict for more examples.
Edit:
I forgot that this __missing__
functionality was added to the standard dict
class in python 2.5. So an even simpler approach doesn't even involve collections.defaultdict
-- just subclass dict
:
class my_defaultdict(dict):
def __missing__(self, key):
return '<unknown %s>' % key