Created
June 1, 2014 03:46
-
-
Save darkfeline/01c12efc712f90d5be10 to your computer and use it in GitHub Desktop.
Python switch-case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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