forked from steffi7574/LayerParallelLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
52 lines (44 loc) · 1.49 KB
/
Copy pathswitch.py
File metadata and controls
52 lines (44 loc) · 1.49 KB
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
45
46
47
48
49
50
51
52
# -------------------------------------------------------------------
# Switch Class
# -------------------------------------------------------------------
# source: Brian Beck, PSF License, ActiveState Code
# http://code.activestate.com/recipes/410692/
class switch(object):
""" Readable switch construction
Example:
c = 'z'
for case in switch(c):
if case('a'): pass # only necessary if the rest of the suite is empty
if case('b'): pass
# ...
if case('y'): pass
if case('z'):
print("c is lowercase!")
break
if case('A'): pass
# ...
if case('Z'):
print("c is uppercase!")
break
if case(): # default
print("I dunno what c was!")
source: Brian Beck, PSF License, ActiveState Code
http://code.activestate.com/recipes/410692/
"""
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:
self.fall = True
return True
else:
return False
#: class switch()