Михаил Авраменко, 441 группа AbsoluteMeta 10190
action22k AbsoluteMeta 10027
t1from inspect import getfullargspect1from inspect import getfullargspec
22
3class Absolute(type):3class Absolute(type):
44
5    def __new__(mcls, cls_name, bases_tuple, cls_dict, **kwargs):5    def __new__(mcls, cls_name, bases_tuple, cls_dict, **kwargs):
6        attr_w = kwargs.get('width', 'width')6        attr_w = kwargs.get('width', 'width')
7        attr_h = kwargs.get('height', 'height')7        attr_h = kwargs.get('height', 'height')
88
9        def is_paramless_method(func):9        def is_paramless_method(func):
10            if not callable(func):10            if not callable(func):
11                return False11                return False
12            try:12            try:
13                args = getfullargspec(func).args13                args = getfullargspec(func).args
14                return len(args) == 114                return len(args) == 1
15            except TypeError:15            except TypeError:
16                return False16                return False
17        orig_abs = cls_dict.get('abs')17        orig_abs = cls_dict.get('abs')
18        orig_len = cls_dict.get('__len__')18        orig_len = cls_dict.get('__len__')
19        orig_w = cls_dict.get(attr_w)19        orig_w = cls_dict.get(attr_w)
20        orig_h = cls_dict.get(attr_h)20        orig_h = cls_dict.get(attr_h)
21        if '__abs__' not in cls_dict or not callable(cls_dict['__abs__']21        if '__abs__' not in cls_dict or not callable(cls_dict['__abs__']
>):>):
22            abs_implementation = lambda self: self22            abs_implementation = lambda self: self
23            if is_paramless_method(orig_abs):23            if is_paramless_method(orig_abs):
24                abs_implementation = lambda self: orig_abs(self)24                abs_implementation = lambda self: orig_abs(self)
25            elif is_paramless_method(orig_len):25            elif is_paramless_method(orig_len):
26                abs_implementation = lambda self: len(self)26                abs_implementation = lambda self: len(self)
27            elif is_paramless_method(orig_w) and is_paramless_method(ori27            elif is_paramless_method(orig_w) and is_paramless_method(ori
>g_h):>g_h):
28                abs_implementation = lambda self: getattr(self, attr_w)(28                abs_implementation = lambda self: getattr(self, attr_w)(
>) * getattr(self, attr_h)()>) * getattr(self, attr_h)()
29            elif attr_w in cls_dict and (not callable(orig_w)) and (attr29            elif attr_w in cls_dict and (not callable(orig_w)) and (attr
>_h in cls_dict) and (not callable(orig_h)):>_h in cls_dict) and (not callable(orig_h)):
30                abs_implementation = lambda self: getattr(self, attr_w) 30                abs_implementation = lambda self: getattr(self, attr_w) 
>* getattr(self, attr_h)>* getattr(self, attr_h)
31            cls_dict['__abs__'] = abs_implementation31            cls_dict['__abs__'] = abs_implementation
3232
33        class AbsProxy:33        class AbsProxy:
3434
35            def __get__(self, instance, owner):35            def __get__(self, instance, owner):
36                if instance is None:36                if instance is None:
37                    return self37                    return self
38                return instance.__abs__()38                return instance.__abs__()
39        cls_dict['abs'] = AbsProxy()39        cls_dict['abs'] = AbsProxy()
40        return super().__new__(mcls, cls_name, bases_tuple, cls_dict)40        return super().__new__(mcls, cls_name, bases_tuple, cls_dict)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op