-
Notifications
You must be signed in to change notification settings - Fork 40
/
switch.py
38 lines (35 loc) · 1.31 KB
/
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
# This class provides the functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
if __name__ == "__main__":
action = "check"
for case in switch(action):
if case('check'):
print('-------------Now doing in action: {0}'.format(action))
break
if case('update'):
print('-------------Now doing in action: {0}'.format(action))
break
if case('deploy'):
print('-------------Now doing in action: {0}'.format(action))
break
if case():
print('-------------Unsupport action, check your action: {0}'.format(action))
# No need to break here, it'll stop anyway