Skip to content

Instantly share code, notes, and snippets.

@chriszf
Created November 12, 2013 05:43
Show Gist options
  • Save chriszf/7426071 to your computer and use it in GitHub Desktop.
Save chriszf/7426071 to your computer and use it in GitHub Desktop.
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
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