-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipleDispatch.py
More file actions
27 lines (22 loc) · 826 Bytes
/
multipleDispatch.py
File metadata and controls
27 lines (22 loc) · 826 Bytes
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
class MultipleDispatch:
def __init__(self):
self.cases = {}
self.default = None
def addCase(self, *nodes):
def f(func):
self.cases[tuple(nodes)] = func
return f
def defaultCase(self, f):
self.default = f
return f
def dispatch(self, *args):
types = tuple(map(type, args))
current_case = self.cases.get(types)
if current_case == None:
if self.default == None: raise Exception("No method with types " + str(types) + " found, and no default exists")
current_case = self.default
return current_case(*args)
def __call__(self, *args):
return self.dispatch(*args)
def __str__(self):
return "{" + ", ".join([str(k) + ": " + f.__name__ for k, f in self.cases.items()]) + "}"