-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverloading.py
96 lines (78 loc) · 2.92 KB
/
overloading.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#Lets have some fun.
#You know what decorators are, they look like this:
def decorator(function):
def inner_function(*args, **kwargs):
print args, kwargs
x = function(*args, **kwargs)
return x
return inner_function
@decorator
def func(thing):
print(thing)
func("thing")
#-----------------------------------------------------------------------------------------------
#thats pretty simple.
#now step two is a decorator that can take arguments, the code looks like this
def check(*types):
def decorator(f):
#shenanigans to check function arglength vs. decorator arglength
if len(types) == f.func_code.co_argcount:
def new_f(*args, **kwds):
counter = 0
for (a, t) in zip(args, types):
if isinstance(a, t):
counter += 1
else:
print "you have an error, the var \"{}\" isn't of {}".format(a, str(t))
raise TypeError("")
if counter == f.func_code.co_argcount:
return f(*args, **kwds)
new_f.func_name = f.func_name
return new_f
else:
def fl(*args):
print "your list of types and list of args don't match"
return fl
return decorator
@check(str, int)
def hai(thing, thang):
print thing
hai("hello world", 8)
hai("hi", 8)
#-----------------------------------------------------------------------------------------------
class TypeCheck:
def __init__(self):
#a dict int the form {String name : Dict { Tuple (args) : Function function }}
self.functions = {}
pass
def __call__(self, *types):
def decorator(function):
if function.func_name in self.functions:
if types in self.functions[function.func_name]:
function = self.functions[function.func_name][types]
else:
self.functions[function.func_name][types] = function
#shenanigans
if len(types) == function.func_code.co_argcount:
print self.
def new_function(*args, **kwargs):
counter = 0
for (a, t) in zip(args, types):
if isinstance(a, t):
counter += 1
else:
print "you have an error, the var \"{}\" isn't of {}".format(a, str(t))
if counter == function.func_code.co_argcount:
return function(*args, **kwargs)
new_function.func_name = function.func_name
return new_function
else:
def fl(*args):
print "you're lists don't match"
return fl
return decorator
checker = TypeCheck()
@checker(str)
def test(name):
print name
test(21)