forked from mikeckennedy/python-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitchlang.py
More file actions
67 lines (49 loc) · 1.82 KB
/
switchlang.py
File metadata and controls
67 lines (49 loc) · 1.82 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Here is a first pass implementation at adding switch
import uuid
from typing import Callable, Any, List
class switch:
__no_result = uuid.uuid4()
def __init__(self, value):
self.value = value
self.cases = {}
self.__result = switch.__no_result
def default(self, func: Callable[[], Any]):
self.case('__default__', func)
def case(self, key, func: Callable[[], Any]):
if isinstance(key, range):
for n in key:
self.case(n, func)
return
if isinstance(key, list):
for i in key:
self.case(i, func)
return
if key in self.cases:
raise ValueError("Duplicate case: {}".format(key))
if not func:
raise ValueError("Action for case cannot be None.")
if not callable(func):
raise ValueError("Func must be callable.")
self.cases[key] = func
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val:
raise exc_val
func = self.cases.get(self.value)
if not func:
func = self.cases.get('__default__')
if not func:
raise Exception("Value does not match any case and there "
"is no default case: value {}".format(self.value))
# noinspection PyCallingNonCallable
self.__result = func()
@property
def result(self):
if self.__result == switch.__no_result:
raise Exception("No result has been computed (did you access switch.result inside the with block?)")
return self.__result
def closed_range(start: int, stop: int, step=1) -> range:
if start >= stop:
raise ValueError("Start must be less than stop.")
return range(start, stop+step, step)