1
|
|
|
class Decorator: |
2
|
|
|
def __new__(self, *args, **kwargs): |
3
|
|
|
self.decoree = None |
4
|
|
|
self.newargs = args |
5
|
|
|
self.newkwargs = kwargs |
6
|
|
|
self.decorators = {} |
7
|
|
|
if "decorators" in self.newkwargs: |
8
|
|
|
self.decorators = self.newargs["decorators"] |
9
|
|
|
if args: |
10
|
|
|
if isinstance(args[0], type): |
11
|
|
|
self.decoree = args[0] |
12
|
|
|
if callable(args[0]): |
13
|
|
|
if len(args) == 1 and len(kwargs) == 0: |
14
|
|
|
return args[0] |
15
|
|
|
else: |
16
|
|
|
pass |
17
|
|
|
return self |
18
|
|
|
|
19
|
|
|
def __init__(self, *args, **kwargs): |
20
|
|
|
self.decoree = None |
21
|
|
|
self.initargs = args |
22
|
|
|
self.initkwargs = kwargs |
23
|
|
|
if args and callable(args[0]): |
24
|
|
|
self.decoree = args[0] |
25
|
|
|
|
26
|
|
|
def __call__(self, *args, **kwargs): |
27
|
|
|
if args and callable(args[0]): |
28
|
|
|
self.decoree = args[0] |
29
|
|
|
if isinstance(self.decoree, type): |
30
|
|
|
return self.create_wrapping_class(self.decoree, self.decorators) |
31
|
|
|
return self.decoree |
32
|
|
|
elif self.decoree: |
33
|
|
|
if isinstance(self.decoree, type): |
34
|
|
|
return self.create_wrapping_class(args[0], self.decorators)(*args, **kwargs) |
35
|
|
|
return self.decoree(*args, **kwargs) |
36
|
|
|
|
37
|
|
|
@staticmethod |
38
|
|
|
def create_wrapping_class(cls, decorators): |
39
|
|
|
from six import with_metaclass |
40
|
|
|
|
41
|
|
|
class MetaNewClass(type): |
42
|
|
|
def __repr__(self): |
43
|
|
|
return repr(cls) |
44
|
|
|
|
45
|
|
|
class NewClass(with_metaclass(MetaNewClass, cls)): |
46
|
|
|
def __init__(self, *args, **kwargs): |
47
|
|
|
self.__instance = cls(*args, **kwargs) |
48
|
|
|
"This is the overwritten class" |
49
|
|
|
def __getattribute__(self, attr_name): |
50
|
|
|
if attr_name == "__class__": |
51
|
|
|
return cls |
52
|
|
|
obj = super(NewClass, self).__getattribute__(attr_name) |
53
|
|
|
if hasattr(obj, '__call__'): |
54
|
|
|
if attr_name in decorators: |
55
|
|
|
for decorator in decorators: |
56
|
|
|
obj = decorator(obj) |
57
|
|
|
elif "*" in decorators: |
58
|
|
|
for decorator in decorators: |
59
|
|
|
obj = decorator(obj) |
60
|
|
|
return obj |
61
|
|
|
return obj |
62
|
|
|
|
63
|
|
|
def __repr__(self): |
64
|
|
|
return repr(self.__instance) |
65
|
|
|
return NewClass |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def lcs(a, b): |
69
|
|
|
lengths = [[0 for j in range(len(b)+1)] for i in range(len(a)+1)] |
70
|
|
|
# row 0 and column 0 are initialized to 0 already |
71
|
|
|
for i, x in enumerate(a): |
72
|
|
|
for j, y in enumerate(b): |
73
|
|
|
if x == y: |
74
|
|
|
lengths[i+1][j+1] = lengths[i][j] + 1 |
75
|
|
|
else: |
76
|
|
|
lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1]) |
77
|
|
|
# read the substring out from the matrix |
78
|
|
|
result = "" |
79
|
|
|
x, y = len(a), len(b) |
80
|
|
|
while x != 0 and y != 0: |
81
|
|
|
if lengths[x][y] == lengths[x-1][y]: |
82
|
|
|
x -= 1 |
83
|
|
|
elif lengths[x][y] == lengths[x][y-1]: |
84
|
|
|
y -= 1 |
85
|
|
|
else: |
86
|
|
|
assert a[x-1] == b[y-1] |
87
|
|
|
result = a[x-1] + result |
88
|
|
|
x -= 1 |
89
|
|
|
y -= 1 |
90
|
|
|
return result |
91
|
|
|
|