-
Notifications
You must be signed in to change notification settings - Fork 6
/
overloaded_methods.py
152 lines (102 loc) · 4.08 KB
/
overloaded_methods.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
import sys
PY3 = sys.version_info[0] >= 3
declared_overloads = {}
def _get_func_name(arg_names, arg_defaults):
if len(arg_defaults) < len(arg_names):
args = arg_names[:-len(arg_defaults)]
arg_names = arg_names[-len(arg_defaults):]
else:
args = []
for i, arg_name in enumerate(arg_names):
arg_default = (
str(type(arg_defaults[i])).split("'", 1)[1].rsplit("'")[0]
)
args += [arg_name + '=' + arg_default]
return args
class _OverloadedMetaClass(type):
def __new__(mcs, name, bases, dct):
if declared_overloads:
dct['__overloaded_funcs'] = {}
for func_name, overloaded_funcs in declared_overloads.items():
if func_name in dct:
del dct[func_name]
for items in overloaded_funcs:
name = items[0]
wrapper = items[3]
dct[name] = wrapper
dct['__overloaded_funcs'][func_name] = overloaded_funcs[:]
declared_overloads.clear()
return type.__new__(mcs, name, bases, dct)
def overload(func):
arg_names = func.__code__.co_varnames
arg_defaults = func.__defaults__
if func.__name__ not in declared_overloads:
declared_overloads[func.__name__] = []
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
if arg_names and arg_names[0] == 'self':
arg_names =arg_names[1:]
args = _get_func_name(arg_names, arg_defaults)
wrapper.__name__ = 'overloaded_{0}({1})'.format(
func.__name__,
', '.join(args)
)
declared_overloads[func.__name__] += [
[wrapper.__name__, arg_names, arg_defaults, wrapper]
]
return wrapper
class _OverloadedWrapper(object):
def __init__(self, parent, name, funcs):
self.parent = parent
self.__name__ = name
self.funcs = funcs
def __call__(self, *args, **kwargs):
for wrapper_name, arg_names, arg_defaults, wrapper in self.funcs:
if len(args) + len(list(kwargs.keys())) != len(arg_names):
continue
if len(arg_defaults) != len(list(kwargs.keys())):
continue
kwarg_types = []
arg_defaults = list(type(arg) for arg in arg_defaults)
if len(arg_defaults) < len(arg_names):
keyword_names = arg_names[-len(arg_defaults):]
else:
keyword_names = arg_names
for name in keyword_names:
if name in kwargs:
kwarg_types += [type(kwargs[name])]
if kwarg_types != arg_defaults:
continue
return getattr(self.parent, wrapper_name)(*args, **kwargs)
if PY3:
class OverloadedClass(object, metaclass=_OverloadedMetaClass):
def __getattr__(self, item):
if item in self.__dict__:
return self.__dict__[item]
if '__overloaded_funcs' in self.__class__.__dict__:
if item in self.__class__.__dict__['__overloaded_funcs']:
return _OverloadedWrapper(
self,
item,
self.__class__.__dict__['__overloaded_funcs'][item]
)
if item in self.__class__.__dict__:
return self.__class__.__dict__[item]
raise AttributeError(item)
else:
class OverloadedClass(object):
__metaclass__ = _OverloadedMetaClass
def __getattr__(self, item):
if item in self.__dict__:
return self.__dict__[item]
if '__overloaded_funcs' in self.__class__.__dict__:
if item in self.__class__.__dict__['__overloaded_funcs']:
return _OverloadedWrapper(
self,
item,
self.__class__.__dict__['__overloaded_funcs'][item]
)
if item in self.__class__.__dict__:
return self.__class__.__dict__[item]
raise AttributeError(item)