Completed
Push — master ( 87fb07...159278 )
by Jerome
01:13
created

MetaNewClass   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
wmc 1
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 future.utils import with_metaclass
40
        class MetaNewClass(type):
41
            def __repr__(self):
42
                return repr(cls)
43
44
        class NewClass(with_metaclass(MetaNewClass,cls)):
45
            def __init__(self,*args,**kwargs):
46
                self.__instance = cls(*args,**kwargs)
47
            "This is the overwritten class"
48
            def __getattribute__(self, attr_name):
49
                if attr_name == "__class__":
50
                    return cls
51
                obj = super(NewClass, self).__getattribute__(attr_name)
52
                if hasattr(obj, '__call__'):
53
                    if attr_name in decorators:
54
                        for decorator in decorators:
55
                            obj = decorator(obj)
56
                    elif "*" in decorators:
57
                        for decorator in decorators:
58
                            obj = decorator(obj)
59
                    return obj
60
                return obj
61
            def __repr__(self):
62
                return repr(self.__instance)
63
        return NewClass
64
65
def lcs(a, b):
66
    lengths = [[0 for j in range(len(b)+1)] for i in range(len(a)+1)]
67
    # row 0 and column 0 are initialized to 0 already
68
    for i, x in enumerate(a):
69
        for j, y in enumerate(b):
70
            if x == y:
71
                lengths[i+1][j+1] = lengths[i][j] + 1
72
            else:
73
                lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1])
74
    # read the substring out from the matrix
75
    result = ""
76
    x, y = len(a), len(b)
77
    while x != 0 and y != 0:
78
        if lengths[x][y] == lengths[x-1][y]:
79
            x -= 1
80
        elif lengths[x][y] == lengths[x][y-1]:
81
            y -= 1
82
        else:
83
            assert a[x-1] == b[y-1]
84
            result = a[x-1] + result
85
            x -= 1
86
            y -= 1
87
    return result
88