Алмаз Сейтхазин (КФ МГУ, кафедра НДС) AbsoluteMeta 10827
david_Yakhin_312 AbsoluteMeta 10856
t1from collections.abc import Callablet1from collections.abc import Callable
22
3class Absolute(type):3class Absolute(type):
44
5    def __new__(mcls, name, bases, namespace, **kwargs):5    def __new__(mcls, name, bases, namespace, **kwargs):
6        width_name = kwargs.pop('width', 'width')6        width_name = kwargs.pop('width', 'width')
7        height_name = kwargs.pop('height', 'height')7        height_name = kwargs.pop('height', 'height')
8        cls = super().__new__(mcls, name, bases, namespace)8        cls = super().__new__(mcls, name, bases, namespace)
9        original_abs = getattr(cls, 'abs', None)9        original_abs = getattr(cls, 'abs', None)
10        existing_abs = getattr(cls, '__abs__', None)10        existing_abs = getattr(cls, '__abs__', None)
11        if not isinstance(existing_abs, Callable):11        if not isinstance(existing_abs, Callable):
1212
13            def __abs__(self, _orig_abs=original_abs, _w_name=width_name13            def __abs__(self, _orig_abs=original_abs, _w_name=width_name
>, _h_name=height_name):>, _h_name=height_name):
14                if isinstance(_orig_abs, Callable):14                if isinstance(_orig_abs, Callable):
15                    func = _orig_abs15                    func = _orig_abs
16                    if hasattr(func, '__get__'):16                    if hasattr(func, '__get__'):
17                        func = func.__get__(self, type(self))17                        func = func.__get__(self, type(self))
18                    try:18                    try:
19                        return func()19                        return func()
20                    except TypeError:20                    except TypeError:
21                        pass21                        pass
22                len_meth = getattr(self, '__len__', None)22                len_meth = getattr(self, '__len__', None)
23                if isinstance(len_meth, Callable):23                if isinstance(len_meth, Callable):
24                    try:24                    try:
25                        return len_meth()25                        return len_meth()
26                    except TypeError:26                    except TypeError:
27                        pass27                        pass
28                w_attr = getattr(self, _w_name, None)28                w_attr = getattr(self, _w_name, None)
29                h_attr = getattr(self, _h_name, None)29                h_attr = getattr(self, _h_name, None)
30                if isinstance(w_attr, Callable) and isinstance(h_attr, C30                if isinstance(w_attr, Callable) and isinstance(h_attr, C
>allable):>allable):
31                    try:31                    try:
32                        return w_attr() * h_attr()32                        return w_attr() * h_attr()
33                    except TypeError:33                    except TypeError:
34                        pass34                        pass
35                if w_attr is not None and h_attr is not None and (not is35                if w_attr is not None and h_attr is not None and (not is
>instance(w_attr, Callable)) and (not isinstance(h_attr, Callable)):>instance(w_attr, Callable)) and (not isinstance(h_attr, Callable)):
36                    return w_attr * h_attr36                    return w_attr * h_attr
37                return self37                return self
38            setattr(cls, '__abs__', __abs__)38            setattr(cls, '__abs__', __abs__)
3939
40        class _AbsDescriptor:40        class _AbsDescriptor:
4141
42            def __get__(self, obj, owner=None):42            def __get__(self, obj, owner=None):
43                if obj is None:43                if obj is None:
4444
45                    def class_abs(instance):45                    def class_abs(instance):
46                        return instance.__abs__()46                        return instance.__abs__()
47                    return class_abs47                    return class_abs
48                return obj.__abs__()48                return obj.__abs__()
49        setattr(cls, 'abs', _AbsDescriptor())49        setattr(cls, 'abs', _AbsDescriptor())
50        return cls50        return cls
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op