Created
November 12, 2013 05:43
-
-
Save chriszf/7426071 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 case(object): | |
EMPTY_CASE = (None for x in [1]) | |
def __init__(self, *args): | |
self.cases = args | |
self.used = False | |
def __iter__(self): | |
return self | |
def next(self): | |
if not self.used: | |
self.used = True | |
return 1 | |
else: | |
raise StopIteration | |
def switch(var, *cases): | |
dispatch = {} | |
for c in cases: | |
cond = c.gi_frame.f_locals[".0"].cases | |
dispatch.update({case_val: c for case_val in cond}) | |
print dispatch | |
return dispatch.get(var, case.EMPTY_CASE).next() | |
__builtins__['case'] = case | |
__builtins__['switch'] = switch |
This file contains hidden or 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
import switchcase | |
x = 5 | |
print switch(x, ("Odd" for x in case(1, 3, 5)), | |
("Even" for x in case(2, 4, 6))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment