-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswitch.py
44 lines (31 loc) · 842 Bytes
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__all__ = ['switch', 'case', 'default', 'fallthrough']
class Switch:
def __init__(self, value):
self.value = value
self.finished = False
def case(self, cond):
if self.finished:
return False
match = False
if hasattr(cond, '__call__'):
if cond(self.value):
match = True
elif cond == self.value:
match = True
if match:
self.finished = True
return match
def fallthrough(self):
self.finished = False
def default(self):
return not self.finished
the_switch = None
def switch(value):
global the_switch
the_switch = Switch(value)
def case(cond):
return the_switch.case(cond)
def fallthrough():
the_switch.fallthrough()
def default():
the_switch.default()