Passed
Push — master ( 93e9b6...c3e849 )
by Max
54s
created

structured_data._conditional_method   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ConditionalMethod.__set_name__() 0 3 1
A ConditionalMethod.__get__() 0 5 3
A ConditionalMethod.__delete__() 0 3 1
A ConditionalMethod.__init__() 0 3 1
A ConditionalMethod.__set__() 0 3 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A conditional_method() 0 3 1
1
import functools
2
3
from . import _attribute_constructor
4
5
6
class ConditionalMethod:
7
    name = None
8
    __objclass__ = None
9
10
    def __init__(self, source, field_check):
11
        self.source = source
12
        self.field_check = field_check
13
14
    def __set_name__(self, owner, name):
15
        self.__objclass__ = owner
16
        self.name = name
17
18
    def __get__(self, instance, owner):
19
        if getattr(owner, self.field_check):
20
            return getattr(self.source, self.name).__get__(instance, owner)
21
        target = owner if instance is None else instance
22
        return getattr(super(self.__objclass__, target), self.name)
23
24
    def __set__(self, instance, value):
25
        # Don't care about this coverage
26
        raise AttributeError  # pragma: nocover
27
28
    def __delete__(self, instance):
29
        # Don't care about this coverage
30
        raise AttributeError  # pragma: nocover
31
32
33
def conditional_method(source):
34
    return _attribute_constructor.AttributeConstructor(
35
        functools.partial(ConditionalMethod, source)
36
    )
37