-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.py
More file actions
55 lines (44 loc) · 1.71 KB
/
method.py
File metadata and controls
55 lines (44 loc) · 1.71 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
class Method():
def __init__(self, name, args=[], body=[], static=False, final=False):
self.name = name
self.args = args
self.body = body
self.static = static
self.final = final
def __eq__(self, other):
eq = self.name == other.name
eq = eq and len(self.args) == len(other.args)
for i, arg in enumerate(other.args):
eq = eq and self.args[i].type == arg.type
return eq
def __hash__(self):
return hash(self.name) + hash(len(self.args))
def __iter__(self):
return self.body.__iter__()
def __str__(self):
return str(self.name) + "(" + ", ".join([arg.type + " " + arg.name for arg in self.args]) + ")"
class MethodSet():
def __init__(self, name):
self.name = name
self.methods = []
def add(self, *methods):
for method in methods:
if method not in self:
self.methods.append(method)
return self
def __iter__(self):
return self.methods.__iter__()
def __getitem__(self, args):
name, params = args
for method in self.methods:
if method.name == name and len(method.args) == len(params):
found = True
for i, param in enumerate(params):
if param.type != method.args[i].type and method.args[i].type != "Object":
found = False
break
if found:
return method
raise NameError(f"Method {name} with parameters ({', '.join([param.type for param in params])}) not found")
def __str__(self):
return "{" + ", ".join([str(method) for method in self.methods]) + "}"