Skip to content

Instantly share code, notes, and snippets.

@darkfeline
Created June 1, 2014 03:46
Show Gist options
  • Save darkfeline/01c12efc712f90d5be10 to your computer and use it in GitHub Desktop.
Save darkfeline/01c12efc712f90d5be10 to your computer and use it in GitHub Desktop.
Python switch-case
class SwitchCase:
"""
Simple implementation of switch-case statements in Pythonic style.
>>> switch = SwitchCase()
Adding case handlers:
>>> @switch.case('foo')
... def foo():
... print('foo')
>>> @switch.case('bar')
... def bar():
... print('bar')
Default handler:
>>> @switch.case()
... def baz():
... print('baz')
Use:
>>> switch('foo')
foo
>>> switch('bar')
bar
>>> switch(42)
baz
"""
__slots__ = ['handlers', 'default']
def __init__(self):
self.handlers = dict()
self.default = None
def __call__(self, arg):
x = self.handlers.get(arg, self.default)
if x is not None:
x()
def case(self, param=None):
if param is not None:
def case_adder(handler):
self.handlers[param] = handler
else:
def case_adder(handler):
self.default = handler
return case_adder
class SwitchCaseNames(SwitchCase):
"""
SwitchCase subclass with namespace support via dicts.
>>> switch = SwitchCaseNames()
>>> @switch.case('foo')
... def foo(names):
... names['foo'] = 'bar'
>>> names = dict()
>>> switch('foo', names)
>>> print(names['foo'])
bar
"""
def __call__(self, arg, names):
x = self.handlers.get(arg, self.default)
if x is not None:
x(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment